TableSelector.tsx 9.6 KB

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