File size: 3,166 Bytes
dde4052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// <reference types="vite/client" />

import { useState } from 'react'
import { confirmToast, notify } from '../lib/notify'

const toastOptions = [
  'message',
  'success',
  'info',
  'warning',
  'error',
  'custom',
] as const

type ToastType = (typeof toastOptions)[number]

const customRenderer = () => (
  <div className="dev-custom-toast">
    <p className="dev-custom-title">Sonner 自定义通知</p>
    <p className="dev-custom-body">
      这是一个通过 `notify.custom` 渲染的测试 Toast
    </p>
  </div>
)

export function DevToastController() {
  const [type, setType] = useState<ToastType>('success')
  const [message, setMessage] = useState('来自 Dev 控制器的测试通知')
  const [duration, setDuration] = useState(2200)

  if (!import.meta.env.DEV) {
    return null
  }

  const triggerToast = async () => {
    switch (type) {
      case 'message':
        notify.message(message, { duration })
        break
      case 'success':
        notify.success(message, { duration })
        break
      case 'info':
        notify.info(message, { duration })
        break
      case 'warning':
        notify.warning(message, { duration })
        break
      case 'error':
        notify.error(message, { duration })
        break
      case 'custom':
        notify.custom(() => customRenderer(), { duration })
        break
    }
  }

  const triggerConfirm = async () => {
    const confirmed = await confirmToast(message, {
      okText: '继续',
      cancelText: '取消',
    })
    if (confirmed) {
      notify.success('确认按钮已点击', { duration: 2000 })
    } else {
      notify.message('已取消确认逻辑', { duration: 2000 })
    }
  }

  return (
    <div className="dev-toast-controller">
      <div className="dev-toast-controller__header">
        <span>Dev Sonner 控制器</span>
        <small>仅在 dev 模式可见</small>
      </div>
      <div className="dev-toast-controller__content">
        <label className="dev-toast-controller__label">
          类型
          <select
            value={type}
            onChange={(event) => setType(event.target.value as ToastType)}
          >
            {toastOptions.map((option) => (
              <option key={option} value={option}>
                {option}
              </option>
            ))}
          </select>
        </label>
        <label className="dev-toast-controller__label">
          文案
          <input
            value={message}
            onChange={(event) => setMessage(event.target.value)}
            placeholder="输入通知/确认文案"
          />
        </label>
        <label className="dev-toast-controller__label">
          持续(ms)
          <input
            type="number"
            min={600}
            value={duration}
            onChange={(event) => setDuration(Number(event.target.value))}
          />
        </label>
        <div className="dev-toast-controller__actions">
          <button onClick={triggerToast}>触发通知</button>
          <button onClick={triggerConfirm}>触发确认</button>
        </div>
      </div>
    </div>
  )
}

export default DevToastController