Skip to content

Commit 7e7c9ac

Browse files
committed
♻️(frontend) replace useModal hook
useModal hook does not use useCallback for its methods that creates useless rerenders.
1 parent d5d2cfa commit 7e7c9ac

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/frontend/apps/impress/src/features/docs/doc-search/components/DocSearchModal.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Modal, ModalProps, ModalSize } from '@openfun/cunningham-react';
1+
import { Modal, ModalSize } from '@openfun/cunningham-react';
22
import Image from 'next/image';
33
import { useRouter } from 'next/navigation';
44
import { useMemo, useState } from 'react';
@@ -19,7 +19,10 @@ import EmptySearchIcon from '../assets/illustration-docs-empty.png';
1919

2020
import { DocSearchItem } from './DocSearchItem';
2121

22-
type DocSearchModalProps = ModalProps & {};
22+
type DocSearchModalProps = {
23+
onClose: () => void;
24+
isOpen: boolean;
25+
};
2326

2427
export const DocSearchModal = ({ ...modalProps }: DocSearchModalProps) => {
2528
const { t } = useTranslation();

src/frontend/apps/impress/src/features/left-panel/components/LefPanelTargetFilters.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const LeftPanelTargetFilters = () => {
4444
const onSelectQuery = (query: DocDefaultFilter) => {
4545
const params = new URLSearchParams(searchParams);
4646
params.set('target', query);
47-
router.replace(`${pathname}?${params.toString()}`);
47+
router.push(`${pathname}?${params.toString()}`);
4848
togglePanel();
4949
};
5050

src/frontend/apps/impress/src/features/left-panel/components/LeftPanel.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { usePathname } from 'next/navigation';
2-
import { useCallback, useEffect } from 'react';
2+
import { useEffect } from 'react';
33
import { createGlobalStyle, css } from 'styled-components';
44

55
import { Box, SeparatedSection } from '@/components';
@@ -30,13 +30,9 @@ export const LeftPanel = () => {
3030
const colors = theme.colorsTokens();
3131
const spacings = theme.spacingsTokens();
3232

33-
const toggle = useCallback(() => {
34-
togglePanel(false);
35-
}, [togglePanel]);
36-
3733
useEffect(() => {
38-
toggle();
39-
}, [pathname, toggle]);
34+
togglePanel(false);
35+
}, [pathname, togglePanel]);
4036

4137
return (
4238
<>

src/frontend/apps/impress/src/features/left-panel/components/LeftPanelHeader.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Button, ModalSize, useModal } from '@openfun/cunningham-react';
1+
import { Button } from '@openfun/cunningham-react';
22
import { t } from 'i18next';
33
import { useRouter } from 'next/navigation';
4-
import { PropsWithChildren } from 'react';
4+
import { PropsWithChildren, useCallback, useState } from 'react';
55

66
import { Box, Icon, SeparatedSection } from '@/components';
77
import { useCreateDoc } from '@/docs/doc-management';
@@ -13,17 +13,24 @@ import { useLeftPanelStore } from '../stores';
1313

1414
export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
1515
const router = useRouter();
16-
const searchModal = useModal();
1716
const { authenticated } = useAuth();
18-
useCmdK(() => {
17+
const [isSearchModalOpen, setIsSearchModalOpen] = useState(false);
18+
19+
const openSearchModal = useCallback(() => {
1920
const isEditorToolbarOpen =
2021
document.getElementsByClassName('bn-formatting-toolbar').length > 0;
2122
if (isEditorToolbarOpen) {
2223
return;
2324
}
2425

25-
searchModal.open();
26-
});
26+
setIsSearchModalOpen(true);
27+
}, []);
28+
29+
const closeSearchModal = useCallback(() => {
30+
setIsSearchModalOpen(false);
31+
}, []);
32+
33+
useCmdK(openSearchModal);
2734
const { togglePanel } = useLeftPanelStore();
2835

2936
const { mutate: createDoc } = useCreateDoc({
@@ -64,7 +71,7 @@ export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
6471
/>
6572
{authenticated && (
6673
<Button
67-
onClick={searchModal.open}
74+
onClick={openSearchModal}
6875
size="medium"
6976
color="tertiary-text"
7077
icon={
@@ -80,8 +87,8 @@ export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
8087
</SeparatedSection>
8188
{children}
8289
</Box>
83-
{searchModal.isOpen && (
84-
<DocSearchModal {...searchModal} size={ModalSize.LARGE} />
90+
{isSearchModalOpen && (
91+
<DocSearchModal onClose={closeSearchModal} isOpen={isSearchModalOpen} />
8592
)}
8693
</>
8794
);

0 commit comments

Comments
 (0)