summaryrefslogtreecommitdiff
path: root/frontend/src/components/document/DocumentSettings.tsx
blob: b575b3deecb28327ffc1355aa3a029d056c89818 (plain) (blame)
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
import { useState, useCallback } from 'react'
import { upsertUserSetting } from '../../services/directiveApi'

interface DocumentSettingsProps {
  isOpen: boolean
  onClose: () => void
  enabled: boolean
  onToggle: (enabled: boolean) => void
}

export default function DocumentSettings({
  isOpen,
  onClose,
  enabled,
  onToggle,
}: DocumentSettingsProps) {
  const [saving, setSaving] = useState(false)

  const handleToggle = useCallback(async () => {
    const newValue = !enabled
    setSaving(true)
    try {
      // Update localStorage immediately for instant UI response
      localStorage.setItem('document_ui_enabled', JSON.stringify(newValue))
      onToggle(newValue)

      // Persist to backend
      await upsertUserSetting('document_ui_enabled', newValue)
    } catch (err) {
      console.error('Failed to save document UI setting:', err)
      // Revert on failure
      localStorage.setItem('document_ui_enabled', JSON.stringify(!newValue))
      onToggle(!newValue)
    } finally {
      setSaving(false)
    }
  }, [enabled, onToggle])

  if (!isOpen) return null

  return (
    <div className="modal-overlay" onClick={onClose}>
      <div className="config-modal" onClick={(e) => e.stopPropagation()}>
        <div className="modal-header">
          <h2>Document UI Settings</h2>
          <button className="close-btn" onClick={onClose}>{'\u00D7'}</button>
        </div>

        <div className="modal-content">
          <div className="config-option">
            <label className="config-label" style={{ cursor: 'pointer' }}>
              <input
                type="checkbox"
                checked={enabled}
                onChange={handleToggle}
                disabled={saving}
                className="config-checkbox"
              />
              <span className="config-text">
                Enable Document UI (Experimental)
              </span>
            </label>
            <div className="config-description">
              Replace the directive management interface with an interactive
              document editor. This is a proof of concept.
            </div>
          </div>
        </div>

        <div className="modal-footer">
          <button className="modal-btn" onClick={onClose}>Close</button>
        </div>
      </div>
    </div>
  )
}