Skip to content

Commit 3e2fa61

Browse files
committed
Fix 2 JS/TS bugs
1 parent 685fea9 commit 3e2fa61

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

frontend/components/left-panel/nav-sections.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,17 @@ export function NavSections() {
157157
onOpenChange={(open) =>
158158
setEditingSectionId(open ? editingSectionId : null)
159159
}
160-
section={findSectionById(sections, editingSectionId)}
160+
section={findSectionByIdGuaranteed(sections, editingSectionId)}
161161
onSubmit={handleSectionEdit}
162162
/>
163163
)}
164-
{movingSectionId && (
164+
{sections && sections.length > 0 && movingSectionId && (
165165
<MoveSectionDialog
166166
isOpened={!!movingSectionId}
167167
onOpenChange={(open) =>
168168
setMovingSectionId(open ? movingSectionId : null)
169169
}
170-
section={findSectionById(sections, movingSectionId)}
170+
section={findSectionByIdGuaranteed(sections, movingSectionId)}
171171
onSubmit={handleSectionMove}
172172
/>
173173
)}
@@ -378,24 +378,34 @@ function SectionsSkeleton() {
378378
);
379379
}
380380

381-
function findSectionById(
381+
function findSectionByIdGuaranteed(
382382
sections: SectionResponse[] | undefined,
383383
id: string
384384
): SectionResponse {
385-
if (!sections) throw new Error("Sections are undefined");
385+
if (!sections || sections.length === 0)
386+
throw new Error("Sections are undefined or empty");
386387

388+
const found = findSectionByIdRecursive(sections, id);
389+
if (!found) throw new Error("Section with id " + id + " not found");
390+
return found;
391+
}
392+
393+
function findSectionByIdRecursive(
394+
sections: SectionResponse[],
395+
id: string
396+
): SectionResponse | null {
387397
for (const section of sections) {
388398
if (section.id === id) {
389399
return section;
390400
}
391401

392402
if (section.subsections) {
393-
const found = findSectionById(section.subsections, id);
403+
const found = findSectionByIdRecursive(section.subsections, id);
394404
if (found) {
395405
return found;
396406
}
397407
}
398408
}
399409

400-
throw new Error("Section with id " + id + " not found");
410+
return null;
401411
}

frontend/components/left-panel/section-move-dialog-form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const SectionMoveDialogForm = forwardRef<
4747
const { sections, rootSectionId } = useSections({ asTree: false });
4848
const handleFinish = (e: React.FormEvent) => {
4949
e.preventDefault();
50-
if (!parentId || !index) {
50+
if (!parentId || index === undefined) {
5151
throw new Error("Parent ID or index is required");
5252
}
5353
onSubmit({

0 commit comments

Comments
 (0)