-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ (synced-prefs) moving the prefs from metadata.json to the db (#3423)
- Loading branch information
1 parent
5b685ec
commit 6c87d85
Showing
16 changed files
with
202 additions
and
50 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
import { useCallback } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
import { savePrefs } from 'loot-core/client/actions'; | ||
import { useQuery } from 'loot-core/client/query-hooks'; | ||
import { send } from 'loot-core/platform/client/fetch'; | ||
import { q } from 'loot-core/shared/query'; | ||
import { type SyncedPrefs } from 'loot-core/src/types/prefs'; | ||
|
||
import { useLocalPrefs } from './useLocalPrefs'; | ||
|
||
type SetSyncedPrefsAction = (value: Partial<SyncedPrefs>) => void; | ||
|
||
/** @deprecated: please use `useSyncedPref` (singular) */ | ||
export function useSyncedPrefs(): [SyncedPrefs, SetSyncedPrefsAction] { | ||
// TODO: implement real logic (follow-up PR) | ||
const dispatch = useDispatch(); | ||
const setPrefs = useCallback<SetSyncedPrefsAction>( | ||
newPrefs => { | ||
dispatch(savePrefs(newPrefs)); | ||
}, | ||
[dispatch], | ||
const { data: queryData } = useQuery<{ id: string; value: string }[]>( | ||
() => q('preferences').select(['id', 'value']), | ||
[], | ||
); | ||
|
||
const prefs = useMemo<SyncedPrefs>( | ||
() => | ||
queryData.reduce( | ||
(carry, { id, value }) => ({ | ||
...carry, | ||
[id]: value, | ||
}), | ||
{}, | ||
), | ||
[queryData], | ||
); | ||
|
||
return [useLocalPrefs(), setPrefs]; | ||
const setPrefs = useCallback<SetSyncedPrefsAction>(newValue => { | ||
Object.entries(newValue).forEach(([id, value]) => { | ||
send('preferences/save', { | ||
id: id as keyof SyncedPrefs, | ||
value: String(value), | ||
}); | ||
}); | ||
}, []); | ||
|
||
return [prefs, setPrefs]; | ||
} |
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,59 @@ | ||
const SYNCED_PREF_KEYS = [ | ||
'firstDayOfWeekIdx', | ||
'dateFormat', | ||
'numberFormat', | ||
'hideFraction', | ||
'isPrivacyEnabled', | ||
/^show-extra-balances-/, | ||
/^hide-cleared-/, | ||
/^parse-date-/, | ||
/^csv-mappings-/, | ||
/^csv-delimiter-/, | ||
/^csv-has-header-/, | ||
/^ofx-fallback-missing-payee-/, | ||
/^flip-amount-/, | ||
// 'budgetType', // TODO: uncomment when `budgetType` moves from metadata to synced prefs | ||
/^flags\./, | ||
]; | ||
|
||
export default async function runMigration(db, { fs, fileId }) { | ||
await db.execQuery(` | ||
CREATE TABLE preferences | ||
(id TEXT PRIMARY KEY, | ||
value TEXT); | ||
`); | ||
|
||
try { | ||
const budgetDir = fs.getBudgetDir(fileId); | ||
const fullpath = fs.join(budgetDir, 'metadata.json'); | ||
|
||
const prefs = JSON.parse(await fs.readFile(fullpath)); | ||
|
||
if (typeof prefs !== 'object') { | ||
return; | ||
} | ||
|
||
await Promise.all( | ||
Object.keys(prefs).map(async key => { | ||
// Check if the current key is of synced-keys type | ||
if ( | ||
!SYNCED_PREF_KEYS.find(keyMatcher => | ||
keyMatcher instanceof RegExp | ||
? keyMatcher.test(key) | ||
: keyMatcher === key, | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
// insert the synced prefs in the new table | ||
await db.runQuery('INSERT INTO preferences (id, value) VALUES (?, ?)', [ | ||
key, | ||
String(prefs[key]), | ||
]); | ||
}), | ||
); | ||
} catch (e) { | ||
// Do nothing | ||
} | ||
} |
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
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,21 @@ | ||
import { type SyncedPrefs } from '../../types/prefs'; | ||
import { createApp } from '../app'; | ||
import * as db from '../db'; | ||
import { mutator } from '../mutators'; | ||
import { undoable } from '../undo'; | ||
|
||
import { PreferencesHandlers } from './types/handlers'; | ||
|
||
export const app = createApp<PreferencesHandlers>(); | ||
|
||
const savePreferences = async ({ | ||
id, | ||
value, | ||
}: { | ||
id: keyof SyncedPrefs; | ||
value: string | undefined; | ||
}) => { | ||
await db.update('preferences', { id, value }); | ||
}; | ||
|
||
app.method('preferences/save', mutator(undoable(savePreferences))); |
8 changes: 8 additions & 0 deletions
8
packages/loot-core/src/server/preferences/types/handlers.d.ts
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,8 @@ | ||
import { type SyncedPrefs } from '../../../types/prefs'; | ||
|
||
export interface PreferencesHandlers { | ||
'preferences/save': (arg: { | ||
id: keyof SyncedPrefs; | ||
value: string | undefined; | ||
}) => Promise<void>; | ||
} |
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
Oops, something went wrong.