SchemaComboBox.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Check, ChevronsUpDown } from 'lucide-react'
  2. import { useState } from 'react'
  3. import {
  4. Alert,
  5. AlertDescription,
  6. AlertTitle,
  7. Button,
  8. Command,
  9. CommandEmpty,
  10. CommandGroup,
  11. CommandInput,
  12. CommandItem,
  13. CommandList,
  14. Popover,
  15. PopoverContent,
  16. PopoverTrigger,
  17. ScrollArea,
  18. } from 'ui'
  19. import { useSchemasQuery } from '@/data/database/schemas-query'
  20. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  21. interface SchemaComboBoxProps {
  22. className?: string
  23. disabled?: boolean
  24. size?: 'tiny' | 'small'
  25. showError?: boolean
  26. selectedSchemas: string[]
  27. supportSelectAll?: boolean
  28. excludedSchemas?: string[]
  29. onSelectSchemas: (schemas: string[]) => void
  30. label: string
  31. }
  32. export const SchemaComboBox = ({
  33. className,
  34. disabled = false,
  35. size = 'tiny',
  36. showError = true,
  37. selectedSchemas = [],
  38. excludedSchemas = [],
  39. label,
  40. onSelectSchemas,
  41. }: SchemaComboBoxProps) => {
  42. const [open, setOpen] = useState(false)
  43. const { data: project } = useSelectedProjectQuery()
  44. const {
  45. data,
  46. isPending: isSchemasLoading,
  47. isSuccess: isSchemasSuccess,
  48. isError: isSchemasError,
  49. error: schemasError,
  50. refetch: refetchSchemas,
  51. } = useSchemasQuery({
  52. projectRef: project?.ref,
  53. connectionString: project?.connectionString,
  54. })
  55. const schemas = (data || [])
  56. .filter((schema) => !excludedSchemas.includes(schema.name))
  57. .sort((a, b) => a.name.localeCompare(b.name))
  58. const toggleSchema = (schema: string) => {
  59. if (selectedSchemas.includes(schema)) {
  60. onSelectSchemas(selectedSchemas.filter((s) => s !== schema))
  61. } else {
  62. const newSelectedSchemas = [...selectedSchemas, schema].sort((a, b) => a.localeCompare(b))
  63. onSelectSchemas(newSelectedSchemas)
  64. }
  65. }
  66. return (
  67. <div className={className}>
  68. {isSchemasLoading && (
  69. <Button type="default" className="justify-start" block size={size} loading>
  70. Loading schemas...
  71. </Button>
  72. )}
  73. {showError && isSchemasError && (
  74. <Alert variant="warning" className="px-3! py-3!">
  75. <AlertTitle className="text-xs text-amber-900">Failed to load schemas</AlertTitle>
  76. <AlertDescription className="text-xs mb-2 wrap-break-word">
  77. Error: {(schemasError as any)?.message}
  78. </AlertDescription>
  79. <Button type="default" size="tiny" onClick={() => refetchSchemas()}>
  80. Reload schemas
  81. </Button>
  82. </Alert>
  83. )}
  84. {isSchemasSuccess && (
  85. <Popover open={open} onOpenChange={setOpen} modal={false}>
  86. <PopoverTrigger asChild>
  87. <Button
  88. size={size}
  89. disabled={disabled}
  90. type="default"
  91. className={`w-full [&>span]:w-full`}
  92. iconRight={
  93. <ChevronsUpDown className="text-foreground-muted" strokeWidth={2} size={14} />
  94. }
  95. >
  96. <div className="w-full flex">
  97. <p className="text-foreground">{label}</p>
  98. </div>
  99. </Button>
  100. </PopoverTrigger>
  101. <PopoverContent className="p-0 w-56" side="bottom" align="start">
  102. <Command>
  103. <CommandInput placeholder="Find schema..." />
  104. <CommandList>
  105. <CommandEmpty>No schemas found</CommandEmpty>
  106. <CommandGroup>
  107. <ScrollArea className={(schemas || []).length > 7 ? 'h-[210px]' : ''}>
  108. {schemas?.map((schema) => (
  109. <CommandItem
  110. key={schema.id}
  111. className="cursor-pointer flex items-center justify-between space-x-2 w-full"
  112. onSelect={() => toggleSchema(schema.name)}
  113. onClick={() => toggleSchema(schema.name)}
  114. >
  115. <span>{schema.name}</span>
  116. {selectedSchemas.includes(schema.name) && (
  117. <Check className="text-brand" strokeWidth={2} size={16} />
  118. )}
  119. </CommandItem>
  120. ))}
  121. </ScrollArea>
  122. </CommandGroup>
  123. </CommandList>
  124. </Command>
  125. </PopoverContent>
  126. </Popover>
  127. )}
  128. </div>
  129. )
  130. }