File size: 2,780 Bytes
4e039c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* 共用小元件 */
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 (
    <label className="block">
      {label && <span className="block text-xs text-muted mb-1">{label}</span>}
      {children}
      {hint && <span className="block text-[11px] text-muted mt-1 leading-snug">{hint}</span>}
    </label>
  )
}

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 (
    <button
      type="button"
      className={`inline-flex items-center justify-center gap-1.5 px-3 py-2 rounded-lg text-sm transition disabled:opacity-40 disabled:cursor-not-allowed ${styles[variant]} ${className}`}
      {...props}
    >
      {children}
    </button>
  )
}

export function Section({ title, defaultOpen = true, right, children }) {
  const [open, setOpen] = useState(defaultOpen)
  return (
    <div className="border-b border-edge">
      <div className="flex items-center gap-1 px-3 py-2">
        <button type="button" onClick={() => setOpen(!open)} className="flex items-center gap-1 flex-1 text-left text-xs font-semibold uppercase tracking-wide text-muted">
          {open ? <ChevronDown className="w-3.5 h-3.5" /> : <ChevronRight className="w-3.5 h-3.5" />}
          {title}
        </button>
        {right}
      </div>
      {open && <div className="px-3 pb-3 space-y-3">{children}</div>}
    </div>
  )
}

/** 觸發瀏覽器下載 */
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, '-')