Skip to content

Commit

Permalink
refactor: apply suggestions about declaring state selector
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Arnautov <[email protected]>
  • Loading branch information
MartinCupela and arnautov-anton committed Sep 11, 2024
1 parent 4f120a4 commit a344533
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/components/Dialog/hooks/useDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@ export const useDialog = ({ id }: GetOrCreateDialogParams) => {
export const useDialogIsOpen = (id: string) => {
const { dialogManager } = useDialogManager();
const dialogIsOpenSelector = useCallback(
({ dialogsById }: DialogManagerState) => [!!dialogsById[id]?.isOpen],
({ dialogsById }: DialogManagerState) => [!!dialogsById[id]?.isOpen] as const,
[id],
);
return useStateStore<DialogManagerState, boolean[]>(dialogManager.state, dialogIsOpenSelector)[0];
return useStateStore(dialogManager.state, dialogIsOpenSelector)[0];
};

const openedDialogCountSelector = (nextValue: DialogManagerState) => [
Object.values(nextValue.dialogsById).reduce((count, dialog) => {
if (dialog.isOpen) return count + 1;
return count;
}, 0),
];
const openedDialogCountSelector = (nextValue: DialogManagerState) =>
[
Object.values(nextValue.dialogsById).reduce((count, dialog) => {
if (dialog.isOpen) return count + 1;
return count;
}, 0),
] as const;

export const useOpenedDialogCount = () => {
const { dialogManager } = useDialogManager();
return useStateStore<DialogManagerState, number[]>(
dialogManager.state,
openedDialogCountSelector,
)[0];
return useStateStore(dialogManager.state, openedDialogCountSelector)[0];
};

0 comments on commit a344533

Please sign in to comment.