Skip to content

Commit

Permalink
refactor: merge Dialog.toggleOpen and Dialog.toggleOpenSingle into Di…
Browse files Browse the repository at this point in the history
…alog.toggle
  • Loading branch information
MartinCupela committed Sep 16, 2024
1 parent f649140 commit 7a67c03
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
3 changes: 1 addition & 2 deletions docusaurus/docs/React/guides/dialog-management.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ Dialog can be controlled via `Dialog` object retrieved using `useDialog()` hook.

- `dialog.open()` - opens the dialog
- `dialog.close()` - closes the dialog
- `dialog.toggleOpen()` - flips the dialog open state and does not close any other dialog that could be open
- `dialog.toggleOpenSingle()` - flips the open state and does close any other dialog that could be open
- `dialog.toggle()` - toggles the dialog open state. Accepts boolean argument `closeAll`. If enabled closes any other dialog that would be open.
- `dialog.remove()` - removes the dialog object reference from the state (primarily for cleanup purposes)

Every `Dialog` object carries its own `id` and `isOpen` flag.
Expand Down
32 changes: 15 additions & 17 deletions src/components/Dialog/DialogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export type Dialog = {
isOpen: boolean | undefined;
open: (zIndex?: number) => void;
remove: () => void;
toggle: () => void;
toggleSingle: () => void;
toggle: (closeAll?: boolean) => void;
};

export type DialogManagerOptions = {
Expand All @@ -26,6 +25,16 @@ export type DialogManagerState = {
dialogsById: Dialogs;
};

/**
* Keeps a map of Dialog objects.
* Dialog can be controlled via `Dialog` object retrieved using `useDialog()` hook.
* The hook returns an object with the following API:
*
* - `dialog.open()` - opens the dialog
* - `dialog.close()` - closes the dialog
* - `dialog.toggle()` - toggles the dialog open state. Accepts boolean argument closeAll. If enabled closes any other dialog that would be open.
* - `dialog.remove()` - removes the dialog object reference from the state (primarily for cleanup purposes)
*/
export class DialogManager {
id: string;
state = new StateStore<DialogManagerState>({
Expand Down Expand Up @@ -58,11 +67,8 @@ export class DialogManager {
remove: () => {
this.remove(id);
},
toggle: () => {
this.toggleOpen({ id });
},
toggleSingle: () => {
this.toggleOpenSingle({ id });
toggle: (closeAll = false) => {
this.toggle({ id }, closeAll);
},
};
this.state.next((current) => ({
Expand Down Expand Up @@ -98,19 +104,11 @@ export class DialogManager {
Object.values(this.state.getLatestValue().dialogsById).forEach((dialog) => dialog.close());
}

toggleOpen(params: GetOrCreateDialogParams) {
if (this.state.getLatestValue().dialogsById[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params);
}
}

toggleOpenSingle(params: GetOrCreateDialogParams) {
toggle(params: GetOrCreateDialogParams, closeAll = false) {
if (this.state.getLatestValue().dialogsById[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params, true);
this.open(params, closeAll);
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/components/Dialog/__tests__/DialogsManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('DialogManager', () => {
open: expect.any(Function),
remove: expect.any(Function),
toggle: expect.any(Function),
toggleSingle: expect.any(Function),
});
expect(Object.keys(dialogManager.state.getLatestValue().dialogsById)).toHaveLength(1);
expect(dialogManager.openDialogCount).toBe(0);
Expand Down Expand Up @@ -54,7 +53,6 @@ describe('DialogManager', () => {
open: expect.any(Function),
remove: expect.any(Function),
toggle: expect.any(Function),
toggleSingle: expect.any(Function),
});
expect(dialogManager.openDialogCount).toBe(1);
});
Expand Down Expand Up @@ -109,9 +107,9 @@ describe('DialogManager', () => {
const dialogManager = new DialogManager();
dialogManager.open({ id: 'xxx' });
dialogManager.open({ id: 'yyy' });
dialogManager.toggleOpen({ id: dialogId });
dialogManager.toggle({ id: dialogId });
expect(dialogManager.openDialogCount).toBe(3);
dialogManager.toggleOpen({ id: dialogId });
dialogManager.toggle({ id: dialogId });
expect(dialogManager.openDialogCount).toBe(2);
});

Expand All @@ -120,10 +118,10 @@ describe('DialogManager', () => {

dialogManager.open({ id: 'xxx' });
dialogManager.open({ id: 'yyy' });
dialogManager.toggleOpenSingle({ id: dialogId });
dialogManager.toggle({ id: dialogId }, true);
expect(dialogManager.openDialogCount).toBe(1);

dialogManager.toggleOpenSingle({ id: dialogId });
dialogManager.toggle({ id: dialogId }, true);
expect(dialogManager.openDialogCount).toBe(0);
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/MessageActions/MessageActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const MessageActions = <
<MessageActionsWrapper
customWrapperClass={customWrapperClass}
inline={inline}
toggleOpen={dialog?.toggleSingle}
toggleOpen={dialog?.toggle}
>
<DialogAnchor
id={dialogId}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Reactions/ReactionSelectorWithButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ReactionSelectorWithButton = <
aria-label={t('aria/Open Reaction Selector')}
className={`str-chat__message-${theme}__actions__action str-chat__message-${theme}__actions__action--reactions str-chat__message-reactions-button`}
data-testid='message-reaction-action'
onClick={dialog.toggleSingle}
onClick={() => dialog?.toggle()}
ref={buttonRef}
>
<ReactionIcon className='str-chat__message-action-icon' />
Expand Down

0 comments on commit 7a67c03

Please sign in to comment.