Skip to content

Commit 0d962e3

Browse files
committed
fix displaying the current selected DND value
1 parent 3ba6523 commit 0d962e3

5 files changed

Lines changed: 41 additions & 12 deletions

File tree

public/startup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function isDNDModeActive(DNDDuration) {
55
console.log(DNDDuration)
66
if (DNDDuration === 'always') {
77
return true
8-
} else if (typeof DNDDuration === 'number') {
9-
return Boolean(DNDDuration && DNDDuration - new Date().getTime() > 0)
8+
} else if (typeof DNDDuration === 'object') {
9+
return Boolean(DNDDuration.value && DNDDuration.countdown - new Date().getTime() > 0)
1010
} else {
1111
return false
1212
}

src/components/Layout/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const Header = ({
7676

7777
const onUnpauseClicked = () => {
7878
trackDNDDisable()
79-
setDNDDuration(0)
79+
setDNDDuration('never')
8080
}
8181

8282
return (

src/features/settings/components/DNDSettings.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const DNDDurations: DndOption[] = [
1212
{ value: 15, label: 'For 15 minutes' },
1313
{ value: 30, label: 'For 30 minutes' },
1414
{ value: 60, label: 'For 1 hour' },
15-
{ value: 'always', label: 'Until you turn off' },
15+
{ value: 'always', label: 'Until you turn it off' },
1616
]
1717

1818
type DNDSettingsProps = {
@@ -34,7 +34,10 @@ export const DNDSettings = ({ setShowSettings }: DNDSettingsProps) => {
3434
} else {
3535
const value = selectedDNDDuration as number
3636
const futureDate = new Date(new Date().getTime() + value * 60000)
37-
setDNDDuration(futureDate.getTime())
37+
setDNDDuration({
38+
value: selectedDNDDuration,
39+
countdown: futureDate.getTime(),
40+
})
3841
}
3942

4043
trackDNDEnable(selectedDNDDuration)
@@ -49,6 +52,18 @@ export const DNDSettings = ({ setShowSettings }: DNDSettingsProps) => {
4952
setSelectedDNDDuration(selectedOption.value)
5053
}
5154

55+
const getDefaultValue = (): DndOption | undefined => {
56+
if (typeof DNDDuration === 'string') {
57+
return DNDDurations.find((e) => e.value === DNDDuration)
58+
} else if (typeof DNDDuration === 'object') {
59+
const DNDDurationObject = DNDDuration as {
60+
value: number
61+
countdown: number
62+
}
63+
return DNDDurations.find((e) => e.value === DNDDurationObject.value)
64+
}
65+
}
66+
5267
return (
5368
<div className="settingRow">
5469
<p className="settingTitle">
@@ -65,7 +80,7 @@ export const DNDSettings = ({ setShowSettings }: DNDSettingsProps) => {
6580
isMulti={false}
6681
isClearable={false}
6782
isSearchable={false}
68-
value={DNDDurations.find((e) => e.value === DNDDuration)}
83+
defaultValue={getDefaultValue()}
6984
classNamePrefix={'hackertab'}
7085
onChange={onPeriodSelect}
7186
/>

src/stores/preferences.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { Tag } from 'src/features/remoteConfig'
44
import { enhanceTags } from 'src/utils/DataEnhancement'
55
import { create } from 'zustand'
66
import { persist } from 'zustand/middleware'
7-
import { CardSettingsType, ListingMode, SelectedCard, SupportedCardType, Theme } from '../types'
7+
import { CardSettingsType, DNDDuration, ListingMode, SelectedCard, SupportedCardType, Theme } from '../types'
8+
89

910
export type UserPreferencesState = {
1011
userSelectedTags: Tag[]
@@ -19,7 +20,7 @@ export type UserPreferencesState = {
1920
cardsSettings: Record<string, CardSettingsType>
2021
firstSeenDate: number
2122
userCustomCards: SupportedCardType[]
22-
DNDDuration: number | "always"
23+
DNDDuration: DNDDuration
2324
}
2425

2526
type UserPreferencesStoreActions = {
@@ -35,7 +36,7 @@ type UserPreferencesStoreActions = {
3536
markOnboardingAsCompleted: (occupation: Omit<Occupation, 'icon'> | null) => void
3637
setUserCustomCards: (cards: SupportedCardType[]) => void
3738
updateCardOrder: (prevIndex: number, newIndex: number) => void
38-
setDNDDuration: (value: number | "always") => void
39+
setDNDDuration: (value: DNDDuration) => void
3940
isDNDModeActive: () => boolean;
4041
}
4142

@@ -59,7 +60,7 @@ export const useUserPreferences = create(
5960
{ id: 3, name: 'producthunt', type: 'supported' },
6061
],
6162
userCustomCards: [],
62-
DNDDuration: 0,
63+
DNDDuration: "never",
6364
setSearchEngine: (searchEngine: string) => set({ searchEngine: searchEngine }),
6465
setListingMode: (listingMode: ListingMode) => set({ listingMode: listingMode }),
6566
setTheme: (theme: Theme) => set({ theme: theme }),
@@ -95,13 +96,21 @@ export const useUserPreferences = create(
9596

9697
return { cards: newState }
9798
}),
98-
setDNDDuration: (value) => set({ DNDDuration: value }),
99+
setDNDDuration: (value: DNDDuration) => set({ DNDDuration: value }),
99100
isDNDModeActive: () => {
100101
const duration = get().DNDDuration
101102
if (duration === "always") {
102103
return true;
104+
} else if (typeof duration === "object") {
105+
const dndValue = duration as {
106+
value: number
107+
countdown: number
108+
}
109+
return Boolean(dndValue.value && dndValue.countdown - new Date().getTime() > 0)
110+
} else {
111+
return false;
103112
}
104-
return Boolean(duration && duration - new Date().getTime() > 0)
113+
105114
}
106115
}),
107116
{

src/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ export type Option = {
108108
value: string
109109
icon?: React.ReactNode
110110
}
111+
112+
export type DNDDuration = {
113+
value: number
114+
countdown: number
115+
} | "always" | "never"

0 commit comments

Comments
 (0)