import { DailyPhase, DailyPhaseModel, Task, toPhase } from '@homeman/models.ts' import { useSignal } from '@preact/signals' import { excitement } from '@homeman/common.ts' export interface Props { tasks: Task[] } interface TaskWithIndex extends Task { index: number } const phaseEmoji: Record = { Morning: '🌄', Midday: '🌞', Evening: '🌆', Bedtime: '🛌', Night: '🌙', } type TaskGroups = Record export function Routine( props: Props, ) { const tasks = useSignal(props.tasks) const currentPhase = useSignal(toPhase()) const taskGroups: TaskGroups = { Morning: [], Midday: [], Evening: [], Bedtime: [], Night: [], } tasks.value.forEach((t, i) => taskGroups[t.phase].push({ ...t, index: i })) return ( <>
    {taskGroups[currentPhase.value].map(( task: TaskWithIndex, ) => { const { emoji, description, doneAt, index } = task return (
  • { tasks.value[index].doneAt = tasks.value[index].doneAt ? null : new Date() tasks.value = [...tasks.value] if (tasks.value[index].doneAt) { excitement() } await (await fetch('/api/tasks', { method: 'PUT', body: JSON.stringify({ ...task, done: !!tasks.value[index].doneAt, }), })).json() }} > {emoji ? `${emoji.trim()} ` : ''} {description.trim()}
  • ) })}
) }