DashboardLogsToggle.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { ChevronRight } from 'lucide-react'
  2. import { useMemo, useState } from 'react'
  3. import type { UseFormReturn } from 'react-hook-form'
  4. import { Collapsible, CollapsibleContent, CollapsibleTrigger, FormField, Switch } from 'ui'
  5. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  6. import { DASHBOARD_LOG_CATEGORIES } from './dashboard-logs'
  7. import type { SupportFormValues } from './SupportForm.schema'
  8. interface DashboardLogsToggleProps {
  9. form: UseFormReturn<SupportFormValues>
  10. sanitizedLog: unknown[]
  11. align?: 'left' | 'right'
  12. className?: string
  13. }
  14. export function DashboardLogsToggle({
  15. form,
  16. sanitizedLog,
  17. align = 'left',
  18. className,
  19. }: DashboardLogsToggleProps) {
  20. const sanitizedLogJson = useMemo(() => JSON.stringify(sanitizedLog, null, 2), [sanitizedLog])
  21. const [isPreviewOpen, setIsPreviewOpen] = useState(false)
  22. if (!DASHBOARD_LOG_CATEGORIES.includes(form.getValues('category'))) return
  23. return (
  24. <FormField
  25. name="attachDashboardLogs"
  26. control={form.control}
  27. render={({ field }) => (
  28. <FormItemLayout
  29. hideMessage
  30. name="attachDashboardLogs"
  31. className={className}
  32. layout="flex"
  33. align={align}
  34. label={
  35. <div className="flex items-center gap-x-2">
  36. <span className="text-foreground">Include dashboard activity log</span>
  37. </div>
  38. }
  39. description={
  40. <div className="flex flex-col">
  41. <span className="text-foreground-light">
  42. Share sanitized logs of recent dashboard actions to help reproduce the issue.
  43. </span>
  44. <Collapsible className="mt-2" open={isPreviewOpen} onOpenChange={setIsPreviewOpen}>
  45. <CollapsibleTrigger
  46. className={
  47. 'group flex items-center gap-x-1 group-data-open:text-foreground hover:text-foreground transition'
  48. }
  49. >
  50. <ChevronRight
  51. size={14}
  52. className="transition-all group-data-open:rotate-90 text-foreground-muted -ml-1"
  53. />
  54. <span className="text-sm">Preview log</span>
  55. </CollapsibleTrigger>
  56. <CollapsibleContent className="mt-2">
  57. <pre className="bg-background-surface-200 border border-strong rounded-lg p-3 max-h-60 overflow-y-auto overflow-x-auto text-xs text-foreground-light whitespace-pre-wrap">
  58. {sanitizedLogJson}
  59. </pre>
  60. </CollapsibleContent>
  61. </Collapsible>
  62. </div>
  63. }
  64. >
  65. <Switch
  66. size="large"
  67. id="attachDashboardLogs"
  68. checked={field.value}
  69. onCheckedChange={field.onChange}
  70. />
  71. </FormItemLayout>
  72. )}
  73. />
  74. )
  75. }