Spaces:
Sleeping
Sleeping
File size: 8,958 Bytes
3305c7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import { type KeyboardEvent, type FocusEvent, useEffect, useRef, useState } from "react";
// βββ Badge ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function Badge({ children, color }: { children: React.ReactNode; color: string }) {
return (
<span className={`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${color}`}>
{children}
</span>
);
}
// βββ IconBtn ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function IconBtn({ title, onClick, children, danger, active, disabled }: {
title: string; onClick: () => void; children: React.ReactNode;
danger?: boolean; active?: boolean; disabled?: boolean;
}) {
return (
<button
title={title}
onClick={onClick}
disabled={disabled}
className={`inline-flex items-center justify-center rounded-md p-1 transition-colors disabled:opacity-40 disabled:cursor-not-allowed ${
danger
? "text-red-400 hover:bg-red-50 hover:text-red-600"
: active
? "bg-violet-100 text-violet-700"
: "text-slate-400 hover:bg-slate-100 hover:text-slate-600"
}`}
>
{children}
</button>
);
}
// βββ HelpTip ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function HelpTip({ text }: { text: string }) {
const [show, setShow] = useState(false);
return (
<span className="relative inline-flex ml-1">
<button
type="button"
onMouseEnter={() => setShow(true)}
onMouseLeave={() => setShow(false)}
onFocus={() => setShow(true)}
onBlur={() => setShow(false)}
onClick={(e) => { e.preventDefault(); setShow((v) => !v); }}
className="inline-flex items-center justify-center h-4 w-4 rounded-full bg-slate-200 text-[9px] font-bold text-slate-500 hover:bg-violet-200 hover:text-violet-700 transition-colors cursor-help flex-shrink-0"
aria-label="Help"
>?</button>
{show && (
<div className="absolute z-50 bottom-full left-1/2 -translate-x-1/2 mb-1.5 w-64 rounded-lg bg-slate-800 px-3 py-2 text-[11px] leading-relaxed text-slate-100 shadow-xl pointer-events-none">
{text}
<div className="absolute left-1/2 -translate-x-1/2 top-full w-0 h-0 border-x-4 border-x-transparent border-t-4 border-t-slate-800" />
</div>
)}
</span>
);
}
// βββ Toast ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export function Toast({ message, onDone }: { message: string; onDone: () => void }) {
useEffect(() => {
const t = setTimeout(onDone, 2500);
return () => clearTimeout(t);
}, [onDone]);
return (
<div className="fixed bottom-6 left-1/2 -translate-x-1/2 z-[100] rounded-full bg-slate-800 px-5 py-2.5 text-xs font-semibold text-white shadow-xl">
{message}
</div>
);
}
// βββ 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() ? (
<mark key={i} className="rounded bg-yellow-200 px-1">{part}</mark>
) : (
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<HTMLInputElement>(null);
const mRef = useRef<HTMLInputElement>(null);
const sRef = useRef<HTMLInputElement>(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<HTMLInputElement>) => {
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<HTMLInputElement>) => { 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 (
<div className="flex items-center gap-1">
<input ref={hRef} type="text" inputMode="numeric" maxLength={2}
value={allowEmpty && !value.trim() ? "" : hLocal} placeholder="HH"
onChange={(e) => handleInput("h", e.target.value)}
onBlur={() => handleBlur("h")}
onKeyDown={(e) => handleKeyDown("h", e)}
onFocus={handleFocus}
className={fieldCls} />
<span className="text-slate-400 font-bold text-sm">:</span>
<input ref={mRef} type="text" inputMode="numeric" maxLength={2}
value={allowEmpty && !value.trim() ? "" : mLocal} placeholder="MM"
onChange={(e) => handleInput("m", e.target.value)}
onBlur={() => handleBlur("m")}
onKeyDown={(e) => handleKeyDown("m", e)}
onFocus={handleFocus}
className={fieldCls} />
<span className="text-slate-400 font-bold text-sm">:</span>
<input ref={sRef} type="text" inputMode="numeric" maxLength={2}
value={allowEmpty && !value.trim() ? "" : sLocal} placeholder="SS"
onChange={(e) => handleInput("s", e.target.value)}
onBlur={() => handleBlur("s")}
onKeyDown={(e) => handleKeyDown("s", e)}
onFocus={handleFocus}
className={fieldCls} />
{allowEmpty && value.trim() && (
<button type="button" onClick={() => onChange("")}
className="ml-1 text-xs text-slate-400 hover:text-slate-600">Clear</button>
)}
</div>
);
} |