From 6931d0d69bb4a45cc5415ae8e7a532a975f4169c Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 21 Jan 2024 20:38:24 -0600 Subject: [PATCH] Auto switch phases --- islands/Routine.tsx | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/islands/Routine.tsx b/islands/Routine.tsx index e3985b1..a716d5c 100644 --- a/islands/Routine.tsx +++ b/islands/Routine.tsx @@ -1,6 +1,8 @@ import { DailyPhase, DailyPhaseModel, Task, toPhase } from '@homeman/models.ts' -import { useSignal } from '@preact/signals' +import { signal, useSignal } from '@preact/signals' import { excitement } from '@homeman/common.ts' +import { IS_BROWSER } from '$fresh/runtime.ts' +import { useEffect } from 'preact/hooks' export interface Props { tasks: Task[] @@ -19,12 +21,27 @@ const phaseEmoji: Record = { } type TaskGroups = Record +let lastPhaseSelection = new Date() export function Routine( props: Props, ) { const tasks = useSignal(props.tasks) const currentPhase = useSignal(toPhase()) + + useEffect(() => { + if (IS_BROWSER) { + setInterval(() => { + const delta = (new Date()).getTime() - lastPhaseSelection.getTime() + const nowPhase = toPhase() + if (nowPhase != currentPhase.value && delta >= 5000) { + currentPhase.value = nowPhase + lastPhaseSelection = new Date() + } + }, 2_000) + } + }, []) + const taskGroups: TaskGroups = { Morning: [], Midday: [], @@ -40,7 +57,7 @@ export function Routine(