Skip to content

Commit

Permalink
feat: remove numberLocale setting and use system defaults instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe committed Oct 20, 2020
1 parent 539af21 commit 63e4819
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 98 deletions.
8 changes: 8 additions & 0 deletions main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export default function main() {
'AppleColorPreferencesChangedNotification',
() => windowManager.broadcast('UPDATE_COLOR_PREFERENCES'),
);
let localeCC = app.getLocaleCountryCode();
setInterval(() => {
const newCC = app.getLocaleCountryCode();
if (newCC !== localeCC) {
localeCC = newCC;
windowManager.broadcast('UPDATE_LOCALE_COUNTRY_CODE');
}
}, 5000);
app.on('window-all-closed', () => {
defaultMenu.activate();
});
Expand Down
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from './budget';
import { ErrorBoundary, Startup } from './components';
import styles from './App.module.scss';
import { useRetryResource } from './lib';
import { useNumberFormatter, useRetryResource } from './lib';
import { useMoneyMoney } from './moneymoney';

const Welcome = React.lazy(() => import('./views/Welcome'));
Expand All @@ -21,6 +21,7 @@ function App({ readInitialView }: { readInitialView: InitRes }) {
const [view, setView] = useState(initialView);
const [moneyMoney, updateSettings] = useMoneyMoney();
const [state, dispatch] = useBudgetReducer(initialState, updateSettings);
const numberFormatter = useNumberFormatter(state.settings.fractionDigits);
const openBudget = useCallback(() => {
setView('budget');
}, []);
Expand Down Expand Up @@ -48,6 +49,7 @@ function App({ readInitialView }: { readInitialView: InitRes }) {
return (
<Main
view={view}
numberFormatter={numberFormatter}
moneyMoney={moneyMoney}
state={state}
dispatch={dispatch}
Expand Down
12 changes: 10 additions & 2 deletions src/budget/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
import { isLeft } from 'fp-ts/lib/Either';
import { ThrowReporter } from 'io-ts/lib/ThrowReporter';

export const VERSION = '0.0.2';
/**
* ## 0.0.1 -> 0.0.2
* now using uuids to reference categories and accounts
*
* ## 0.0.2 -> 0.0.3
* removed setting.numberLocale in favor of always using system
*/
export const VERSION = '0.0.3';

const categoryShape = t.partial({
amount: t.number,
Expand All @@ -31,7 +38,6 @@ const incomeCategoryShape = t.type(
);
const settingsShape = t.type(
{
numberLocale: t.string,
fractionDigits: t.number,
startDate: t.number,
currency: t.string,
Expand All @@ -43,6 +49,8 @@ const settingsShape = t.type(
);
const optionalSettingsShape = t.partial(
{
/** @deprecated */
numberLocale: t.string,
collapsedCategories: t.array(t.string),
},
'optionalSettings',
Expand Down
16 changes: 0 additions & 16 deletions src/budget/budgetReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export const INITIAL_STATE = unsaved({

export const ACTION_INIT = Symbol('INIT');
export const ACTION_SET_NAME = Symbol('SET_NAME');
export const ACTION_SETTINGS_SET_NUMBER_LOCALE = Symbol(
'ACTION_SETTINGS_SET_NUMBER_LOCALE',
);
export const ACTION_SETTINGS_SET_FRACTION_DIGITS = Symbol(
'ACTION_SETTINGS_SET_FRACTION_DIGITS',
);
Expand Down Expand Up @@ -70,10 +67,6 @@ type SetNameAction = {
type: typeof ACTION_SET_NAME;
payload: string;
};
type SetSettingsNumberLocale = {
type: typeof ACTION_SETTINGS_SET_NUMBER_LOCALE;
payload: string;
};
type SetSettingsFractionDigits = {
type: typeof ACTION_SETTINGS_SET_FRACTION_DIGITS;
payload: number;
Expand Down Expand Up @@ -131,7 +124,6 @@ export type Action =
| SetCategoryValueAction
| SetCategoryRolloverAction
| SetNameAction
| SetSettingsNumberLocale
| SetSettingsFractionDigits
| SetSettingsSelectedAccounts
| SetSettingsStartDate
Expand Down Expand Up @@ -233,14 +225,6 @@ function budgetReducer(state: BudgetState, action: Action): BudgetState {
...state,
name: action.payload,
};
case ACTION_SETTINGS_SET_NUMBER_LOCALE:
return {
...state,
settings: {
...state.settings,
numberLocale: action.payload,
},
};
case ACTION_SETTINGS_SET_FRACTION_DIGITS:
return {
...state,
Expand Down
8 changes: 1 addition & 7 deletions src/budget/useBudgetData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMemo } from 'react';
import { createNumberFormatter } from '../lib';
import type { Transaction, Category, MoneyMoneyRes } from '../moneymoney';
import { BudgetState } from './Types';
import useBudgets from './useBudgets';
Expand All @@ -20,12 +19,8 @@ export default function useBudgetData(
state: BudgetState,
{ readCategories, readTransactions }: MoneyMoneyRes,
) {
const { fractionDigits, numberLocale, incomeCategories } = state.settings;
const { incomeCategories } = state.settings;

const numberFormatter = useMemo(
() => createNumberFormatter(fractionDigits, numberLocale),
[fractionDigits, numberLocale],
);
const transactions = readTransactions();
const [categories, defaultCategories] = readCategories();
const usableCategories = useMemo(() => {
Expand All @@ -49,6 +44,5 @@ export default function useBudgetData(
months,
categories: usableCategories,
extendFuture,
numberFormatter,
};
}
23 changes: 23 additions & 0 deletions src/lib/createNumberFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { remote, ipcRenderer } from 'electron';
import { useEffect, useMemo, useState } from 'react';

type FormatOptions = {
thousandDelimiter?: boolean;
withFractions?: boolean;
Expand Down Expand Up @@ -54,3 +57,23 @@ export default function createNumberFormatter(
}

export type NumberFormatter = ReturnType<typeof createNumberFormatter>;

export function useNumberFormatter(fractionDigits: number) {
const [localeCC, setLocaleCC] = useState(() =>
remote.app.getLocaleCountryCode(),
);
useEffect(() => {
const handler = () => {
setLocaleCC(remote.app.getLocaleCountryCode());
};
ipcRenderer.addListener('UPDATE_LOCALE_COUNTRY_CODE', handler);
return () => {
ipcRenderer.removeListener('UPDATE_LOCALE_COUNTRY_CODE', handler);
};
}, []);

return useMemo(() => createNumberFormatter(fractionDigits, localeCC), [
fractionDigits,
localeCC,
]);
}
5 changes: 4 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export type Resource<T> = ResourceT<T>;

export { default as formatDateKey } from './formatDateKey';
export { default as roundWithFractions } from './roundWithFractions';
export { default as createNumberFormatter } from './createNumberFormatter';
export {
default as createNumberFormatter,
useNumberFormatter,
} from './createNumberFormatter';
export { default as useInputProps } from './useInputProps';
export { default as useAmountInputProps } from './useAmountInputProps';
export { default as getSharedSettings } from './getSharedSettings';
Expand Down
7 changes: 4 additions & 3 deletions src/lib/initialSettings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { remote } from 'electron';
import startOfMonth from 'date-fns/startOfMonth';
import subMonths from 'date-fns/subMonths';
import getToday from './getToday';
import { BudgetState } from '../budget';

export default {
const settings: BudgetState['settings'] = {
accounts: [],
currency: 'EUR',
incomeCategories: [],
fractionDigits: 2,
startDate: startOfMonth(subMonths(getToday(), 1)).getTime(),
startBalance: 0,
numberLocale: remote.app.getLocale(),
};

export default settings;
14 changes: 9 additions & 5 deletions src/views/Budget/Budget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
VisibleMothContextProvider,
getToday,
MonthsContextProvider,
NumberFormatter,
} from '../../lib';
import Month from '../Month';
import BudgetHeader from './Header';
Expand All @@ -25,19 +26,22 @@ type Props = {
state: BudgetState;
dispatch: Dispatch<Action>;
moneyMoney: MoneyMoneyRes;
numberFormatter: NumberFormatter;
};

export default function Budget({ state, dispatch, moneyMoney }: Props) {
export default function Budget({
state,
dispatch,
moneyMoney,
numberFormatter,
}: Props) {
const sidebarRef = useRef<HTMLDivElement | null>(null);
const sliderRef = useRef<HTMLDivElement | null>(null);
const onSliderScrollRef = useRef<((target: HTMLDivElement) => void) | null>(
null,
);
const [scrollTo, setScrollTo] = useState<ScrollTo | null>(null);
const { months, numberFormatter, extendFuture, categories } = useBudgetData(
state,
moneyMoney,
);
const { months, extendFuture, categories } = useBudgetData(state, moneyMoney);
const handleHeaderMonthClick = useCallback(
(key: string) => {
if (!scrollTo) {
Expand Down
12 changes: 10 additions & 2 deletions src/views/Main/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Dispatch, useCallback } from 'react';
import { Action, BudgetState } from '../../budget';
import { useMenu, useSave } from '../../lib';
import { NumberFormatter, useMenu, useSave } from '../../lib';
import { MoneyMoneyRes } from '../../moneymoney';

const Settings = React.lazy(() => import('../Settings'));
Expand All @@ -10,12 +10,14 @@ type MainViewProps = {
view: 'budget' | 'settings';
state: BudgetState;
moneyMoney: MoneyMoneyRes;
numberFormatter: NumberFormatter;
dispatch: Dispatch<Action>;
setView: (view: 'budget' | 'settings') => void;
};
export default function MainView({
moneyMoney,
state,
numberFormatter,
dispatch,
view,
setView,
Expand All @@ -31,7 +33,12 @@ export default function MainView({
switch (view) {
case 'budget':
return (
<Budget moneyMoney={moneyMoney} state={state} dispatch={dispatch} />
<Budget
moneyMoney={moneyMoney}
state={state}
dispatch={dispatch}
numberFormatter={numberFormatter}
/>
);
case 'settings':
return (
Expand All @@ -40,6 +47,7 @@ export default function MainView({
state={state}
dispatch={dispatch}
onClose={openBudget}
numberFormatter={numberFormatter}
/>
);
}
Expand Down
10 changes: 9 additions & 1 deletion src/views/NewBudget/NewBudget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import General from '../Settings/General';
import Categories from '../Settings/Categories';
import useMenu from '../../lib/useMenu';
import { MoneyMoneyRes } from '../../moneymoney';
import { useNumberFormatter } from '../../lib';

type Props = {
state: BudgetState;
Expand All @@ -26,6 +27,8 @@ export default function NewBudget({
throw new Error('Unexpected non-initialized state');
}

const numberFormatter = useNumberFormatter(state.settings.fractionDigits);

return (
<Content
padding
Expand All @@ -51,7 +54,12 @@ export default function NewBudget({
}
>
{page === 'general' && (
<General moneyMoney={moneyMoney} state={state} dispatch={dispatch} />
<General
moneyMoney={moneyMoney}
state={state}
dispatch={dispatch}
numberFormatter={numberFormatter}
/>
)}
{page === 'categories' && (
<Categories moneyMoney={moneyMoney} state={state} dispatch={dispatch} />
Expand Down
21 changes: 6 additions & 15 deletions src/views/Settings/General/General.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import React, { useMemo, Suspense } from 'react';
import { createNumberFormatter } from '../../../lib';
import React, { Suspense } from 'react';
import Setting from '../Setting';
import NameSetting from './Name';
import AccountSetting from './Account';
import NumberLocaleSetting from './NumberLocale';
import FractionDigitsSetting from './FractionDigits';
import StartDateSetting from './StartDate';
import StartBalanceSetting from './StartBalance';
import CurrencySetting from './Currency';
import { Props } from './Types';
import { Loading } from '../../../components';
import { NumberFormatter } from '../../../lib/createNumberFormatter';

export default function Settings(props: Props) {
const { numberLocale, fractionDigits } = props.state.settings;
const numberFormatter = useMemo(
() => createNumberFormatter(fractionDigits, numberLocale),
[fractionDigits, numberLocale],
);

export default function Settings(
props: Props & { numberFormatter: NumberFormatter },
) {
return (
<>
<NameSetting {...props} />
Expand All @@ -29,14 +24,10 @@ export default function Settings(props: Props) {
</Suspense>
</Setting>
<hr />
<Setting label="Number format Example">
{numberFormatter.format(12345.6789)}
</Setting>
<NumberLocaleSetting {...props} />
<FractionDigitsSetting {...props} />
<hr />
<StartDateSetting {...props} />
<StartBalanceSetting {...props} numberFormatter={numberFormatter} />
<StartBalanceSetting {...props} />
</>
);
}
Loading

0 comments on commit 63e4819

Please sign in to comment.