MCPcopy Create free account
hub / github.com/code100x/cms / findContentById

Function findContentById

src/lib/find-content-by-id.ts:3–34  ·  view source on GitHub ↗
(
  contents: FullCourseContent[],
  ids: number[],
)

Source from the content-addressed store, hash-verified

1import { ChildCourseContent, FullCourseContent } from '@/db/course';
2
3export default function findContentById(
4 contents: FullCourseContent[],
5 ids: number[],
6):
7 | {
8 folder: true;
9 value: ChildCourseContent[];
10 }
11 | {
12 folder: false;
13 value: ChildCourseContent;
14 }
15 | null {
16 if (ids.length === 0) return { folder: true, value: contents };
17
18 const currentId = ids[0];
19 const remainingIds = ids.slice(1);
20
21 const foundContent = contents.find((content) => content.id === currentId);
22
23 if (!foundContent) {
24 return null;
25 } else if (remainingIds.length === 0) {
26 if (foundContent.type === 'folder') {
27 return { folder: true, value: foundContent.children ?? [] }; // [Object]
28 }
29
30 return { folder: false, value: foundContent };
31 }
32
33 return findContentById(foundContent?.children || [], remainingIds);
34}

Callers 4

CourseFunction · 0.85
CourseFunction · 0.85
UpdateCourseContentFunction · 0.85
UpdateCourseContentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected