AIOptInModal.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useEffect } from 'react'
  3. import {
  4. Button,
  5. cn,
  6. Dialog,
  7. DialogContent,
  8. DialogFooter,
  9. DialogHeader,
  10. DialogSection,
  11. DialogSectionSeparator,
  12. DialogTitle,
  13. Form,
  14. } from 'ui'
  15. import { AIOptInLevelSelector } from '@/components/interfaces/Organization/GeneralSettings/AIOptInLevelSelector'
  16. import { useAIOptInForm } from '@/hooks/forms/useAIOptInForm'
  17. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  18. interface AIOptInModalProps {
  19. visible: boolean
  20. onCancel: () => void
  21. }
  22. export const AIOptInModal = ({ visible, onCancel }: AIOptInModalProps) => {
  23. const { form, onSubmit, isUpdating, currentOptInLevel } = useAIOptInForm(onCancel)
  24. const { can: canUpdateOrganization } = useAsyncCheckPermissions(
  25. PermissionAction.UPDATE,
  26. 'organizations'
  27. )
  28. const onOpenChange = (open: boolean) => {
  29. if (!open) {
  30. onCancel()
  31. }
  32. }
  33. useEffect(() => {
  34. if (visible) {
  35. form.reset({ aiOptInLevel: currentOptInLevel })
  36. }
  37. }, [visible, currentOptInLevel, form])
  38. return (
  39. <Dialog open={visible} onOpenChange={onOpenChange}>
  40. <DialogContent size="large" aria-describedby={undefined}>
  41. <Form {...form}>
  42. <form id="ai-opt-in-form" onSubmit={form.handleSubmit(onSubmit)}>
  43. <DialogHeader padding="small">
  44. <DialogTitle>Update Briven Assistant Opt-in Level</DialogTitle>
  45. </DialogHeader>
  46. <DialogSectionSeparator />
  47. <DialogSection className="space-y-4 pb-0" padding="small">
  48. <AIOptInLevelSelector
  49. control={form.control}
  50. disabled={!canUpdateOrganization || isUpdating}
  51. />
  52. </DialogSection>
  53. <DialogFooter
  54. padding="small"
  55. className={cn(!canUpdateOrganization && 'justify-between!')}
  56. >
  57. {!canUpdateOrganization && (
  58. <p className="text-sm text-foreground-lighter">
  59. You need additional permissions to update the opt-in level
  60. </p>
  61. )}
  62. <div className="flex items-center gap-x-2">
  63. <Button type="default" disabled={isUpdating} onClick={onCancel}>
  64. Cancel
  65. </Button>
  66. <Button
  67. type="primary"
  68. htmlType="submit"
  69. form="ai-opt-in-form"
  70. loading={isUpdating}
  71. disabled={isUpdating || !canUpdateOrganization || !form.formState.isDirty}
  72. >
  73. Confirm
  74. </Button>
  75. </div>
  76. </DialogFooter>
  77. </form>
  78. </Form>
  79. </DialogContent>
  80. </Dialog>
  81. )
  82. }