# All files / src/hooks useSnippetManager.ts

90.65% Statements 223/246
70.27% Branches 26/37
100% Functions 2/2
90.65% Lines 223/246

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
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    2471x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    21x
    21x
    21x
    20x
    20x
    21x
    1x
    1x
    1x
    21x
    21x
    21x
    99x
    99x
    99x
    23x
    22x
    22x
    99x
    99x
    99x
    3x
    3x

    3x
    3x
    3x
    3x
    3x
    2x
    2x
    2x
    3x
    1x
    1x
    1x
    99x
    99x
    99x

    99x
    99x
    99x
    1x
    1x
    1x
    1x

    99x
    99x
    99x
    1x
    1x
    99x
    99x
    99x
    1x
    99x
    99x
    99x

    99x
    99x
    99x
    2x
    99x
    99x
    99x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    99x
    99x
    99x
    2x
    99x
    99x
    99x
    1x
    99x
    99x
    99x
    1x

    1x
    1x
    1x
    99x
    99x
    99x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x

    99x
    99x
    99x
    2x
    1x
    1x
    99x
    99x
    99x
    1x
    99x
    99x
    99x
    1x
    1x
    1x
    99x
    99x
    99x
    1x
    1x
    1x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
     import { useEffect, useCallback } from 'react'
    import { useAppDispatch, useAppSelector } from '@/store/hooks'
    import { Snippet, SnippetTemplate } from '@/lib/types'
    import { toast } from 'sonner'
    import { strings } from '@/lib/config'
    import { seedDatabase, syncTemplatesFromJSON } from '@/lib/db'
    import {
      fetchSnippetsByNamespace,
      createSnippet,
      updateSnippet,
      deleteSnippet,
      toggleSelectionMode,
      toggleSnippetSelection,
      selectAllSnippets as selectAllSnippetsAction,
      clearSelection,
      bulkMoveSnippets,
    } from '@/store/slices/snippetsSlice'
    import {
      fetchNamespaces,
      setSelectedNamespace,
    } from '@/store/slices/namespacesSlice'
    import {
      openDialog,
      closeDialog,
      openViewer,
      closeViewer,
      setSearchQuery,
    } from '@/store/slices/uiSlice'
    import {
      selectFilteredSnippets,
      selectSnippetsLoading,
      selectSelectionMode,
      selectSelectedIds,
      selectNamespaces,
      selectSelectedNamespaceId,
      selectDialogOpen,
      selectViewerOpen,
      selectEditingSnippet,
      selectViewingSnippet,
      selectSearchQuery,
      selectSnippets,
    } from '@/store/selectors'

    export function useSnippetManager(templates: SnippetTemplate[]) {
      const dispatch = useAppDispatch()

      const snippets = useAppSelector(selectSnippets)
      const filteredSnippets = useAppSelector(selectFilteredSnippets)
      const loading = useAppSelector(selectSnippetsLoading)
      const selectionMode = useAppSelector(selectSelectionMode)
      const selectedIds = useAppSelector(selectSelectedIds)
      const namespaces = useAppSelector(selectNamespaces)
      const selectedNamespaceId = useAppSelector(selectSelectedNamespaceId)
      const dialogOpen = useAppSelector(selectDialogOpen)
      const viewerOpen = useAppSelector(selectViewerOpen)
      const editingSnippet = useAppSelector(selectEditingSnippet)
      const viewingSnippet = useAppSelector(selectViewingSnippet)
      const searchQuery = useAppSelector(selectSearchQuery)

      useEffect(() => {
        const loadData = async () => {
          try {
            await seedDatabase()
            await syncTemplatesFromJSON(templates)
            await dispatch(fetchNamespaces()).unwrap()
          } catch (error) {
            console.error('Failed to load data:', error)
            toast.error('Failed to load data')
          }
        }

        loadData()
      }, [dispatch, templates])

      useEffect(() => {
        if (selectedNamespaceId) {
          dispatch(fetchSnippetsByNamespace(selectedNamespaceId))
        }
      }, [dispatch, selectedNamespaceId])

      const handleSaveSnippet = useCallback(async (snippetData: Omit<Snippet, 'id' | 'createdAt' | 'updatedAt'>) => {
        try {
          if (editingSnippet?.id) {
            await dispatch(updateSnippet({ ...editingSnippet, ...snippetData })).unwrap()
            toast.success(strings.toast.snippetUpdated)
          } else {
            await dispatch(createSnippet({
              ...snippetData,
              namespaceId: selectedNamespaceId || undefined,
            })).unwrap()
            toast.success(strings.toast.snippetCreated)
          }
          dispatch(closeDialog())
        } catch (error) {
          console.error('Failed to save snippet:', error)
          toast.error('Failed to save snippet')
        }
      }, [dispatch, editingSnippet, selectedNamespaceId])

      const handleEditSnippet = useCallback((snippet: Snippet) => {
        dispatch(openDialog(snippet))
      }, [dispatch])

      const handleDeleteSnippet = useCallback(async (id: string) => {
        try {
          await dispatch(deleteSnippet(id)).unwrap()
          toast.success(strings.toast.snippetDeleted)
        } catch (error) {
          console.error('Failed to delete snippet:', error)
          toast.error('Failed to delete snippet')
        }
      }, [dispatch])

      const handleCopyCode = useCallback((code: string) => {
        navigator.clipboard.writeText(code)
        toast.success(strings.toast.codeCopied)
      }, [])

      const handleViewSnippet = useCallback((snippet: Snippet) => {
        dispatch(openViewer(snippet))
      }, [dispatch])

      const handleMoveSnippet = useCallback(async () => {
        if (selectedNamespaceId) {
          dispatch(fetchSnippetsByNamespace(selectedNamespaceId))
        }
      }, [dispatch, selectedNamespaceId])

      const handleCreateNew = useCallback(() => {
        dispatch(openDialog(null))
      }, [dispatch])

      const handleCreateFromTemplate = useCallback((templateId: string) => {
        const template = templates.find((t) => t.id === templateId)
        if (!template) return

        const templateSnippet = {
          id: '',
          title: template.title,
          description: template.description,
          language: template.language,
          code: template.code,
          category: template.category,
          hasPreview: template.hasPreview,
          functionName: template.functionName,
          inputParameters: template.inputParameters,
          createdAt: 0,
          updatedAt: 0,
        } as Snippet

        dispatch(openDialog(templateSnippet))
      }, [dispatch, templates])

      const handleToggleSelectionMode = useCallback(() => {
        dispatch(toggleSelectionMode())
      }, [dispatch])

      const handleToggleSnippetSelection = useCallback((snippetId: string) => {
        dispatch(toggleSnippetSelection(snippetId))
      }, [dispatch])

      const handleSelectAll = useCallback(() => {
        if (selectedIds.length === filteredSnippets.length) {
          dispatch(clearSelection())
        } else {
          dispatch(selectAllSnippetsAction())
        }
      }, [dispatch, filteredSnippets.length, selectedIds.length])

      const handleBulkMove = useCallback(async (targetNamespaceId: string) => {
        if (selectedIds.length === 0) {
          toast.error('No snippets selected')
          return
        }

        try {
          await dispatch(bulkMoveSnippets({
            snippetIds: Array.from(selectedIds),
            targetNamespaceId
          })).unwrap()

          const targetNamespace = namespaces.find(n => n.id === targetNamespaceId)
          toast.success(`Moved ${selectedIds.length} snippet${selectedIds.length > 1 ? 's' : ''} to ${targetNamespace?.name || 'namespace'}`)

          if (selectedNamespaceId) {
            dispatch(fetchSnippetsByNamespace(selectedNamespaceId))
          }
        } catch (error) {
          console.error('Failed to bulk move snippets:', error)
          toast.error('Failed to move snippets')
        }
      }, [dispatch, selectedIds, namespaces, selectedNamespaceId])

      const handleNamespaceChange = useCallback((namespaceId: string | null) => {
        if (namespaceId) {
          dispatch(setSelectedNamespace(namespaceId))
        }
      }, [dispatch])

      const handleSearchChange = useCallback((query: string) => {
        dispatch(setSearchQuery(query))
      }, [dispatch])

      const handleDialogClose = useCallback((open: boolean) => {
        if (!open) {
          dispatch(closeDialog())
        }
      }, [dispatch])

      const handleViewerClose = useCallback((open: boolean) => {
        if (!open) {
          dispatch(closeViewer())
        }
      }, [dispatch])

      return {
        snippets,
        filteredSnippets,
        loading,
        selectionMode,
        selectedIds,
        namespaces,
        selectedNamespaceId,
        dialogOpen,
        viewerOpen,
        editingSnippet,
        viewingSnippet,
        searchQuery,
        handleSaveSnippet,
        handleEditSnippet,
        handleDeleteSnippet,
        handleCopyCode,
        handleViewSnippet,
        handleMoveSnippet,
        handleCreateNew,
        handleCreateFromTemplate,
        handleToggleSelectionMode,
        handleToggleSnippetSelection,
        handleSelectAll,
        handleBulkMove,
        handleNamespaceChange,
        handleSearchChange,
        handleDialogClose,
        handleViewerClose,
      }
    }