TableSelector.tsx 10 KB

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