# All files / src/components/features/namespace-manager NamespaceSelector.tsx

69.41% Statements 118/170
100% Branches 14/14
25% Functions 1/4
69.41% Lines 118/170

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
    1711x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    14x
    14x
    13x
    13x
    14x
    1x
    1x
    1x
    1x
    1x
    14x
    1x
    1x
    1x
    25x
    25x
    25x
    14x
    25x
    25x
    25x

    25x
    25x

    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    13x
    13x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x
    25x

    25x
    25x
    25x
    25x
    25x
     import { useState, useEffect, useCallback } from 'react'
    import {
      Select,
      SelectContent,
      SelectItem,
      SelectTrigger,
      SelectValue,
    } from '@/components/ui/select'
    import { Folder } from '@phosphor-icons/react'
    import { toast } from 'sonner'
    import { Namespace } from '@/lib/types'
    import {
      getAllNamespaces,
      createNamespace,
      deleteNamespace,
    } from '@/lib/db'
    import { CreateNamespaceDialog } from './CreateNamespaceDialog'
    import { DeleteNamespaceDialog } from './DeleteNamespaceDialog'

    interface NamespaceSelectorProps {
      selectedNamespaceId: string | null
      onNamespaceChange: (namespaceId: string) => void
    }

    export function NamespaceSelector({ selectedNamespaceId, onNamespaceChange }: NamespaceSelectorProps) {
      const [namespaces, setNamespaces] = useState<Namespace[]>([])
      const [newNamespaceName, setNewNamespaceName] = useState('')
      const [createDialogOpen, setCreateDialogOpen] = useState(false)
      const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
      const [namespaceToDelete, setNamespaceToDelete] = useState<Namespace | null>(null)
      const [loading, setLoading] = useState(false)

      const loadNamespaces = useCallback(async () => {
        try {
          const loadedNamespaces = await getAllNamespaces()
          setNamespaces(loadedNamespaces)

          if (!selectedNamespaceId && loadedNamespaces.length > 0) {
            const defaultNamespace = loadedNamespaces.find(n => n.isDefault)
            if (defaultNamespace) {
              onNamespaceChange(defaultNamespace.id)
            }
          }
        } catch (error) {
          console.error('Failed to load namespaces:', error)
          toast.error('Failed to load namespaces')
        }
      }, [onNamespaceChange, selectedNamespaceId])

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

      const handleCreateNamespace = async () => {
        if (!newNamespaceName.trim()) {
          toast.error('Please enter a namespace name')
          return
        }

        setLoading(true)
        try {
          const newNamespace: Namespace = {
            id: Date.now().toString(),
            name: newNamespaceName.trim(),
            createdAt: Date.now(),
            isDefault: false,
          }
          await createNamespace(newNamespace)
          setNamespaces(prev => [...prev, newNamespace])
          setNewNamespaceName('')
          setCreateDialogOpen(false)
          toast.success('Namespace created')
        } catch (error) {
          console.error('Failed to create namespace:', error)
          toast.error('Failed to create namespace')
        } finally {
          setLoading(false)
        }
      }

      const handleDeleteNamespace = async () => {
        if (!namespaceToDelete) return

        setLoading(true)
        try {
          await deleteNamespace(namespaceToDelete.id)
          setNamespaces(prev => prev.filter(n => n.id !== namespaceToDelete.id))

          if (selectedNamespaceId === namespaceToDelete.id) {
            const defaultNamespace = namespaces.find(n => n.isDefault)
            if (defaultNamespace) {
              onNamespaceChange(defaultNamespace.id)
            }
          }

          setDeleteDialogOpen(false)
          setNamespaceToDelete(null)
          toast.success('Namespace deleted, snippets moved to default')
        } catch (error) {
          console.error('Failed to delete namespace:', error)
          toast.error('Failed to delete namespace')
        } finally {
          setLoading(false)
        }
      }

      const selectedNamespace = namespaces.find(n => n.id === selectedNamespaceId)

      return (
        <div className="flex items-center gap-2">
          <div className="flex items-center gap-2 text-muted-foreground">
            <Folder weight="fill" className="h-4 w-4" aria-hidden="true" />
            <span className="text-sm font-medium">Namespace:</span>
          </div>

          <Select
            value={selectedNamespaceId || undefined}
            onValueChange={onNamespaceChange}
          >
            <SelectTrigger
              className="w-[200px]"
              data-testid="namespace-selector-trigger"
              aria-label="Select namespace"
            >
              <SelectValue placeholder={selectedNamespace?.name || 'Select namespace'} />
            </SelectTrigger>
            <SelectContent data-testid="namespace-selector-content">
              {namespaces.map(namespace => (
                <SelectItem
                  key={namespace.id}
                  value={namespace.id}
                  data-testid={`namespace-option-${namespace.id}`}
                >
                  <div className="flex items-center gap-2">
                    <span>{namespace.name}</span>
                    {namespace.isDefault && (
                      <span className="text-xs text-muted-foreground">(Default)</span>
                    )}
                  </div>
                </SelectItem>
              ))}
            </SelectContent>
          </Select>

          <CreateNamespaceDialog
            open={createDialogOpen}
            onOpenChange={setCreateDialogOpen}
            namespaceName={newNamespaceName}
            onNamespaceNameChange={setNewNamespaceName}
            onCreateNamespace={handleCreateNamespace}
            loading={loading}
          />

          {selectedNamespace && !selectedNamespace.isDefault && (
            <DeleteNamespaceDialog
              open={deleteDialogOpen}
              onOpenChange={setDeleteDialogOpen}
              namespace={namespaceToDelete}
              onDeleteNamespace={handleDeleteNamespace}
              loading={loading}
              showTrigger
              onOpenDialog={() => {
                setNamespaceToDelete(selectedNamespace)
                setDeleteDialogOpen(true)
              }}
            />
          )}
        </div>
      )
    }