From 5b6f685db77d05bf2dd18b5bf1a9d86f3af45c19 Mon Sep 17 00:00:00 2001 From: teabyte Date: Mon, 8 Jan 2024 20:46:19 +0300 Subject: [PATCH] cache values --- components/Nav.tsx | 2 +- routes/group/[slug].tsx | 4 ++-- routes/index.tsx | 8 ++++---- utils/course-cache.ts | 2 +- utils/course.ts | 21 ++++++++++++--------- utils/types.ts | 10 +++++----- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/components/Nav.tsx b/components/Nav.tsx index df55402..87a2227 100644 --- a/components/Nav.tsx +++ b/components/Nav.tsx @@ -27,7 +27,7 @@ export default function NavBar() { - + ); diff --git a/routes/group/[slug].tsx b/routes/group/[slug].tsx index 0c349e3..91f61d5 100644 --- a/routes/group/[slug].tsx +++ b/routes/group/[slug].tsx @@ -19,12 +19,12 @@ export const handler: Handlers = { : parseInt(ctx.params.slug); if (typeof toFind === "number") { - foundCourseGroup = cache.merged.find((c) => { + foundCourseGroup = cache.courses.find((c) => { return "courses" in c && c.courses.length > 0 && c.order === toFind; }); } else { - foundCourseGroup = cache.merged.find((c) => { + foundCourseGroup = cache.courses.find((c) => { return "courses" in c && c.courses.length > 0 && c.label === toFind; }); diff --git a/routes/index.tsx b/routes/index.tsx index ea00df5..e7563f9 100644 --- a/routes/index.tsx +++ b/routes/index.tsx @@ -11,7 +11,7 @@ import IconChevronDown from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/chev populateCache(); -export const handler: Handlers<{ merged: (Course | CourseGroup)[] }> = { +export const handler: Handlers<{ courses: (Course | CourseGroup)[] }> = { async GET(_req, ctx) { const courses = await getCourses(cache); return ctx.render(courses); @@ -19,9 +19,9 @@ export const handler: Handlers<{ merged: (Course | CourseGroup)[] }> = { }; export default function BlogIndexPage( - props: PageProps<{ merged: (Course | CourseGroup)[] }>, + props: PageProps<{ courses: (Course | CourseGroup)[] }>, ) { - const { merged } = props.data; + const { courses } = props.data; return ( <> @@ -42,7 +42,7 @@ export default function BlogIndexPage(

المحتوى

- {merged.map((course, index) => { + {courses.map((course, index) => { // Group of courses if ("courses" in course) { return ( diff --git a/utils/course-cache.ts b/utils/course-cache.ts index 0b32599..d9e1f62 100644 --- a/utils/course-cache.ts +++ b/utils/course-cache.ts @@ -1,6 +1,6 @@ import { Course, CourseGroup } from "./types.ts"; import { getCourses } from "./course.ts"; -export const cache: { merged: (Course | CourseGroup)[] } = { merged: [] }; +export const cache: { courses: (Course | CourseGroup)[] } = { courses: [] }; export async function populateCache(): Promise { await getCourses(cache); diff --git a/utils/course.ts b/utils/course.ts index 8eb4f43..902a4a1 100644 --- a/utils/course.ts +++ b/utils/course.ts @@ -44,11 +44,11 @@ export async function getJson( } export async function getCourses( - cache: { merged: (Course | CourseGroup)[] } = { merged: [] }, + cache: { courses: (Course | CourseGroup)[] } = { courses: [] }, ): Promise< - { merged: (Course | CourseGroup)[] } + { courses: (Course | CourseGroup)[] } > { - if (cache.merged.length > 0) { + if (cache.courses.length > 0) { return cache; } @@ -56,6 +56,7 @@ export async function getCourses( const files = Deno.readDir("./courses"); const groups: CourseGroup[] = []; const nonGroups: Course[] = []; + for await (const file of files) { if (file.isDirectory) { const groupSlug = file.name; @@ -71,11 +72,13 @@ export async function getCourses( } } const groupOrder = await getGroupOrder(join("./courses", groupSlug)); - groups.push({ - courses: groupPromises, - order: groupOrder?.order, - label: groupOrder?.label, - }); + if (groupOrder) { + groups.push({ + courses: groupPromises, + order: groupOrder.order, + label: groupOrder.label, + }); + } } else if (file.name.endsWith(".md")) { const slug = file.name.replace(".md", ""); const course = await getCourse(slug); @@ -87,6 +90,6 @@ export async function getCourses( const merged: (Course | CourseGroup)[] = [...nonGroups, ...groups]; merged.sort((a, b) => (a.order || 999) - (b.order || 999)); - cache.merged = merged; + cache.courses = merged; return cache; } diff --git a/utils/types.ts b/utils/types.ts index ddeb9bf..0fdc8ab 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -8,12 +8,12 @@ export interface Course { export interface CourseGroup { courses: Course[]; - order?: number; - label?: string; + order: number; + label: string; } export interface CourseAttributes { - title?: string; - snippet?: string; - order?: number; + title: string; + snippet: string; + order: number; }