|
| 1 | +import { useState } from 'react' |
| 2 | +import Select, { SingleValue } from 'react-select' |
| 3 | +import { useUserPreferences } from 'src/stores/preferences' |
| 4 | + |
| 5 | +type DndOption = { |
| 6 | + value: number | 'always' |
| 7 | + label: string |
| 8 | +} |
| 9 | + |
| 10 | +const DNDDurations: DndOption[] = [ |
| 11 | + { value: 15, label: 'For 15 minutes' }, |
| 12 | + { value: 30, label: 'For 30 minutes' }, |
| 13 | + { value: 60, label: 'For 1 hour' }, |
| 14 | + { value: 'always', label: 'Until you turn off' }, |
| 15 | +] |
| 16 | + |
| 17 | +type DNDSettingsProps = { |
| 18 | + setShowSettings: (show: boolean) => void |
| 19 | +} |
| 20 | + |
| 21 | +export const DNDSettings = ({ setShowSettings }: DNDSettingsProps) => { |
| 22 | + const [selectedDNDDuration, setSelectedDNDDuration] = useState<DndOption['value']>() |
| 23 | + |
| 24 | + const { pauseTo, setPauseTo } = useUserPreferences() |
| 25 | + |
| 26 | + const onApplyClicked = () => { |
| 27 | + if (!selectedDNDDuration) { |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + if (typeof selectedDNDDuration === 'string') { |
| 32 | + setPauseTo('always') |
| 33 | + } else { |
| 34 | + const value = selectedDNDDuration as number |
| 35 | + const futureDate = new Date(new Date().getTime() + value * 60000) |
| 36 | + setPauseTo(futureDate.getTime()) |
| 37 | + } |
| 38 | + |
| 39 | + setShowSettings(false) |
| 40 | + } |
| 41 | + |
| 42 | + const onPeriodSelect = (selectedOption: SingleValue<DndOption>) => { |
| 43 | + if (!selectedOption) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + setSelectedDNDDuration(selectedOption.value) |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="settingRow"> |
| 52 | + <p className="settingTitle"> |
| 53 | + Do not disturb |
| 54 | + <br /> |
| 55 | + <span className="settingHint">(Pause Hackertab for some moment)</span> |
| 56 | + </p> |
| 57 | + <div className="settingContent"> |
| 58 | + <div className="pauseFormWrapper"> |
| 59 | + <div className="form"> |
| 60 | + <div style={{ flex: 1 }}> |
| 61 | + <Select |
| 62 | + options={DNDDurations} |
| 63 | + placeholder="For x minutes" |
| 64 | + isMulti={false} |
| 65 | + isClearable={false} |
| 66 | + isSearchable={false} |
| 67 | + value={DNDDurations.find((e) => e.value === pauseTo)} |
| 68 | + classNamePrefix={'hackertab'} |
| 69 | + onChange={onPeriodSelect} |
| 70 | + /> |
| 71 | + </div> |
| 72 | + |
| 73 | + <button className="pauseButton" onClick={onApplyClicked}> |
| 74 | + Apply |
| 75 | + </button> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ) |
| 81 | +} |
0 commit comments