AnalyticsSettings.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { zodResolver } from '@hookform/resolvers/zod'
  2. import { useConsentState } from 'common'
  3. import { useForm } from 'react-hook-form'
  4. import { toast } from 'sonner'
  5. import { Card, CardContent, Form, FormControl, FormField, Switch } from 'ui'
  6. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  7. import {
  8. PageSection,
  9. PageSectionContent,
  10. PageSectionDescription,
  11. PageSectionMeta,
  12. PageSectionSummary,
  13. PageSectionTitle,
  14. } from 'ui-patterns/PageSection'
  15. import * as z from 'zod'
  16. import { useSendResetMutation } from '@/data/telemetry/send-reset-mutation'
  17. const AnalyticsSchema = z.object({
  18. telemetryEnabled: z.boolean(),
  19. })
  20. export const AnalyticsSettings = () => {
  21. const { hasAccepted, acceptAll, denyAll, categories } = useConsentState()
  22. const hasLoaded = categories !== null
  23. const { mutate: sendReset } = useSendResetMutation()
  24. const form = useForm<z.infer<typeof AnalyticsSchema>>({
  25. resolver: zodResolver(AnalyticsSchema as any),
  26. values: { telemetryEnabled: hasAccepted },
  27. })
  28. const handleToggle = (value: boolean) => {
  29. if (!hasLoaded) {
  30. toast.error(
  31. "We couldn't load the privacy settings due to an ad blocker or network error. Please disable any ad blockers and try again. If the problem persists, please contact support."
  32. )
  33. form.setValue('telemetryEnabled', !value)
  34. return
  35. }
  36. if (value) {
  37. acceptAll()
  38. } else {
  39. denyAll()
  40. sendReset()
  41. }
  42. form.setValue('telemetryEnabled', value)
  43. }
  44. return (
  45. <PageSection>
  46. <PageSectionMeta>
  47. <PageSectionSummary>
  48. <PageSectionTitle>Analytics and Marketing</PageSectionTitle>
  49. <PageSectionDescription>
  50. Control whether telemetry and marketing data is sent from Briven services.
  51. </PageSectionDescription>
  52. </PageSectionSummary>
  53. </PageSectionMeta>
  54. <PageSectionContent>
  55. <Form {...form}>
  56. <Card>
  57. <CardContent>
  58. <FormField
  59. control={form.control}
  60. name="telemetryEnabled"
  61. render={({ field }) => (
  62. <FormItemLayout
  63. layout="flex-row-reverse"
  64. label="Send telemetry data from Briven services"
  65. description="By opting in to sharing telemetry data, Briven can analyze usage patterns to enhance user experience and use it for marketing and advertising purposes"
  66. >
  67. <FormControl>
  68. <Switch
  69. checked={field.value}
  70. onCheckedChange={(value) => {
  71. field.onChange(value)
  72. handleToggle(value)
  73. }}
  74. />
  75. </FormControl>
  76. </FormItemLayout>
  77. )}
  78. />
  79. </CardContent>
  80. </Card>
  81. </Form>
  82. </PageSectionContent>
  83. </PageSection>
  84. )
  85. }