TableSelector.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * TableSelector - Header component for switching between sand tables
  3. *
  4. * Displays the current table and provides a dropdown to switch between
  5. * discovered tables or add new ones manually.
  6. */
  7. import { useState } from 'react'
  8. import { useTable, type Table } from '@/contexts/TableContext'
  9. import { Button } from '@/components/ui/button'
  10. import { Input } from '@/components/ui/input'
  11. import { Badge } from '@/components/ui/badge'
  12. import {
  13. Popover,
  14. PopoverContent,
  15. PopoverTrigger,
  16. } from '@/components/ui/popover'
  17. import {
  18. Dialog,
  19. DialogContent,
  20. DialogHeader,
  21. DialogTitle,
  22. DialogFooter,
  23. } from '@/components/ui/dialog'
  24. import { toast } from 'sonner'
  25. import {
  26. Layers,
  27. Plus,
  28. Check,
  29. Wifi,
  30. WifiOff,
  31. Pencil,
  32. Trash2,
  33. ChevronDown,
  34. } from 'lucide-react'
  35. export function TableSelector() {
  36. const {
  37. tables,
  38. activeTable,
  39. setActiveTable,
  40. addTable,
  41. removeTable,
  42. updateTableName,
  43. } = useTable()
  44. const [isOpen, setIsOpen] = useState(false)
  45. const [showAddDialog, setShowAddDialog] = useState(false)
  46. const [showRenameDialog, setShowRenameDialog] = useState(false)
  47. const [newTableUrl, setNewTableUrl] = useState('')
  48. const [newTableName, setNewTableName] = useState('')
  49. const [renameTable, setRenameTable] = useState<Table | null>(null)
  50. const [renameValue, setRenameValue] = useState('')
  51. const [isAdding, setIsAdding] = useState(false)
  52. const handleSelectTable = (table: Table) => {
  53. if (table.id !== activeTable?.id) {
  54. setActiveTable(table)
  55. toast.success(`Switched to ${table.name}`)
  56. }
  57. setIsOpen(false)
  58. }
  59. const handleAddTable = async () => {
  60. if (!newTableUrl.trim()) {
  61. toast.error('Please enter a URL')
  62. return
  63. }
  64. setIsAdding(true)
  65. try {
  66. // Ensure URL has protocol
  67. let url = newTableUrl.trim()
  68. if (!url.startsWith('http://') && !url.startsWith('https://')) {
  69. url = `http://${url}`
  70. }
  71. const table = await addTable(url, newTableName.trim() || undefined)
  72. if (table) {
  73. toast.success(`Added ${table.name}`)
  74. setShowAddDialog(false)
  75. setNewTableUrl('')
  76. setNewTableName('')
  77. } else {
  78. toast.error('Failed to add table. Check the URL and try again.')
  79. }
  80. } finally {
  81. setIsAdding(false)
  82. }
  83. }
  84. const handleRename = async () => {
  85. if (!renameTable || !renameValue.trim()) return
  86. await updateTableName(renameTable.id, renameValue.trim())
  87. toast.success('Table renamed')
  88. setShowRenameDialog(false)
  89. setRenameTable(null)
  90. setRenameValue('')
  91. }
  92. const handleRemove = (table: Table) => {
  93. if (table.isCurrent) {
  94. toast.error("Can't remove the current table")
  95. return
  96. }
  97. removeTable(table.id)
  98. toast.success(`Removed ${table.name}`)
  99. }
  100. const openRenameDialog = (table: Table) => {
  101. setRenameTable(table)
  102. setRenameValue(table.name)
  103. setShowRenameDialog(true)
  104. }
  105. // Always show if there are tables or discovering
  106. // This allows users to manually add tables even with just one
  107. return (
  108. <>
  109. <Popover open={isOpen} onOpenChange={setIsOpen}>
  110. <PopoverTrigger asChild>
  111. <Button
  112. variant="ghost"
  113. size="sm"
  114. className="gap-2 h-9 px-3"
  115. >
  116. <Layers className="h-4 w-4" />
  117. <span className="hidden sm:inline max-w-[120px] truncate">
  118. {activeTable?.name || 'Select Table'}
  119. </span>
  120. <ChevronDown className="h-3 w-3 opacity-50" />
  121. </Button>
  122. </PopoverTrigger>
  123. <PopoverContent className="w-72 p-2" align="end">
  124. <div className="space-y-2">
  125. {/* Header */}
  126. <div className="px-2 py-1">
  127. <span className="text-sm font-medium">Sand Tables</span>
  128. </div>
  129. {/* Table list */}
  130. <div className="space-y-1">
  131. {tables.map(table => (
  132. <div
  133. key={table.id}
  134. className={`flex items-center gap-2 px-2 py-2 rounded-md cursor-pointer hover:bg-accent group ${
  135. activeTable?.id === table.id ? 'bg-accent' : ''
  136. }`}
  137. onClick={() => handleSelectTable(table)}
  138. >
  139. {/* Status indicator */}
  140. {table.isOnline ? (
  141. <Wifi className="h-3.5 w-3.5 text-green-500 flex-shrink-0" />
  142. ) : (
  143. <WifiOff className="h-3.5 w-3.5 text-red-500 flex-shrink-0" />
  144. )}
  145. {/* Name and info */}
  146. <div className="flex-1 min-w-0">
  147. <div className="flex items-center gap-2">
  148. <span className="text-sm truncate">{table.name}</span>
  149. {table.isCurrent && (
  150. <Badge variant="secondary" className="text-[10px] px-1 py-0">
  151. This
  152. </Badge>
  153. )}
  154. </div>
  155. <span className="text-xs text-muted-foreground truncate block">
  156. {table.host || new URL(table.url).hostname}
  157. </span>
  158. </div>
  159. {/* Selected indicator */}
  160. {activeTable?.id === table.id && (
  161. <Check className="h-4 w-4 text-primary flex-shrink-0" />
  162. )}
  163. {/* Actions - always visible on mobile, hover on desktop */}
  164. <div className="flex md:opacity-0 md:group-hover:opacity-100 items-center gap-1 transition-opacity">
  165. <Button
  166. variant="ghost"
  167. size="sm"
  168. className="h-7 w-7 p-0"
  169. onClick={e => {
  170. e.stopPropagation()
  171. openRenameDialog(table)
  172. }}
  173. title="Rename"
  174. >
  175. <Pencil className="h-3.5 w-3.5" />
  176. </Button>
  177. {!table.isCurrent && (
  178. <Button
  179. variant="ghost"
  180. size="sm"
  181. className="h-7 w-7 p-0 text-destructive hover:text-destructive"
  182. onClick={e => {
  183. e.stopPropagation()
  184. handleRemove(table)
  185. }}
  186. title="Remove"
  187. >
  188. <Trash2 className="h-3.5 w-3.5" />
  189. </Button>
  190. )}
  191. </div>
  192. </div>
  193. ))}
  194. </div>
  195. {/* Add table button */}
  196. <Button
  197. variant="outline"
  198. size="sm"
  199. className="w-full gap-2"
  200. onClick={() => setShowAddDialog(true)}
  201. >
  202. <Plus className="h-3.5 w-3.5" />
  203. Add Table Manually
  204. </Button>
  205. </div>
  206. </PopoverContent>
  207. </Popover>
  208. {/* Add Table Dialog */}
  209. <Dialog open={showAddDialog} onOpenChange={setShowAddDialog}>
  210. <DialogContent>
  211. <DialogHeader>
  212. <DialogTitle>Add Table Manually</DialogTitle>
  213. </DialogHeader>
  214. <div className="space-y-4 py-4">
  215. <div className="space-y-2">
  216. <label className="text-sm font-medium">Table URL</label>
  217. <Input
  218. placeholder="192.168.1.100:8080 or http://..."
  219. value={newTableUrl}
  220. onChange={e => setNewTableUrl(e.target.value)}
  221. onKeyDown={e => e.key === 'Enter' && handleAddTable()}
  222. />
  223. <p className="text-xs text-muted-foreground">
  224. Enter the IP address and port of the table's backend
  225. </p>
  226. </div>
  227. <div className="space-y-2">
  228. <label className="text-sm font-medium">Name (optional)</label>
  229. <Input
  230. placeholder="Living Room Table"
  231. value={newTableName}
  232. onChange={e => setNewTableName(e.target.value)}
  233. onKeyDown={e => e.key === 'Enter' && handleAddTable()}
  234. />
  235. </div>
  236. </div>
  237. <DialogFooter>
  238. <Button variant="outline" onClick={() => setShowAddDialog(false)}>
  239. Cancel
  240. </Button>
  241. <Button onClick={handleAddTable} disabled={isAdding}>
  242. {isAdding ? 'Adding...' : 'Add Table'}
  243. </Button>
  244. </DialogFooter>
  245. </DialogContent>
  246. </Dialog>
  247. {/* Rename Dialog */}
  248. <Dialog open={showRenameDialog} onOpenChange={setShowRenameDialog}>
  249. <DialogContent>
  250. <DialogHeader>
  251. <DialogTitle>Rename Table</DialogTitle>
  252. </DialogHeader>
  253. <div className="py-4">
  254. <Input
  255. placeholder="Table name"
  256. value={renameValue}
  257. onChange={e => setRenameValue(e.target.value)}
  258. onKeyDown={e => e.key === 'Enter' && handleRename()}
  259. autoFocus
  260. />
  261. </div>
  262. <DialogFooter>
  263. <Button variant="outline" onClick={() => setShowRenameDialog(false)}>
  264. Cancel
  265. </Button>
  266. <Button onClick={handleRename}>Save</Button>
  267. </DialogFooter>
  268. </DialogContent>
  269. </Dialog>
  270. </>
  271. )
  272. }