All files / web/src/components/flowchart FlowchartTab.tsx

0% Statements 0/130
0% Branches 0/1
0% Functions 0/1
0% Lines 0/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
'use client'

import { useState, useCallback } from 'react'
import type { ExecutableFlowchart } from '@/lib/flowcharts/schema'
import { downloadFlowchartPDF } from '@/lib/flowcharts/pdf-export'
import { MermaidViewer } from './MermaidViewer'
import { css } from '../../../styled-system/css'
import { vstack, hstack } from '../../../styled-system/patterns'

interface FlowchartTabProps {
  /** The loaded flowchart to display */
  flowchart: ExecutableFlowchart
  /** Optional share URL for the flowchart */
  shareUrl?: string
}

/**
 * Tab content for viewing the flowchart diagram and downloading PDF.
 */
export function FlowchartTab({ flowchart, shareUrl }: FlowchartTabProps) {
  const [isExporting, setIsExporting] = useState(false)
  const [shareCopied, setShareCopied] = useState(false)

  const handleDownloadPDF = useCallback(async () => {
    setIsExporting(true)
    try {
      await downloadFlowchartPDF(flowchart.rawMermaid, {
        title: flowchart.definition.title,
        flowchartId: flowchart.definition.id,
      })
    } catch (err) {
      console.error('Failed to export PDF:', err)
    } finally {
      setIsExporting(false)
    }
  }, [flowchart])

  const handleShare = useCallback(async () => {
    if (!shareUrl) return
    try {
      await navigator.clipboard.writeText(shareUrl)
      setShareCopied(true)
      setTimeout(() => setShareCopied(false), 2000)
    } catch (err) {
      console.error('Failed to copy share URL:', err)
    }
  }, [shareUrl])

  return (
    <div data-component="flowchart-tab" className={vstack({ gap: '4', alignItems: 'stretch' })}>
      {/* Mermaid Diagram */}
      <div
        data-element="diagram-container"
        className={css({
          borderRadius: 'lg',
          border: '1px solid',
          borderColor: { base: 'gray.200', _dark: 'gray.700' },
          backgroundColor: { base: 'gray.50', _dark: 'gray.900' },
          overflow: 'auto',
          maxHeight: '400px',
          padding: '4',
        })}
      >
        <MermaidViewer mermaidContent={flowchart.rawMermaid} />
      </div>

      {/* Action buttons */}
      <div className={hstack({ gap: '3', justifyContent: 'center' })}>
        <button
          data-action="download-pdf"
          onClick={handleDownloadPDF}
          disabled={isExporting}
          className={css({
            paddingY: '2',
            paddingX: '4',
            fontSize: 'sm',
            fontWeight: 'medium',
            borderRadius: 'lg',
            border: 'none',
            cursor: isExporting ? 'wait' : 'pointer',
            transition: 'all 0.15s',
            backgroundColor: { base: 'blue.500', _dark: 'blue.600' },
            color: 'white',
            opacity: isExporting ? 0.7 : 1,
            _hover: {
              backgroundColor: { base: 'blue.600', _dark: 'blue.500' },
            },
            _disabled: {
              cursor: 'not-allowed',
            },
          })}
        >
          {isExporting ? 'Exporting...' : 'Download PDF'}
        </button>

        {shareUrl && (
          <button
            data-action="copy-link"
            onClick={handleShare}
            className={css({
              paddingY: '2',
              paddingX: '4',
              fontSize: 'sm',
              fontWeight: 'medium',
              borderRadius: 'lg',
              border: '1px solid',
              borderColor: shareCopied
                ? { base: 'green.400', _dark: 'green.500' }
                : { base: 'gray.300', _dark: 'gray.600' },
              cursor: 'pointer',
              transition: 'all 0.15s',
              backgroundColor: shareCopied
                ? { base: 'green.50', _dark: 'green.900/30' }
                : { base: 'white', _dark: 'gray.800' },
              color: shareCopied
                ? { base: 'green.700', _dark: 'green.300' }
                : { base: 'gray.700', _dark: 'gray.300' },
              _hover: {
                borderColor: { base: 'blue.400', _dark: 'blue.500' },
                color: { base: 'blue.600', _dark: 'blue.400' },
              },
            })}
          >
            {shareCopied ? 'Copied!' : 'Copy Link'}
          </button>
        )}
      </div>
    </div>
  )
}