/* 共用小元件 */
import { useState } from 'react'
import { ChevronDown, ChevronRight } from 'lucide-react'
export const inputCls =
'w-full bg-field border border-edge px-3 py-2 rounded-lg text-sm focus:outline-none focus:border-purple-500 placeholder:text-muted'
export function Field({ label, hint, children }) {
return (
)
}
export function Btn({ children, variant = 'ghost', className = '', ...props }) {
const styles = {
primary: 'bg-purple-600 hover:bg-purple-700 text-white',
ghost: 'bg-field hover:bg-edge',
danger: 'bg-red-600/90 hover:bg-red-600 text-white',
subtle: 'hover:bg-field',
}
return (
)
}
export function Section({ title, defaultOpen = true, right, children }) {
const [open, setOpen] = useState(defaultOpen)
return (
{right}
{open &&
{children}
}
)
}
/** 觸發瀏覽器下載 */
export function download(filename, text, type = 'application/json') {
const blob = new Blob([text], { type })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
a.remove()
setTimeout(() => URL.revokeObjectURL(url), 1000)
}
/** 開檔案選擇器讀 JSON */
export function pickJsonFile() {
return new Promise((resolve, reject) => {
const input = document.createElement('input')
input.type = 'file'
input.accept = 'application/json,.json'
input.onchange = async () => {
const file = input.files?.[0]
if (!file) return reject(new Error('沒有選擇檔案'))
try { resolve(JSON.parse(await file.text())) } catch { reject(new Error('不是合法的 JSON 檔')) }
}
input.click()
})
}
export const stamp = () => new Date().toISOString().slice(0, 16).replace(/[:T]/g, '-')