Skip to content

Commit 9abf688

Browse files
AntoLCsampaccoud
authored andcommitted
🎨(frontend) reduce prop drilling thanks to doc store
We start to have a deep prop drilling with doc, time to use the doc store to reduce that. We still prefer to pass the doc as a prop to keep our component as "pure" as possible, but if the drilling is too deep, better to use the doc store.
1 parent aff3b43 commit 9abf688

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/stores/useDocStore.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Y from 'yjs';
44
import { create } from 'zustand';
55

66
import { providerUrl } from '@/core';
7-
import { Base64 } from '@/features/docs/doc-management';
7+
import { Base64, Doc } from '@/features/docs/doc-management';
88

99
import { blocksToYDoc } from '../utils';
1010

@@ -14,14 +14,17 @@ interface DocStore {
1414
}
1515

1616
export interface UseDocStore {
17+
currentDoc?: Doc;
1718
docsStore: {
1819
[storeId: string]: DocStore;
1920
};
2021
createProvider: (storeId: string, initialDoc: Base64) => HocuspocusProvider;
2122
setStore: (storeId: string, props: Partial<DocStore>) => void;
23+
setCurrentDoc: (doc: Doc | undefined) => void;
2224
}
2325

2426
export const useDocStore = create<UseDocStore>((set, get) => ({
27+
currentDoc: undefined,
2528
docsStore: {},
2629
createProvider: (storeId: string, initialDoc: Base64) => {
2730
const doc = new Y.Doc({
@@ -52,8 +55,9 @@ export const useDocStore = create<UseDocStore>((set, get) => ({
5255
return provider;
5356
},
5457
setStore: (storeId, props) => {
55-
set(({ docsStore }) => {
58+
set(({ docsStore }, ...store) => {
5659
return {
60+
...store,
5761
docsStore: {
5862
...docsStore,
5963
[storeId]: {
@@ -64,4 +68,7 @@ export const useDocStore = create<UseDocStore>((set, get) => ({
6468
};
6569
});
6670
},
71+
setCurrentDoc: (doc) => {
72+
set({ currentDoc: doc });
73+
},
6774
}));

src/frontend/apps/impress/src/pages/docs/[id]/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useEffect, useState } from 'react';
66
import { Box, Text } from '@/components';
77
import { TextErrors } from '@/components/TextErrors';
88
import { useAuthStore } from '@/core/auth';
9-
import { DocEditor } from '@/features/docs';
9+
import { DocEditor, useDocStore } from '@/features/docs';
1010
import { useDoc } from '@/features/docs/doc-management';
1111
import { MainLayout } from '@/layouts';
1212
import { NextPageWithLayout } from '@/types/next';
@@ -35,6 +35,7 @@ const DocPage = ({ id }: DocProps) => {
3535
const { login } = useAuthStore();
3636
const { data: docQuery, isError, error } = useDoc({ id });
3737
const [doc, setDoc] = useState(docQuery);
38+
const { setCurrentDoc } = useDocStore();
3839

3940
const navigate = useNavigate();
4041

@@ -52,7 +53,12 @@ const DocPage = ({ id }: DocProps) => {
5253
}
5354

5455
setDoc(docQuery);
55-
}, [docQuery]);
56+
setCurrentDoc(docQuery);
57+
58+
return () => {
59+
setCurrentDoc(undefined);
60+
};
61+
}, [docQuery, setCurrentDoc]);
5662

5763
if (isError && error) {
5864
if (error.status === 404) {

0 commit comments

Comments
 (0)