Skip to content

Commit

Permalink
Fix 2 JS/TS bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
m-danya committed Jan 30, 2025
1 parent 685fea9 commit 3e2fa61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 17 additions & 7 deletions frontend/components/left-panel/nav-sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ export function NavSections() {
onOpenChange={(open) =>
setEditingSectionId(open ? editingSectionId : null)
}
section={findSectionById(sections, editingSectionId)}
section={findSectionByIdGuaranteed(sections, editingSectionId)}
onSubmit={handleSectionEdit}
/>
)}
{movingSectionId && (
{sections && sections.length > 0 && movingSectionId && (
<MoveSectionDialog
isOpened={!!movingSectionId}
onOpenChange={(open) =>
setMovingSectionId(open ? movingSectionId : null)
}
section={findSectionById(sections, movingSectionId)}
section={findSectionByIdGuaranteed(sections, movingSectionId)}
onSubmit={handleSectionMove}
/>
)}
Expand Down Expand Up @@ -378,24 +378,34 @@ function SectionsSkeleton() {
);
}

function findSectionById(
function findSectionByIdGuaranteed(
sections: SectionResponse[] | undefined,
id: string
): SectionResponse {
if (!sections) throw new Error("Sections are undefined");
if (!sections || sections.length === 0)
throw new Error("Sections are undefined or empty");

const found = findSectionByIdRecursive(sections, id);
if (!found) throw new Error("Section with id " + id + " not found");
return found;
}

function findSectionByIdRecursive(
sections: SectionResponse[],
id: string
): SectionResponse | null {
for (const section of sections) {
if (section.id === id) {
return section;
}

if (section.subsections) {
const found = findSectionById(section.subsections, id);
const found = findSectionByIdRecursive(section.subsections, id);
if (found) {
return found;
}
}
}

throw new Error("Section with id " + id + " not found");
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const SectionMoveDialogForm = forwardRef<
const { sections, rootSectionId } = useSections({ asTree: false });
const handleFinish = (e: React.FormEvent) => {
e.preventDefault();
if (!parentId || !index) {
if (!parentId || index === undefined) {
throw new Error("Parent ID or index is required");
}
onSubmit({
Expand Down

0 comments on commit 3e2fa61

Please sign in to comment.