Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

section-progress.ts

23 lines924 Bytes

1import { markSectionDone } from "@/components/guide/guide-store";
2import { matchesAppAction, subscribeToAppActions } from "@/lib/app-actions";
3import type { AppActionEvent } from "@/lib/app-actions";
4​
5import { GUIDE_SECTIONS } from "./content";
6import type { GuideSectionContent } from "./content";
7​
8/** The sections whose hands-on step `event` completes. */
9export function sectionsDoneBy(
10 event: AppActionEvent,
11 sections: Pick<GuideSectionContent, "id" | "doneWhen">[] = GUIDE_SECTIONS,
12): string[] {
13 return sections
14 .filter((section) => section.doneWhen?.some((matcher) => matchesAppAction(matcher, event)))
15 .map((section) => section.id);
17​
18/** Marks sections done as the reader does their steps in the app. Returns a stop function. */
19export function trackSectionProgress(): () => void {
20 return subscribeToAppActions((event) => {
21 for (const id of sectionsDoneBy(event)) markSectionDone(id);
22 });