OrganizationSelector.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { UseFormReturn } from 'react-hook-form'
  2. import {
  3. Badge,
  4. FormControl,
  5. FormField,
  6. Select,
  7. SelectContent,
  8. SelectGroup,
  9. SelectItem,
  10. SelectTrigger,
  11. SelectValue,
  12. } from 'ui'
  13. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  14. import type { SupportFormValues } from './SupportForm.schema'
  15. import { getOrgSubscriptionPlan, NO_ORG_MARKER, NO_PROJECT_MARKER } from './SupportForm.utils'
  16. // End of third-party imports
  17. import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
  18. interface OrganizationSelectorProps {
  19. form: UseFormReturn<SupportFormValues>
  20. orgSlug: string | null
  21. }
  22. export function OrganizationSelector({ form, orgSlug }: OrganizationSelectorProps) {
  23. const { data: organizations, isSuccess: isSuccessOrganizations } = useOrganizationsQuery()
  24. const subscriptionPlanId = getOrgSubscriptionPlan(organizations, orgSlug)
  25. return (
  26. <FormField
  27. name="organizationSlug"
  28. control={form.control}
  29. render={({ field }) => {
  30. const { ref: _ref, ...fieldWithoutRef } = field
  31. return (
  32. <FormItemLayout hideMessage layout="vertical" label="Which organization is affected?">
  33. <FormControl>
  34. <Select
  35. {...fieldWithoutRef}
  36. disabled={!isSuccessOrganizations}
  37. defaultValue={field.value}
  38. onValueChange={(value) => {
  39. const previousOrgSlug = form.getValues('organizationSlug')
  40. field.onChange(value)
  41. if (previousOrgSlug !== value) {
  42. form.resetField('projectRef', { defaultValue: NO_PROJECT_MARKER })
  43. }
  44. }}
  45. >
  46. <SelectTrigger className="w-full" aria-label="Select an organization">
  47. <SelectValue asChild placeholder="Select an organization">
  48. <div className="flex items-center gap-x-2">
  49. {orgSlug === NO_ORG_MARKER ? (
  50. <span>No specific organization</span>
  51. ) : (
  52. (organizations ?? []).find((o) => o.slug === field.value)?.name
  53. )}
  54. {subscriptionPlanId && <Badge variant="default">{subscriptionPlanId}</Badge>}
  55. </div>
  56. </SelectValue>
  57. </SelectTrigger>
  58. <SelectContent>
  59. <SelectGroup>
  60. {organizations?.map((org) => (
  61. <SelectItem key={org.slug} value={org.slug}>
  62. {org.name}
  63. </SelectItem>
  64. ))}
  65. {isSuccessOrganizations && (organizations ?? []).length === 0 && (
  66. <SelectItem value={NO_ORG_MARKER}>No specific organization</SelectItem>
  67. )}
  68. </SelectGroup>
  69. </SelectContent>
  70. </Select>
  71. </FormControl>
  72. </FormItemLayout>
  73. )
  74. }}
  75. />
  76. )
  77. }