All files / web/src/app/create/worksheets/components ActionsSidebar.tsx

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

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { css } from '@styled/css'
import { stack } from '@styled/patterns'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import { UploadWorksheetModal } from '@/components/worksheets/UploadWorksheetModal'
import { useTheme } from '@/contexts/ThemeContext'
import { extractConfigFields } from '../utils/extractConfigFields'
import { GenerateButton } from './GenerateButton'
import { ShareModal } from './ShareModal'
import { useWorksheetConfig } from './WorksheetConfigContext'

interface ActionsSidebarProps {
  onGenerate: () => Promise<void>
  status: 'idle' | 'generating' | 'success' | 'error'
}

export function ActionsSidebar({ onGenerate, status }: ActionsSidebarProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'
  const router = useRouter()
  const [isUploadModalOpen, setIsUploadModalOpen] = useState(false)
  const [isShareModalOpen, setIsShareModalOpen] = useState(false)
  const [isGeneratingShare, setIsGeneratingShare] = useState(false)
  const [justCopied, setJustCopied] = useState(false)
  const { formState } = useWorksheetConfig()

  // Upload complete handler
  const handleUploadComplete = (attemptId: string) => {
    router.push(`/worksheets/attempts/${attemptId}`)
  }

  // Quick share - copy link to clipboard without showing modal
  const handleQuickShare = async () => {
    setIsGeneratingShare(true)
    setJustCopied(false)

    try {
      const response = await fetch('/api/worksheets/share', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          worksheetType: 'addition',
          config: extractConfigFields(formState),
        }),
      })

      if (!response.ok) {
        throw new Error('Failed to create share link')
      }

      const data = await response.json()
      await navigator.clipboard.writeText(data.url)

      setJustCopied(true)
      setTimeout(() => setJustCopied(false), 2000)
    } catch (err) {
      console.error('Failed to create share link:', err)
      // TODO: Show error toast
    } finally {
      setIsGeneratingShare(false)
    }
  }

  return (
    <>
      <div
        data-component="actions-sidebar"
        className={css({
          h: 'full',
          bg: isDark ? 'gray.800' : 'white',
          rounded: 'xl',
          shadow: 'card',
          p: '4',
          overflow: 'auto',
        })}
      >
        <div className={stack({ gap: '4' })}>
          {/* Generate Button */}
          <GenerateButton
            status={status === 'success' ? 'idle' : status}
            onGenerate={onGenerate}
            isDark={isDark}
          />

          {/* Share Button with Copy Shortcut */}
          <div
            data-component="share-button-group"
            className={css({
              w: 'full',
              display: 'flex',
              rounded: 'xl',
              overflow: 'hidden',
              shadow: 'md',
              border: '2px solid',
              borderColor: 'blue.700',
            })}
          >
            {/* Main share button - opens QR modal */}
            <button
              data-action="show-qr-code"
              onClick={() => setIsShareModalOpen(true)}
              className={css({
                flex: '1',
                px: '6',
                py: '4',
                bg: 'blue.600',
                color: 'white',
                fontSize: 'md',
                fontWeight: 'bold',
                transition: 'all 0.2s',
                cursor: 'pointer',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                gap: '2',
                border: 'none',
                outline: 'none',
                _hover: {
                  bg: 'blue.700',
                  transform: 'translateY(-1px)',
                },
                _active: {
                  transform: 'translateY(0)',
                },
              })}
            >
              <span className={css({ fontSize: 'xl' })}>📱</span>
              <span>Share</span>
            </button>

            {/* Copy shortcut */}
            <button
              data-action="copy-share-link"
              onClick={handleQuickShare}
              disabled={isGeneratingShare}
              title={justCopied ? 'Copied!' : 'Copy share link'}
              className={css({
                px: '4',
                py: '4',
                bg: justCopied ? 'green.600' : 'blue.600',
                color: 'white',
                fontSize: 'xl',
                cursor: isGeneratingShare ? 'wait' : 'pointer',
                border: 'none',
                borderLeft: '1px solid',
                borderColor: justCopied ? 'green.700' : 'blue.700',
                outline: 'none',
                transition: 'all 0.2s',
                opacity: isGeneratingShare ? '0.6' : '1',
                _hover:
                  isGeneratingShare || justCopied
                    ? {}
                    : {
                        bg: 'blue.700',
                        transform: 'translateY(-1px)',
                      },
                _active:
                  isGeneratingShare || justCopied
                    ? {}
                    : {
                        transform: 'translateY(0)',
                      },
              })}
            >
              {isGeneratingShare ? '⏳' : justCopied ? '✓' : '📋'}
            </button>
          </div>

          {/* Upload Worksheet Button */}
          <button
            data-action="upload-worksheet"
            onClick={() => setIsUploadModalOpen(true)}
            className={css({
              w: 'full',
              px: '6',
              py: '4',
              bg: 'purple.600',
              color: 'white',
              fontSize: 'md',
              fontWeight: 'bold',
              rounded: 'xl',
              shadow: 'md',
              transition: 'all 0.2s',
              cursor: 'pointer',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              gap: '2',
              border: '2px solid',
              borderColor: 'purple.700',
              _hover: {
                bg: 'purple.700',
                borderColor: 'purple.800',
                transform: 'translateY(-1px)',
                shadow: 'lg',
              },
              _active: {
                transform: 'translateY(0)',
              },
            })}
          >
            <span className={css({ fontSize: 'xl' })}>⬆️</span>
            <span>Upload</span>
          </button>
        </div>
      </div>

      {/* Share Modal */}
      <ShareModal
        isOpen={isShareModalOpen}
        onClose={() => setIsShareModalOpen(false)}
        worksheetType="addition"
        config={formState}
        isDark={isDark}
      />

      {/* Upload Worksheet Modal */}
      <UploadWorksheetModal
        isOpen={isUploadModalOpen}
        onClose={() => setIsUploadModalOpen(false)}
        onUploadComplete={handleUploadComplete}
      />
    </>
  )
}