import { type KeyboardEvent, type FocusEvent, useEffect, useRef, useState } from "react";
// ─── Badge ────────────────────────────────────────────────────────────────────
export function Badge({ children, color }: { children: React.ReactNode; color: string }) {
return (
{children}
);
}
// ─── IconBtn ──────────────────────────────────────────────────────────────────
export function IconBtn({ title, onClick, children, danger, active, disabled }: {
title: string; onClick: () => void; children: React.ReactNode;
danger?: boolean; active?: boolean; disabled?: boolean;
}) {
return (
);
}
// ─── HelpTip ──────────────────────────────────────────────────────────────────
export function HelpTip({ text }: { text: string }) {
const [show, setShow] = useState(false);
return (
{show && (
)}
);
}
// ─── Toast ────────────────────────────────────────────────────────────────────
export function Toast({ message, onDone }: { message: string; onDone: () => void }) {
useEffect(() => {
const t = setTimeout(onDone, 2500);
return () => clearTimeout(t);
}, [onDone]);
return (
{message}
);
}
// ─── HighlightedText ──────────────────────────────────────────────────────────
export function HighlightedText({ text, highlight }: { text: string; highlight: string }) {
if (!highlight.trim()) return <>{text}>;
const escaped = highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const parts = text.split(new RegExp(`(${escaped})`, 'gi'));
return (
<>
{parts.map((part, i) =>
part.toLowerCase() === highlight.toLowerCase() ? (
{part}
) : (
part
)
)}
>
);
}
// ─── TimePartsInput ───────────────────────────────────────────────────────────
import { pad2, clampParts, toHmsString, parseHmsParts, parseLooseTimeToSeconds, parsePastedSegments } from "../utils";
export function TimePartsInput({
value,
onChange,
maxSeconds,
allowEmpty = false,
}: {
value: string;
onChange: (next: string) => void;
maxSeconds?: number;
allowEmpty?: boolean;
}) {
const parsed = parseHmsParts(value);
const [hLocal, setHLocal] = useState(pad2(parsed.h));
const [mLocal, setMLocal] = useState(pad2(parsed.m));
const [sLocal, setSLocal] = useState(pad2(parsed.s));
const hRef = useRef(null);
const mRef = useRef(null);
const sRef = useRef(null);
const vals = useRef({ h: pad2(parsed.h), m: pad2(parsed.m), s: pad2(parsed.s) });
useEffect(() => {
const active = document.activeElement;
if (active === hRef.current || active === mRef.current || active === sRef.current) return;
const p = parseHmsParts(value);
const h = pad2(p.h), m = pad2(p.m), s = pad2(p.s);
setHLocal(h); setMLocal(m); setSLocal(s);
vals.current = { h, m, s };
}, [value]);
const commit = () => {
const hNum = parseInt(vals.current.h, 10) || 0;
const mNum = Math.min(59, parseInt(vals.current.m, 10) || 0);
const sNum = Math.min(59, parseInt(vals.current.s, 10) || 0);
const clamped = clampParts(hNum, mNum, sNum, maxSeconds);
onChange(toHmsString(clamped.h, clamped.m, clamped.s));
};
const handleInput = (field: "h" | "m" | "s", raw: string) => {
const digits = raw.replace(/\D/g, "").slice(0, 2);
vals.current[field] = digits;
if (field === "h") setHLocal(digits);
if (field === "m") setMLocal(digits);
if (field === "s") setSLocal(digits);
if (digits.length >= 2) {
if (field === "h") { setTimeout(() => { mRef.current?.focus(); mRef.current?.select(); }, 0); }
if (field === "m") { setTimeout(() => { sRef.current?.focus(); sRef.current?.select(); }, 0); }
commit();
}
};
const handleBlur = (field: "h" | "m" | "s") => {
const num = parseInt(vals.current[field], 10) || 0;
const clamped = field === "h" ? num : Math.min(59, num);
const padded = pad2(clamped);
vals.current[field] = padded;
if (field === "h") setHLocal(padded);
if (field === "m") setMLocal(padded);
if (field === "s") setSLocal(padded);
commit();
};
const handleKeyDown = (field: "h" | "m" | "s", e: KeyboardEvent) => {
if (e.key === "Backspace" && e.currentTarget.value === "") {
if (field === "m") { hRef.current?.focus(); hRef.current?.select(); }
if (field === "s") { mRef.current?.focus(); mRef.current?.select(); }
}
};
const handleFocus = (e: FocusEvent) => { e.target.select(); };
const fieldCls = "w-12 rounded-lg border border-slate-200 px-1 py-2 text-sm font-mono text-center focus:border-violet-400 focus:outline-none focus:ring-2 focus:ring-violet-100";
return (
handleInput("h", e.target.value)}
onBlur={() => handleBlur("h")}
onKeyDown={(e) => handleKeyDown("h", e)}
onFocus={handleFocus}
className={fieldCls} />
:
handleInput("m", e.target.value)}
onBlur={() => handleBlur("m")}
onKeyDown={(e) => handleKeyDown("m", e)}
onFocus={handleFocus}
className={fieldCls} />
:
handleInput("s", e.target.value)}
onBlur={() => handleBlur("s")}
onKeyDown={(e) => handleKeyDown("s", e)}
onFocus={handleFocus}
className={fieldCls} />
{allowEmpty && value.trim() && (
)}
);
}