dashboard-logs.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as Sentry from '@sentry/nextjs'
  2. import { SupportCategories } from '@supabase/shared-types/out/constants'
  3. import { createSupportStorageClient } from './support-storage-client'
  4. import type { ExtendedSupportCategories } from './Support.constants'
  5. import type {
  6. GenerateAttachmentURLsData,
  7. GenerateAttachmentURLsVariables,
  8. } from '@/data/support/generate-attachment-urls-mutation'
  9. import { getMirroredBreadcrumbs, getOwnershipOfBreadcrumbSnapshot } from '@/lib/breadcrumbs'
  10. import { uuidv4 } from '@/lib/helpers'
  11. import { sanitizeArrayOfObjects } from '@/lib/sanitize'
  12. export type DashboardBreadcrumb = Sentry.Breadcrumb
  13. export const DASHBOARD_LOG_BUCKET = 'dashboard-logs'
  14. export const DASHBOARD_LOG_CATEGORIES: ExtendedSupportCategories[] = [
  15. SupportCategories.DASHBOARD_BUG,
  16. ]
  17. export const getSanitizedBreadcrumbs = (): unknown[] => {
  18. const breadcrumbs = getOwnershipOfBreadcrumbSnapshot() ?? getMirroredBreadcrumbs()
  19. return sanitizeArrayOfObjects(breadcrumbs)
  20. }
  21. export const uploadDashboardLog = async ({
  22. userId,
  23. sanitizedLogs,
  24. uploadDashboardLogFn,
  25. }: {
  26. userId: string | undefined
  27. sanitizedLogs: unknown[]
  28. uploadDashboardLogFn: (
  29. vars: GenerateAttachmentURLsVariables
  30. ) => Promise<GenerateAttachmentURLsData>
  31. }): Promise<string[]> => {
  32. if (!userId) {
  33. console.error(
  34. '[SupportForm > uploadDashboardLog] Cannot upload dashboard log: user ID is undefined'
  35. )
  36. return []
  37. }
  38. if (sanitizedLogs.length === 0) return []
  39. try {
  40. const supportStorageClient = createSupportStorageClient()
  41. const objectKey = `${userId}/${uuidv4()}.json`
  42. const body = new Blob([JSON.stringify(sanitizedLogs, null, 2)], {
  43. type: 'application/json',
  44. })
  45. const { error: uploadError } = await supportStorageClient.storage
  46. .from(DASHBOARD_LOG_BUCKET)
  47. .upload(objectKey, body, {
  48. cacheControl: '3600',
  49. contentType: 'application/json',
  50. upsert: false,
  51. })
  52. if (uploadError) {
  53. console.error(
  54. '[SupportForm > uploadDashboardLog] Failed to upload dashboard log to support storage bucket',
  55. uploadError
  56. )
  57. return []
  58. }
  59. return uploadDashboardLogFn({
  60. bucket: DASHBOARD_LOG_BUCKET,
  61. filenames: [objectKey],
  62. })
  63. } catch (error) {
  64. console.error('[SupportForm] Unexpected error uploading dashboard log', error)
  65. return []
  66. }
  67. }