IncidentAdmonition.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { AnimatePresence, motion } from 'framer-motion'
  2. import { ExternalLink } from 'lucide-react'
  3. import Link from 'next/link'
  4. import { Button } from 'ui'
  5. import { Admonition } from 'ui-patterns/admonition'
  6. import { useIncidentStatusQuery } from '@/data/platform/incident-status-query'
  7. import { processIncidentData } from '@/data/platform/incident-status-utils'
  8. interface IncidentAdmonitionProps {
  9. isActive: boolean
  10. className?: string
  11. }
  12. const STATUS_DESCRIPTION_SIGN_OFF = 'Follow the status page for updates.'
  13. const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)
  14. const getStatusDescription = (
  15. status: string,
  16. hasMultipleIncidents: boolean,
  17. allSameStatus: boolean
  18. ): string => {
  19. const isPlural = hasMultipleIncidents
  20. const issueTerm = isPlural ? 'these issues' : 'this issue'
  21. switch (status) {
  22. case 'investigating':
  23. if (hasMultipleIncidents && !allSameStatus) {
  24. return `We are aware of multiple ongoing issues and are investigating. ${STATUS_DESCRIPTION_SIGN_OFF}`
  25. }
  26. return `We are investigating ${issueTerm}. ${STATUS_DESCRIPTION_SIGN_OFF}`
  27. case 'identified':
  28. if (hasMultipleIncidents && !allSameStatus) {
  29. return `We have identified the cause of some of ${issueTerm} and are working on fixes. ${STATUS_DESCRIPTION_SIGN_OFF}`
  30. }
  31. return `We have identified the cause of ${issueTerm} and are working on a fix. ${STATUS_DESCRIPTION_SIGN_OFF}`
  32. case 'monitoring':
  33. if (hasMultipleIncidents && !allSameStatus) {
  34. return `Fixes have been deployed for some of ${issueTerm} and we are monitoring the results. ${STATUS_DESCRIPTION_SIGN_OFF}`
  35. }
  36. return `A fix has been deployed and we are monitoring the results. ${STATUS_DESCRIPTION_SIGN_OFF}`
  37. case 'resolved':
  38. if (hasMultipleIncidents && !allSameStatus) {
  39. return `Some of ${issueTerm} have been resolved, but others may still be ongoing. ${STATUS_DESCRIPTION_SIGN_OFF}`
  40. }
  41. return `${capitalizeFirstLetter(issueTerm)} ${isPlural ? 'have' : 'has'} been resolved but may take some time to fully recover. ${STATUS_DESCRIPTION_SIGN_OFF}`
  42. default:
  43. return `We are investigating ${issueTerm}. ${STATUS_DESCRIPTION_SIGN_OFF}`
  44. }
  45. }
  46. export function IncidentAdmonition({ isActive, className }: IncidentAdmonitionProps) {
  47. const { data: allStatusPageEvents, isLoading, isError } = useIncidentStatusQuery()
  48. const { incidents = [] } = allStatusPageEvents ?? {}
  49. // Don't render anything while loading, on error, or if no incidents
  50. if (isLoading || isError || !incidents || incidents.length === 0) {
  51. return null
  52. }
  53. const { hasMultipleIncidents, mostCriticalIncident, overallStatus, allSameStatus } =
  54. processIncidentData(incidents)
  55. // Show most recent incident name + count if multiple incidents
  56. const statusTitle =
  57. (mostCriticalIncident?.name ?? '') +
  58. (hasMultipleIncidents
  59. ? ` and ${incidents.length - 1} other issue${incidents.length > 2 ? 's' : ''}`
  60. : '')
  61. return (
  62. <AnimatePresence>
  63. {isActive && (
  64. <motion.aside
  65. initial={{ height: 0, opacity: 0 }}
  66. animate={{ height: 'auto', opacity: 1 }}
  67. exit={{ height: 0, opacity: 0 }}
  68. >
  69. <Admonition
  70. type="warning"
  71. layout="horizontal"
  72. className={className}
  73. title={statusTitle}
  74. description={getStatusDescription(overallStatus, hasMultipleIncidents, allSameStatus)}
  75. actions={
  76. <Button asChild type="default" icon={<ExternalLink strokeWidth={1.5} />}>
  77. <Link href="https://status.supabase.com/" target="_blank" rel="noreferrer">
  78. Status page
  79. </Link>
  80. </Button>
  81. }
  82. />
  83. </motion.aside>
  84. )}
  85. </AnimatePresence>
  86. )
  87. }