File size: 2,867 Bytes
9a6f968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useMemo, useState } from 'react'

const GROUPS = [
  { key: 'visual_apresentacao', label: 'Formato para Apresentação', canExport: false },
  { key: 'excel', label: 'Formato Excel', canExport: true, exportMode: 'excel' },
  { key: 'excel_sab', label: 'Formato Excel (estilo SAB)', canExport: true, exportMode: 'excel_sab' },
]

function fallbackCopy(text) {
  const area = document.createElement('textarea')
  area.value = text
  area.setAttribute('readonly', '')
  area.style.position = 'fixed'
  area.style.opacity = '0'
  area.style.pointerEvents = 'none'
  document.body.appendChild(area)
  area.focus()
  area.select()
  try {
    document.execCommand('copy')
  } finally {
    area.remove()
  }
}

async function copyText(text) {
  const value = String(text || '').trim()
  if (!value) return false
  try {
    if (navigator?.clipboard?.writeText) {
      await navigator.clipboard.writeText(value)
      return true
    }
  } catch {
    // fallback below
  }
  try {
    fallbackCopy(value)
    return true
  } catch {
    return false
  }
}

export default function EquationFormatsPanel({ equacoes, onDownload, disabled = false }) {
  const [lastCopied, setLastCopied] = useState('')
  const groups = useMemo(() => GROUPS, [])

  async function onCopy(value, key) {
    const ok = await copyText(value)
    setLastCopied(ok ? key : '')
    if (ok) {
      window.setTimeout(() => setLastCopied(''), 1800)
    }
  }

  return (
    <div className="equation-formats-wrap">
      {groups.map((group) => {
        const value = String(equacoes?.[group.key] || '').trim()
        const hasValue = Boolean(value)
        return (
          <div key={group.key} className="equation-format-card">
            <div className="equation-format-head">
              <h5>{group.label}</h5>
              {group.canExport ? (
                <div className="equation-format-actions">
                  <button
                    type="button"
                    className="btn-download-subtle"
                    onClick={() => void onCopy(value, group.key)}
                    disabled={disabled || !hasValue}
                  >
                    {lastCopied === group.key ? 'Copiado' : 'Copiar fórmula'}
                  </button>
                  <button
                    type="button"
                    className="btn-download-subtle"
                    onClick={() => onDownload?.(group.exportMode)}
                    disabled={disabled || !hasValue || typeof onDownload !== 'function'}
                  >
                    Baixar Excel
                  </button>
                </div>
              ) : null}
            </div>
            <div className="equation-box equation-box-plain">
              {hasValue ? value : 'Equação indisponível.'}
            </div>
          </div>
        )
      })}
    </div>
  )
}