From 64b59dd227203e968bd95f482fd8ac2830dc3bd1 Mon Sep 17 00:00:00 2001 From: RanXom Date: Sat, 13 Jun 2026 17:41:18 +0530 Subject: add timer, control buttons and tasks ui compenents --- desktop/src/components/PixelTimer.tsx | 136 ++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 desktop/src/components/PixelTimer.tsx (limited to 'desktop/src/components/PixelTimer.tsx') diff --git a/desktop/src/components/PixelTimer.tsx b/desktop/src/components/PixelTimer.tsx new file mode 100644 index 0000000..b3310d1 --- /dev/null +++ b/desktop/src/components/PixelTimer.tsx @@ -0,0 +1,136 @@ +import { useState, useRef, useEffect } from "react"; +import type { TimerState } from "../lib/commands"; + +interface PixelTimerProps { + timer: TimerState; + onSetDuration: (mins: number) => void; +} + +const ASCII_MAP: Record = { + "0": [" ██████ ", "██ ██", "██ ██", "██ ██", " ██████ "], + "1": [" ██ ", " ███ ", " ██ ", " ██ ", " ██████ "], + "2": [" ██████ ", "██ ██", " ███ ", " ███ ", "████████"], + "3": [" ██████ ", " ██", " █████ ", " ██", " ██████ "], + "4": ["██ ██", "██ ██", "████████", " ██", " ██"], + "5": ["████████", "██ ", "███████ ", " ██", "███████ "], + "6": [" ██████ ", "██ ", "███████ ", "██ ██", " ██████ "], + "7": ["████████", " ██", " ██ ", " ██ ", " ██ "], + "8": [" ██████ ", "██ ██", " ██████ ", "██ ██", " ██████ "], + "9": [" ██████ ", "██ ██", " ███████", " ██", " ██████ "], + ":": [" ", " █ ", " ", " █ ", " "], +}; + +function formatMonolithicAscii(timeStr: string): string { + const lines = ["", "", "", "", ""]; + for (let i = 0; i < timeStr.length; i++) { + const char = timeStr[i]; + const art = ASCII_MAP[char] || ASCII_MAP[":"]; + for (let j = 0; j < 5; j++) { + lines[j] += art[j]; + if (i < timeStr.length - 1) { + lines[j] += " "; + } + } + } + return lines.join("\n"); +} + +function formatTimeStr(secs: number): string { + const m = Math.floor(secs / 60); + const s = secs % 60; + return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`; +} + +export default function PixelTimer({ timer, onSetDuration }: PixelTimerProps) { + const [isEditing, setIsEditing] = useState(false); + const [editValue, setEditValue] = useState(""); + const inputRef = useRef(null); + + useEffect(() => { + if (isEditing && inputRef.current) { + inputRef.current.focus(); + } + }, [isEditing]); + + const timeStr = formatTimeStr(timer.time_remaining_secs); + const asciiArt = formatMonolithicAscii(timeStr); + + const [currentAscii, setCurrentAscii] = useState(asciiArt); + const [prevAscii, setPrevAscii] = useState(""); + const [isFading, setIsFading] = useState(false); + + useEffect(() => { + if (asciiArt !== currentAscii) { + setPrevAscii(currentAscii); + setCurrentAscii(asciiArt); + setIsFading(true); + const timeout = setTimeout(() => setIsFading(false), 300); + return () => clearTimeout(timeout); + } + }, [asciiArt, currentAscii]); + + function handleStartEdit() { + setEditValue(String(Math.floor(timer.time_remaining_secs / 60))); + setIsEditing(true); + } + + function handleSubmit() { + const mins = parseInt(editValue, 10); + if (!isNaN(mins) && mins >= 0) { + onSetDuration(mins); + } + setIsEditing(false); + } + + function handleKeyDown(e: React.KeyboardEvent) { + if (e.key === "Enter") { + handleSubmit(); + } else if (e.key === "Escape") { + setIsEditing(false); + } + } + + if (isEditing) { + return ( +
+ setEditValue(e.target.value)} + onBlur={handleSubmit} + onKeyDown={handleKeyDown} + min="0" + max="999" + aria-label="Set minutes" + /> + minutes +
+ ); + } + + return ( +
{ + if (e.key === "Enter" || e.key === " ") handleStartEdit(); + }} + > +
+ {isFading && ( +
+            {prevAscii}
+          
+ )} +
+          {currentAscii}
+        
+
+
+ ); +} -- cgit v1.2.3