Skip to content

Commit

Permalink
move to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
psybers committed Aug 25, 2024
1 parent 62a7d50 commit 9023686
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useLocation } from 'react-router-dom';
import { popModal } from 'loot-core/client/actions';
import { send } from 'loot-core/src/platform/client/fetch';

import { i18nObjectList } from '../../i18n';
import { useFormatList } from '../../hooks/useFormatList';
import { theme } from '../../style';
import { Button } from '../common/Button2';
import { Modal, ModalCloseButton, ModalHeader } from '../common/Modal2';
Expand All @@ -33,7 +33,7 @@ export function PostsOfflineNotification() {
<DisplayId id={id} type="payees" />
</Text>
));
const payeeNamesList = i18nObjectList(payeesList, t.language);
const payeeNamesList = useFormatList(payeesList, t.language);

return (
<Modal name="schedule-posts-offline-notification">
Expand Down
25 changes: 25 additions & 0 deletions packages/desktop-client/src/hooks/useFormatList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type ReactNode } from 'react';

const interleaveArrays = (...arrays: ReactNode[][]) =>
Array.from(
{
length: Math.max(...arrays.map(array => array.length)),
},
(_, i) => arrays.map(array => array[i]),
).flat();

export function useFormatList(values: ReactNode[], lng: string, opt = {}) {
const formatter = new Intl.ListFormat(lng, {
style: 'long',
type: 'conjunction',
...opt,
});

const placeholders = Array.from(
{ length: values.length },
(_, i) => `<${i}>`,
);
const formatted = formatter.format(placeholders);
const parts = formatted.split(/<\d+>/g);
return interleaveArrays(parts, values);
}
26 changes: 0 additions & 26 deletions packages/desktop-client/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,3 @@ i18n
transSupportBasicHtmlNodes: false,
},
});

const interleaveArrays = (
...arrays: (React.ReactNode | string)[][]
) =>
Array.from(
{
length: Math.max(...arrays.map(array => array.length)),
},
(_, i) => arrays.map(array => array[i]),
).flat();

export const i18nObjectList = (values: (React.ReactNode | string)[], lng: string, opt = {}) => {
const formatter = new Intl.ListFormat(lng, {
style: 'long',
type: 'conjunction',
...opt,
});

const placeholders = Array.from(
{ length: values.length },
(_, i) => `<${i}>`,
);
const formatted = formatter.format(placeholders);
const parts = formatted.split(/<\d+>/g);
return interleaveArrays(parts, values);
};

0 comments on commit 9023686

Please sign in to comment.