forked from actualbudget/actual
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ (synced-prefs) separate metadata and local prefs out (actualbudget…
- Loading branch information
1 parent
4485a63
commit a1bc66b
Showing
10 changed files
with
122 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import { useCallback } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useEffect } from 'react'; | ||
|
||
import { useLocalStorage } from 'usehooks-ts'; | ||
|
||
import { savePrefs } from 'loot-core/src/client/actions'; | ||
import { type State } from 'loot-core/src/client/state-types'; | ||
import { type LocalPrefs } from 'loot-core/src/types/prefs'; | ||
|
||
import { useMetadataPref } from './useMetadataPref'; | ||
|
||
type SetLocalPrefAction<K extends keyof LocalPrefs> = ( | ||
value: LocalPrefs[K], | ||
) => void; | ||
|
||
/** | ||
* Local preferences are scoped to a specific budget file. | ||
*/ | ||
export function useLocalPref<K extends keyof LocalPrefs>( | ||
prefName: K, | ||
): [LocalPrefs[K], SetLocalPrefAction<K>] { | ||
const dispatch = useDispatch(); | ||
const setLocalPref = useCallback<SetLocalPrefAction<K>>( | ||
value => { | ||
dispatch(savePrefs({ [prefName]: value } as LocalPrefs)); | ||
const [budgetId] = useMetadataPref('id'); | ||
|
||
const [value, setValue] = useLocalStorage<LocalPrefs[K]>( | ||
`${budgetId}-${prefName}`, | ||
undefined, | ||
{ | ||
deserializer: JSON.parse, | ||
serializer: JSON.stringify, | ||
}, | ||
[prefName, dispatch], | ||
); | ||
const localPref = useSelector( | ||
(state: State) => state.prefs.local?.[prefName] as LocalPrefs[K], | ||
); | ||
|
||
return [localPref, setLocalPref]; | ||
// Migrate from old pref storage location (metadata.json) to local storage | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const [metadataPref] = useMetadataPref(prefName as any); | ||
useEffect(() => { | ||
if (value !== undefined || metadataPref === undefined) { | ||
return; | ||
} | ||
|
||
setValue(metadataPref); | ||
}, [value, metadataPref, setValue]); | ||
|
||
return [value, setValue]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
category: Maintenance | ||
authors: [MatissJanis] | ||
--- | ||
|
||
SyncedPrefs: separate out MetadataPrefs and LocalPrefs in different storage locations. |