Skip to content

Commit

Permalink
Merge branch 'component-accounts-to-typescript' of https://github.com…
Browse files Browse the repository at this point in the history
…/tlesicka/actual into component-accounts-to-typescript
  • Loading branch information
tlesicka committed Dec 28, 2024
2 parents e9385b8 + ac4d4a7 commit b904981
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback, useEffect, useMemo } from 'react';
import {
getNumberFormat,
integerToCurrency,
isNumberFormat,
parseNumberFormat,
setNumberFormat,
} from 'loot-core/src/shared/util';

Expand Down Expand Up @@ -64,10 +64,7 @@ export function useFormat() {
const [hideFraction] = useSyncedPref('hideFraction');

const config = useMemo(
() => ({
format: isNumberFormat(numberFormat) ? numberFormat : 'comma-dot',
hideFraction: String(hideFraction) === 'true',
}),
() => parseNumberFormat({ format: numberFormat, hideFraction }),
[numberFormat, hideFraction],
);

Expand Down
18 changes: 16 additions & 2 deletions packages/loot-core/src/client/actions/prefs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { send } from '../../platform/client/fetch';
import { parseNumberFormat, setNumberFormat } from '../../shared/util';
import {
type GlobalPrefs,
type MetadataPrefs,
Expand All @@ -19,13 +20,26 @@ export function loadPrefs() {
dispatch(closeModal());
}

const [globalPrefs, syncedPrefs] = await Promise.all([
send('load-global-prefs'),
send('preferences/get'),
]);

dispatch({
type: constants.SET_PREFS,
prefs,
globalPrefs: await send('load-global-prefs'),
syncedPrefs: await send('preferences/get'),
globalPrefs,
syncedPrefs,
});

// Certain loot-core utils depend on state outside of the React tree, update them
setNumberFormat(
parseNumberFormat({
format: syncedPrefs.numberFormat,
hideFraction: syncedPrefs.hideFraction,
}),
);

return prefs;
};
}
Expand Down
15 changes: 14 additions & 1 deletion packages/loot-core/src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const NUMBER_FORMATS = [

type NumberFormats = (typeof NUMBER_FORMATS)[number];

export function isNumberFormat(input: string = ''): input is NumberFormats {
function isNumberFormat(input: string = ''): input is NumberFormats {
return (NUMBER_FORMATS as readonly string[]).includes(input);
}

Expand All @@ -259,6 +259,19 @@ let numberFormatConfig: {
hideFraction: false,
};

export function parseNumberFormat({
format,
hideFraction,
}: {
format?: string;
hideFraction?: string | boolean;
}) {
return {
format: isNumberFormat(format) ? format : 'comma-dot',
hideFraction: String(hideFraction) === 'true',
};
}

export function setNumberFormat(config: typeof numberFormatConfig) {
numberFormatConfig = config;
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4038.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [jfdoming]
---

Fix loading of number format preferences at app startup

0 comments on commit b904981

Please sign in to comment.