refactor: usePreference + update TS tooling (#2333)
This commit is contained in:
@@ -81,7 +81,7 @@ const ClipperView = ({
|
||||
})
|
||||
}, [application])
|
||||
|
||||
const defaultTagId = usePreference<string>(PrefKey.ClipperDefaultTagUuid)
|
||||
const defaultTagId = usePreference(PrefKey.ClipperDefaultTagUuid)
|
||||
const [defaultTag, setDefaultTag] = useState<SNTag | undefined>()
|
||||
const defaultTagRef = useStateRef(defaultTag)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ApplicationEvent, PrefKey, PrefValue } from '@standardnotes/snjs'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { PrefKey, PrefValue } from '@standardnotes/snjs'
|
||||
import { useApplication } from '../ApplicationProvider'
|
||||
import Dropdown from '../Dropdown/Dropdown'
|
||||
import Modal from '../Modal/Modal'
|
||||
import usePreference from '@/Hooks/usePreference'
|
||||
|
||||
type Props = {
|
||||
exportNotes: () => void
|
||||
@@ -11,14 +11,7 @@ type Props = {
|
||||
|
||||
const SuperExportModal = ({ exportNotes, close }: Props) => {
|
||||
const application = useApplication()
|
||||
const [superNoteExportFormat, setSuperNoteExportFormat] = useState<PrefValue[PrefKey.SuperNoteExportFormat]>(
|
||||
() => application.getPreference(PrefKey.SuperNoteExportFormat) || 'json',
|
||||
)
|
||||
useEffect(() => {
|
||||
return application.addSingleEventObserver(ApplicationEvent.PreferencesChanged, async () => {
|
||||
setSuperNoteExportFormat(application.getPreference(PrefKey.SuperNoteExportFormat) || 'json')
|
||||
})
|
||||
}, [application, superNoteExportFormat])
|
||||
const superNoteExportFormat = usePreference(PrefKey.SuperNoteExportFormat)
|
||||
|
||||
return (
|
||||
<Modal
|
||||
|
||||
@@ -21,7 +21,7 @@ const Moments: FunctionComponent<Props> = ({ application }: Props) => {
|
||||
const momentsEnabled = application.momentsService.isEnabled
|
||||
const premiumModal = usePremiumModal()
|
||||
|
||||
const defaultTagId = usePreference<string>(PrefKey.MomentsDefaultTagUuid)
|
||||
const defaultTagId = usePreference(PrefKey.MomentsDefaultTagUuid)
|
||||
const [defaultTag, setDefaultTag] = useState<SNTag | undefined>()
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
EditorLineHeight,
|
||||
EditorFontSize,
|
||||
EditorLineWidth,
|
||||
PrefValue,
|
||||
} from '@standardnotes/models'
|
||||
import { FeatureIdentifier } from '@standardnotes/snjs'
|
||||
|
||||
@@ -37,4 +38,12 @@ export const PrefDefaults = {
|
||||
[PrefKey.CustomNoteTitleFormat]: 'YYYY-MM-DD [at] hh:mm A',
|
||||
[PrefKey.UpdateSavingStatusIndicator]: true,
|
||||
[PrefKey.PaneGesturesEnabled]: true,
|
||||
} as const
|
||||
[PrefKey.MomentsDefaultTagUuid]: undefined,
|
||||
[PrefKey.ClipperDefaultTagUuid]: undefined,
|
||||
[PrefKey.DefaultEditorIdentifier]: FeatureIdentifier.PlainEditor,
|
||||
[PrefKey.SuperNoteExportFormat]: 'json',
|
||||
[PrefKey.SystemViewPreferences]: {},
|
||||
[PrefKey.AuthenticatorNames]: '',
|
||||
} satisfies {
|
||||
[key in PrefKey]: PrefValue[key]
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { useApplication } from '@/Components/ApplicationProvider'
|
||||
import { PrefDefaults } from '@/Constants/PrefDefaults'
|
||||
import { ApplicationEvent, PrefKey } from '@standardnotes/snjs'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function usePreference<T>(preference: PrefKey) {
|
||||
export default function usePreference<Key extends PrefKey>(preference: Key) {
|
||||
const application = useApplication()
|
||||
|
||||
const [value, setValue] = useState<T>(application.getPreference(preference) as T)
|
||||
const [value, setValue] = useState(application.getPreference(preference, PrefDefaults[preference]))
|
||||
|
||||
useEffect(() => {
|
||||
return application.addEventObserver(async () => {
|
||||
const latestValue = application.getPreference(preference)
|
||||
const latestValue = application.getPreference(preference, PrefDefaults[preference])
|
||||
|
||||
setValue(latestValue as T)
|
||||
setValue(latestValue)
|
||||
}, ApplicationEvent.PreferencesChanged)
|
||||
}, [application, preference])
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { WebApplication } from '@/Application/WebApplication'
|
||||
import { InvisibleSuperConverter } from '@/Components/SuperEditor/Tools/InvisibleMarkdownConverter'
|
||||
import { PrefDefaults } from '@/Constants/PrefDefaults'
|
||||
import { NoteType, PrefKey, SNNote } from '@standardnotes/snjs'
|
||||
|
||||
export const getNoteFormat = (application: WebApplication, note: SNNote) => {
|
||||
@@ -8,7 +9,10 @@ export const getNoteFormat = (application: WebApplication, note: SNNote) => {
|
||||
const isSuperNote = note.noteType === NoteType.Super
|
||||
|
||||
if (isSuperNote) {
|
||||
const superNoteExportFormatPref = application.getPreference(PrefKey.SuperNoteExportFormat) || 'json'
|
||||
const superNoteExportFormatPref = application.getPreference(
|
||||
PrefKey.SuperNoteExportFormat,
|
||||
PrefDefaults[PrefKey.SuperNoteExportFormat],
|
||||
)
|
||||
|
||||
return superNoteExportFormatPref
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user