import * as Tooltip from '@radix-ui/react-tooltip' import { Notification, NotificationStatus } from '@supabase/shared-types/out/notifications' import { useQuery, useQueryClient } from '@tanstack/react-query' import dayjs from 'dayjs' import { useState } from 'react' import { notificationKeys } from 'data/notifications/keys' import { useProjectsQuery } from 'data/projects/projects-query' import { useStore } from 'hooks' import { delete_, get } from 'lib/common/fetch' import { API_URL } from 'lib/constants' import { Project } from 'types' import { Button, IconX } from 'ui' import NotificationActions from './NotificationActions' import { formatNotificationCTAText, formatNotificationText } from './NotificationRows.utils' export interface NotificationRowProps { notification: Notification onSelectRestartProject: (project: Project, notification: Notification) => void onSelectApplyMigration: (project: Project, notification: Notification) => void onSelectRollbackMigration: (project: Project, notification: Notification) => void onSelectFinalizeMigration: (project: Project, notification: Notification) => void } const NotificationRow = ({ notification, onSelectRestartProject, onSelectApplyMigration, onSelectRollbackMigration, onSelectFinalizeMigration, }: NotificationRowProps) => { const { ui } = useStore() const queryClient = useQueryClient() const [dismissing, setDismissing] = useState(false) const { data: projects } = useProjectsQuery() const project = projects?.find((project) => project.id === notification.project_id) const insertedAt = dayjs(notification.inserted_at).format('DD MMM YYYY, HH:mma') const changelogLink = (notification.data as any).changelog_link const availableActions = notification.meta?.actions_available ?? [] // [Joshen TODO] This should be removed after the env of Feb when the migration notifications // have been removed, double check with Qiao before removing. // Relevant PR: https://github.com/supabase/supabase/pull/9229 const { data: ownerReassignStatus } = useQuery( ['projects', project?.ref, 'owner-reassign'], ({ signal }) => get(`${API_URL}/database/${project?.ref}/owner-reassign`, { signal }), { enabled: (notification.data as any).upgrade_type === 'schema-migration' && project !== undefined, } ) const dismissNotification = async (notificationId: string) => { if (!notificationId) return setDismissing(true) const { error } = await delete_(`${API_URL}/notifications`, { ids: [notificationId] }) if (error) { ui.setNotification({ category: 'error', message: 'Failed to dismiss notification', error, duration: 4000, }) } else { await queryClient.invalidateQueries(notificationKeys.list()) } setDismissing(false) } if (!project) return null return (
{notification.notification_status !== NotificationStatus.Seen && (
)}
{formatNotificationText(project, notification, ownerReassignStatus)} {formatNotificationCTAText(availableActions, ownerReassignStatus)}

{insertedAt}

{(availableActions.length > 0 || changelogLink !== undefined) && (
onSelectRestartProject(project, notification)} onSelectApplyMigration={() => onSelectApplyMigration(project, notification)} onSelectRollbackMigration={() => onSelectRollbackMigration(project, notification)} onSelectFinalizeMigration={() => onSelectFinalizeMigration(project, notification)} />
)}
) } export default NotificationRow