Skip to content

Commit da2e0cc

Browse files
authored
feat: Keep state in dataroom view between document and dataroom view #783 (#968)
1 parent 9a06aa1 commit da2e0cc

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

components/datarooms/folders/view-tree.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,34 @@ type DataroomFolderWithDocuments = DataroomFolder & {
3030
}[];
3131
};
3232

33+
type FolderPath = Set<string> | null
34+
35+
function findFolderPath(folder: DataroomFolderWithDocuments, folderId: string, currentPath: Set<string> = new Set<string>()):FolderPath {
36+
if (folder.id === folderId) {
37+
return currentPath.add(folder.id);
38+
}
39+
40+
for (const child of folder.childFolders) {
41+
const path = findFolderPath(child, folderId, currentPath.add(folder.id));
42+
if (path) {
43+
return path;
44+
}
45+
}
46+
47+
return null;
48+
}
49+
3350
const FolderComponent = memo(
3451
({
3552
folder,
3653
folderId,
3754
setFolderId,
55+
folderPath
3856
}: {
3957
folder: DataroomFolderWithDocuments;
4058
folderId: string | null;
4159
setFolderId: React.Dispatch<React.SetStateAction<string | null>>;
60+
folderPath: Set<string> | null;
4261
}) => {
4362
const router = useRouter();
4463

@@ -64,13 +83,14 @@ const FolderComponent = memo(
6483
folder={childFolder}
6584
folderId={folderId}
6685
setFolderId={setFolderId}
86+
folderPath={folderPath}
6787
/>
6888
)),
6989
[folder.childFolders, folderId, setFolderId],
7090
);
7191

7292
const isActive = folder.id === folderId;
73-
const isChildActive = folder.childFolders.some(
93+
const isChildActive = folderPath?.has(folder.id) || folder.childFolders.some(
7494
(childFolder) => childFolder.id === folderId,
7595
);
7696

@@ -116,6 +136,21 @@ const SidebarFolders = ({
116136
return [];
117137
}, [folders, documents]);
118138

139+
const folderPath = useMemo(() => {
140+
if (!folderId) {
141+
return null;
142+
}
143+
144+
for (let i = 0; i < nestedFolders.length; i++) {
145+
const path = findFolderPath(nestedFolders[i], folderId);
146+
if (path) {
147+
return path;
148+
}
149+
}
150+
151+
return null;
152+
}, [folders, documents, folderId])
153+
119154
return (
120155
<FileTree>
121156
{nestedFolders.map((folder) => (
@@ -124,6 +159,7 @@ const SidebarFolders = ({
124159
folder={folder}
125160
folderId={folderId}
126161
setFolderId={setFolderId}
162+
folderPath={folderPath}
127163
/>
128164
))}
129165
</FileTree>

components/view/DataroomViewer.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export default function DataroomViewer({
7676
setDocumentData,
7777
setDataroomVerified,
7878
isPreview,
79+
folderId,
80+
setFolderId
7981
}: {
8082
brand: Partial<DataroomBrand>;
8183
viewId?: string;
@@ -89,8 +91,9 @@ export default function DataroomViewer({
8991
setDocumentData: React.Dispatch<React.SetStateAction<TDocumentData | null>>;
9092
setDataroomVerified: React.Dispatch<React.SetStateAction<boolean>>;
9193
isPreview?: boolean;
94+
folderId: string | null;
95+
setFolderId: React.Dispatch<React.SetStateAction<string | null>>;
9296
}) {
93-
const [folderId, setFolderId] = useState<string | null>(null);
9497
const { documents, folders } = dataroom as {
9598
documents: DataroomDocument[];
9699
folders: DataroomFolder[];

components/view/dataroom/dataroom-view.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default function DataroomView({
110110
const plausible = usePlausible();
111111
const analytics = useAnalytics();
112112
const router = useRouter();
113+
const [folderId, setFolderId] = useState<string | null>(null);
113114

114115
const didMount = useRef<boolean>(false);
115116
const [submitted, setSubmitted] = useState<boolean>(false);
@@ -442,6 +443,8 @@ export default function DataroomView({
442443
setDocumentData={setDocumentData}
443444
setViewType={setViewType}
444445
setDataroomVerified={setDataroomVerified}
446+
folderId={folderId}
447+
setFolderId={setFolderId}
445448
/>
446449
</div>
447450
);

0 commit comments

Comments
 (0)