pl-api-ogama / src /components /Shared.tsx
mozolla's picture
Upload 30 files
3305c7f verified
Raw History Blame Contribute Delete
8.96 kB
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>
);
}