// 轻量 UI 原件
import { ReactNode, useCallback, useEffect, useState } from 'react'
export function cx(...a: (string | false | null | undefined)[]) {
return a.filter(Boolean).join(' ')
}
export function Section({ id, kicker, title, desc, children }: { id: string; kicker: string; title: string; desc?: string; children: ReactNode }) {
return (
{kicker}
{title}
{desc &&
{desc}
}
{children}
)
}
export function Chip({ active, onClick, children, disabled, title }: { active?: boolean; onClick?: () => void; children: ReactNode; disabled?: boolean; title?: string }) {
return (
)
}
export function Switch({ on, onChange, label }: { on: boolean; onChange: (v: boolean) => void; label: ReactNode }) {
return (
)
}
let toastFn: ((s: string) => void) | null = null
export function toast(s: string) {
toastFn?.(s)
}
export function Toaster() {
const [msg, setMsg] = useState(null)
useEffect(() => {
let tm: number | undefined
toastFn = (s) => {
setMsg(s)
clearTimeout(tm)
tm = window.setTimeout(() => setMsg(null), 1800)
}
return () => {
toastFn = null
}
}, [])
if (!msg) return null
return (
{msg}
)
}
export function useCopy() {
return useCallback(async (s: string, what = '内容') => {
let ok = false
try {
await navigator.clipboard.writeText(s)
ok = true
} catch {
const ta = document.createElement('textarea')
ta.value = s
ta.style.position = 'fixed'
ta.style.opacity = '0'
document.body.appendChild(ta)
ta.select()
try {
ok = document.execCommand('copy')
} catch {
ok = false
}
ta.remove()
}
toast(ok ? `已复制${what}` : '复制失败,请手动选择文本复制')
}, [])
}
export function fmtTime(t: number) {
const m = Math.floor(t / 60)
const s = Math.floor(t % 60)
return `${m}:${String(s).padStart(2, '0')}`
}