Skip to content

Commit

Permalink
LocalPrefKey implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-411 committed Nov 13, 2024
1 parent 6e222cf commit 03c3d6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
19 changes: 16 additions & 3 deletions packages/services/src/Domain/Preferences/LocalPrefKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EditorFontSize, EditorLineHeight, EditorLineWidth } from '@standardnotes/models'
import { NativeFeatureIdentifier } from '@standardnotes/features'

export enum LocalPrefKey {
ListPaneCollapsed = 'listPaneCollapsed',
Expand Down Expand Up @@ -31,6 +32,18 @@ export type LocalPrefValue = {
}

export const LocalPrefDefaults = {
listPaneCollapsed: false,
navigationPaneCollapsed: false
}
[LocalPrefKey.ListPaneCollapsed]: false,
[LocalPrefKey.NavigationPaneCollapsed]: false,
[LocalPrefKey.ActiveThemes]: [],
[LocalPrefKey.UseSystemColorScheme]: false,
[LocalPrefKey.UseTranslucentUI]: true,
[LocalPrefKey.AutoLightThemeIdentifier]: 'Default',
[LocalPrefKey.AutoDarkThemeIdentifier]: NativeFeatureIdentifier.TYPES.DarkTheme,

[LocalPrefKey.EditorMonospaceEnabled]: false,
[LocalPrefKey.EditorLineHeight]: EditorLineHeight.Normal,
[LocalPrefKey.EditorLineWidth]: EditorLineWidth.FullWidth,
[LocalPrefKey.EditorFontSize]: EditorFontSize.Normal
} satisfies {
[key in LocalPrefKey]: LocalPrefValue[key]
}
6 changes: 3 additions & 3 deletions packages/web/src/javascripts/Hooks/usePreference.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useApplication } from '@/Components/ApplicationProvider'
import { ApplicationEvent, PrefKey, PrefDefaults, LocalPrefKey, LocalPrefValue } from '@standardnotes/snjs'
import { ApplicationEvent, PrefKey, PrefDefaults, LocalPrefKey, LocalPrefValue, LocalPrefDefaults } from '@standardnotes/snjs'
import { useCallback, useEffect, useState } from 'react'

export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {
const application = useApplication()

const [value, setValue] = useState(application.preferences.getLocalValue(preference, PrefDefaults[preference]))
const [value, setValue] = useState(application.preferences.getLocalValue(preference, LocalPrefDefaults[preference]))

const setNewValue = useCallback(
(newValue: LocalPrefValue[Key]) => {
Expand All @@ -16,7 +16,7 @@ export function useLocalPreference<Key extends LocalPrefKey>(preference: Key) {

useEffect(() => {
return application.addEventObserver(async () => {
const latestValue = application.preferences.getLocalValue(preference, PrefDefaults[preference])
const latestValue = application.preferences.getLocalValue(preference, LocalPrefDefaults[preference])

setValue(latestValue)
}, ApplicationEvent.LocalPreferencesChanged)
Expand Down

0 comments on commit 03c3d6c

Please sign in to comment.