import { $, component$, useOnWindow, useSignal } from "@builder.io/qwik"; import { useLocalStorage } from "~/hooks/useLocalStorage"; import type { Checklist, Section } from '~/types/PSC'; import Icon from '~/components/core/icon'; import styles from './psc.module.css'; export default component$((props: { sections: Section[] }) => { // Create signals to store the number of items done or ignored per section const completions = useSignal(); const done = useSignal(); // Get the IDs of completed and ignore items from local storage const [checked] = useLocalStorage('PSC_PROGRESS', {}); const [ignored] = useLocalStorage('PSC_IGNORED', {}); /** * Get the percentage of completion for a given section * using completion data from local storage, and disregarding ignored items */ const getPercentCompletion = $((section: Section): number => { const id = (item: Checklist) => item.point.toLowerCase().replace(/ /g, '-') const total = section.checklist.filter((item) => !ignored.value[id(item)]).length; const done = section.checklist.filter((item) => checked.value[id(item)]).length; return Math.round((done / total) * 100); }); // On load (in browser only), calculate and set completion data for sections useOnWindow('load', $(async () => { // Percentage completion, per section completions.value = await Promise.all(props.sections.map(section => getPercentCompletion(section), )); // Count of completed items, per section done.value = await Promise.all(props.sections.map(section => section.checklist.filter( (item) => checked.value[item.point.toLowerCase().replace(/ /g, '-')], ).length )); })); return ( ); });