24 changed files with 257 additions and 29 deletions
@ -0,0 +1,40 @@ |
|||||||
|
import storage from '@/services/local-storage.service' |
||||||
|
import { TNotificationStyle } from '@/types' |
||||||
|
import { createContext, useContext, useState } from 'react' |
||||||
|
|
||||||
|
type TUserPreferencesContext = { |
||||||
|
notificationListStyle: TNotificationStyle |
||||||
|
updateNotificationListStyle: (style: TNotificationStyle) => void |
||||||
|
} |
||||||
|
|
||||||
|
const UserPreferencesContext = createContext<TUserPreferencesContext | undefined>(undefined) |
||||||
|
|
||||||
|
export const useUserPreferences = () => { |
||||||
|
const context = useContext(UserPreferencesContext) |
||||||
|
if (!context) { |
||||||
|
throw new Error('useUserPreferences must be used within a UserPreferencesProvider') |
||||||
|
} |
||||||
|
return context |
||||||
|
} |
||||||
|
|
||||||
|
export function UserPreferencesProvider({ children }: { children: React.ReactNode }) { |
||||||
|
const [notificationListStyle, setNotificationListStyle] = useState( |
||||||
|
storage.getNotificationListStyle() |
||||||
|
) |
||||||
|
|
||||||
|
const updateNotificationListStyle = (style: TNotificationStyle) => { |
||||||
|
setNotificationListStyle(style) |
||||||
|
storage.setNotificationListStyle(style) |
||||||
|
} |
||||||
|
|
||||||
|
return ( |
||||||
|
<UserPreferencesContext.Provider |
||||||
|
value={{ |
||||||
|
notificationListStyle, |
||||||
|
updateNotificationListStyle |
||||||
|
}} |
||||||
|
> |
||||||
|
{children} |
||||||
|
</UserPreferencesContext.Provider> |
||||||
|
) |
||||||
|
} |
||||||
Loading…
Reference in new issue