# All files / src/components/features/snippet-display SnippetCard.tsx

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

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

     import { useState, useMemo, useEffect } from 'react'
    import { Card } from '@/components/ui/card'
    import { Snippet, Namespace } from '@/lib/types'
    import { strings, appConfig } from '@/lib/config'
    import { getAllNamespaces, moveSnippetToNamespace } from '@/lib/db'
    import { toast } from 'sonner'
    import { SnippetCardHeader } from './SnippetCardHeader'
    import { SnippetCodePreview } from './SnippetCodePreview'
    import { SnippetCardActions } from './SnippetCardActions'

    interface SnippetCardProps {
      snippet: Snippet
      onEdit: (snippet: Snippet) => void
      onDelete: (id: string) => void
      onCopy: (code: string) => void
      onView: (snippet: Snippet) => void
      onMove?: () => void
      selectionMode?: boolean
      isSelected?: boolean
      onToggleSelect?: (id: string) => void
    }

    export function SnippetCard({
      snippet,
      onEdit,
      onDelete,
      onCopy,
      onView,
      onMove,
      selectionMode = false,
      isSelected = false,
      onToggleSelect
    }: SnippetCardProps) {
      const [isCopied, setIsCopied] = useState(false)
      const [namespaces, setNamespaces] = useState<Namespace[]>([])
      const [isMoving, setIsMoving] = useState(false)

      useEffect(() => {
        loadNamespaces()
      }, [])

      const loadNamespaces = async () => {
        try {
          const loadedNamespaces = await getAllNamespaces()
          setNamespaces(loadedNamespaces)
        } catch {
          console.error('Failed to load namespaces')
        }
      }

      const snippetData = useMemo(() => {
        const code = snippet?.code || ''
        const description = snippet?.description || ''
        const maxLength = appConfig.codePreviewMaxLength
        const isTruncated = code.length > maxLength
        const displayCode = isTruncated ? code.slice(0, maxLength) + '...' : code

        return {
          description,
          displayCode,
          fullCode: code,
          isTruncated,
          hasPreview: snippet?.hasPreview || false
        }
      }, [snippet])

      const handleCopy = (e: React.MouseEvent) => {
        e.stopPropagation()
        onCopy(snippetData.fullCode)
        setIsCopied(true)
        setTimeout(() => setIsCopied(false), appConfig.copiedTimeout)
      }

      const handleEdit = (e: React.MouseEvent) => {
        e.stopPropagation()
        onEdit(snippet)
      }

      const handleDelete = (e: React.MouseEvent) => {
        e.stopPropagation()
        onDelete(snippet.id)
      }

      const handleView = (e: React.MouseEvent) => {
        e.stopPropagation()
        if (selectionMode) {
          handleToggleSelect()
        } else {
          onView(snippet)
        }
      }

      const handleToggleSelect = () => {
        if (onToggleSelect) {
          onToggleSelect(snippet.id)
        }
      }

      const handleMoveToNamespace = async (targetNamespaceId: string) => {
        if (snippet.namespaceId === targetNamespaceId) {
          toast.info('Snippet is already in this namespace')
          return
        }

        setIsMoving(true)
        try {
          await moveSnippetToNamespace(snippet.id, targetNamespaceId)
          const targetNamespace = namespaces.find(n => n.id === targetNamespaceId)
          toast.success(`Moved to ${targetNamespace?.name || 'namespace'}`)
          if (onMove) {
            onMove()
          }
        } catch (error) {
          console.error('Failed to move snippet:', error)
          toast.error('Failed to move snippet')
        } finally {
          setIsMoving(false)
        }
      }

      if (!snippet) {
        return (
          <Card className="p-6">
            <p className="text-muted-foreground">{strings.snippetCard.errorMessage}</p>
          </Card>
        )
      }

      const availableNamespaces = namespaces.filter(n => n.id !== snippet.namespaceId)

      return (
        <Card
          className={`group overflow-hidden hover:border-accent/50 transition-all cursor-pointer ${
            isSelected ? 'border-accent ring-2 ring-accent/20' : ''
          }`}
          onClick={handleView}
          data-testid={`snippet-card-${snippet.id}`}
          role="article"
        >
          <div className="p-6 space-y-4">
            <SnippetCardHeader
              snippet={snippet}
              description={snippetData.description}
              selectionMode={selectionMode}
              isSelected={isSelected}
              onToggleSelect={handleToggleSelect}
            />

            <SnippetCodePreview
              displayCode={snippetData.displayCode}
              isTruncated={snippetData.isTruncated}
            />

            {!selectionMode && (
              <SnippetCardActions
                isCopied={isCopied}
                isMoving={isMoving}
                availableNamespaces={availableNamespaces}
                onView={handleView}
                onCopy={handleCopy}
                onEdit={handleEdit}
                onDelete={handleDelete}
                onMoveToNamespace={handleMoveToNamespace}
              />
            )}
          </div>
        </Card>
      )
    }