FunctionSelector.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { useParams } from 'common'
  2. import { uniqBy } from 'lodash'
  3. import { Check, ChevronsUpDown, Plus } from 'lucide-react'
  4. import Link from 'next/link'
  5. import { useRouter } from 'next/router'
  6. import { useState } from 'react'
  7. import {
  8. Alert,
  9. AlertDescription,
  10. AlertTitle,
  11. Button,
  12. Command,
  13. CommandEmpty,
  14. CommandGroup,
  15. CommandInput,
  16. CommandItem,
  17. CommandList,
  18. CommandSeparator,
  19. Popover,
  20. PopoverContent,
  21. PopoverTrigger,
  22. ScrollArea,
  23. } from 'ui'
  24. import {
  25. DatabaseFunctionsData,
  26. useDatabaseFunctionsQuery,
  27. } from '@/data/database-functions/database-functions-query'
  28. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  29. type DatabaseFunction = DatabaseFunctionsData[number]
  30. interface FunctionSelectorProps {
  31. className?: string
  32. size?: 'tiny' | 'small'
  33. showError?: boolean
  34. schema?: string
  35. value: string
  36. onChange: (value: string) => void
  37. disabled?: boolean
  38. stopScrollPropagation?: boolean
  39. // used to filter the functions by a criteria
  40. filterFunction?: (func: DatabaseFunction) => boolean
  41. noResultsLabel?: React.ReactNode
  42. }
  43. const FunctionSelector = ({
  44. className,
  45. size = 'tiny',
  46. showError = true,
  47. disabled = false,
  48. schema,
  49. value,
  50. onChange,
  51. stopScrollPropagation = false,
  52. filterFunction = () => true,
  53. noResultsLabel = <span>No functions found in this schema.</span>,
  54. }: FunctionSelectorProps) => {
  55. const router = useRouter()
  56. const { ref } = useParams()
  57. const { data: project } = useSelectedProjectQuery()
  58. const [open, setOpen] = useState(false)
  59. const {
  60. data,
  61. error,
  62. isPending: isLoading,
  63. isError,
  64. isSuccess,
  65. refetch,
  66. } = useDatabaseFunctionsQuery({
  67. projectRef: project?.ref,
  68. connectionString: project?.connectionString,
  69. })
  70. const filteredFunctions = (data ?? [])
  71. .filter((func) => schema && func.schema === schema)
  72. .filter(filterFunction)
  73. const functions = uniqBy(filteredFunctions, (func) => func.name)
  74. return (
  75. <div className={className}>
  76. {isLoading && (
  77. <Button type="default" className="justify-start" block size={size} loading>
  78. Loading functions...
  79. </Button>
  80. )}
  81. {showError && isError && (
  82. <Alert variant="warning" className="px-3! py-3!">
  83. <AlertTitle className="text-xs text-amber-900">Failed to load functions</AlertTitle>
  84. <AlertDescription className="text-xs mb-2">Error: {error.message}</AlertDescription>
  85. <Button type="default" size="tiny" onClick={() => refetch()}>
  86. Reload functions
  87. </Button>
  88. </Alert>
  89. )}
  90. {isSuccess && (
  91. <Popover open={open} onOpenChange={setOpen} modal={false}>
  92. <PopoverTrigger asChild>
  93. <Button
  94. size={size}
  95. disabled={!!disabled}
  96. type="default"
  97. className={`w-full [&>span]:w-full ${size === 'small' ? 'py-1.5' : ''}`}
  98. iconRight={
  99. <ChevronsUpDown className="text-foreground-muted" strokeWidth={2} size={14} />
  100. }
  101. >
  102. {value ? (
  103. <div className="w-full flex gap-1">
  104. <p className="text-foreground-lighter">function:</p>
  105. <p className="text-foreground">{value}</p>
  106. </div>
  107. ) : (
  108. <div className="w-full flex gap-1">
  109. <p className="text-foreground-lighter">Select a function</p>
  110. </div>
  111. )}
  112. </Button>
  113. </PopoverTrigger>
  114. <PopoverContent className="p-0" side="bottom" align="start" sameWidthAsTrigger>
  115. <Command>
  116. <CommandInput placeholder="Search functions..." />
  117. <CommandList
  118. onWheel={stopScrollPropagation ? (event) => event.stopPropagation() : undefined}
  119. >
  120. <CommandEmpty>No functions found</CommandEmpty>
  121. <CommandGroup>
  122. <ScrollArea className={(functions || []).length > 7 ? 'h-[210px]' : ''}>
  123. {!functions.length && (
  124. <CommandItem
  125. key="no-function-found"
  126. disabled={true}
  127. className="flex items-center justify-between space-x-2 w-full"
  128. >
  129. {noResultsLabel}
  130. </CommandItem>
  131. )}
  132. {functions.map((func) => (
  133. <CommandItem
  134. key={func.id}
  135. value={func.name.replaceAll('"', '')}
  136. className="cursor-pointer flex items-center justify-between space-x-2 w-full"
  137. onSelect={() => {
  138. onChange(func.name)
  139. setOpen(false)
  140. }}
  141. onClick={() => {
  142. onChange(func.name)
  143. setOpen(false)
  144. }}
  145. >
  146. <span>{func.name}</span>
  147. {value === func.name && (
  148. <Check className="text-brand" size={14} strokeWidth={2} />
  149. )}
  150. </CommandItem>
  151. ))}
  152. </ScrollArea>
  153. </CommandGroup>
  154. <CommandSeparator />
  155. <CommandGroup>
  156. <CommandItem
  157. className="cursor-pointer w-full"
  158. onSelect={() => {
  159. setOpen(false)
  160. router.push(`/project/${ref}/database/functions`)
  161. }}
  162. onClick={() => setOpen(false)}
  163. >
  164. <Link
  165. href={`/project/${ref}/database/functions`}
  166. onClick={() => {
  167. setOpen(false)
  168. }}
  169. className="w-full flex items-center gap-2"
  170. >
  171. <Plus size={14} strokeWidth={1.5} />
  172. <p>New function</p>
  173. </Link>
  174. </CommandItem>
  175. </CommandGroup>
  176. </CommandList>
  177. </Command>
  178. </PopoverContent>
  179. </Popover>
  180. )}
  181. </div>
  182. )
  183. }
  184. export default FunctionSelector