Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Hook up restart-service endpoint for notifications that require a postgresql restart

Joshen Limfd50c6b5270d
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationActions.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationActions.tsx
index ee9cac8fd42..1bf5f150b46 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationActions.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationActions.tsx
@@ -1,23 +1,34 @@
import { FC } from 'react'
+import { useRouter } from 'next/router'
import { Button } from '@supabase/ui'
+import { Project } from 'types'
+
interface Props {
+ project: Project
availableActions: any[]
+ onSelectRestartProject: () => void
}
-const NotificationActions: FC<Props> = ({ availableActions }) => {
+const NotificationActions: FC<Props> = ({ project, availableActions, onSelectRestartProject }) => {
+ const router = useRouter()
+
+ const onSelectUpgradeProject = () => {
+ return router.push(`/project/${project.ref}/settings/billing/update/pro`)
+ }
+
return (
<div className="space-y-2">
{availableActions.map((action: any) => {
if (action.action_type === 'project.upgrade') {
return (
- <Button key={action.action_type} type="default">
+ <Button key={action.action_type} type="default" onClick={onSelectUpgradeProject}>
Upgrade project
</Button>
)
} else if (action.action_type === 'postgresql.restart') {
return (
- <Button key={action.action_type} type="default">
+ <Button key={action.action_type} type="default" onClick={onSelectRestartProject}>
Restart project
</Button>
)
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
index ae0675d8a56..3673ab8dff2 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
@@ -9,9 +9,10 @@ import NotificationActions from './NotificationActions'
interface Props {
notification: Notification
+ onSelectRestartProject: (project: Project, notification: Notification) => void
}
-const NotificationRow: FC<Props> = ({ notification }) => {
+const NotificationRow: FC<Props> = ({ notification, onSelectRestartProject }) => {
const { app } = useStore()
const [project] = app.projects.list((project: Project) => project.id === notification.project_id)
@@ -33,7 +34,11 @@ const NotificationRow: FC<Props> = ({ notification }) => {
</div>
<div className="col-span-3 flex items-center justify-end">
{availableActions.length > 0 && (
- <NotificationActions availableActions={availableActions} />
+ <NotificationActions
+ project={project}
+ availableActions={availableActions}
+ onSelectRestartProject={() => onSelectRestartProject(project, notification)}
+ />
)}
</div>
</div>
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
index 973452b55e0..84bd65b71e4 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
@@ -1,17 +1,23 @@
-import { FC, Fragment } from 'react'
+import { FC, Fragment, useState } from 'react'
import { Button, IconBell, Popover } from '@supabase/ui'
-import { NotificationStatus } from '@supabase/shared-types/out/notifications'
+import { Notification, NotificationStatus } from '@supabase/shared-types/out/notifications'
-import { useNotifications } from 'hooks'
-import { patch } from 'lib/common/fetch'
-import NotificationRow from './NotificationRow'
+import { Project } from 'types'
+import { useNotifications, useStore } from 'hooks'
+import { patch, post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import NotificationRow from './NotificationRow'
+import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
interface Props {}
const NotificationsPopover: FC<Props> = () => {
+ const { app, ui } = useStore()
const { notifications } = useNotifications()
+ const [projectToRestart, setProjectToRestart] = useState<Project>()
+ const [targetNotification, setTargetNotification] = useState<Notification>()
+
if (!notifications) return <div />
const hasNewNotifications = notifications.some(
@@ -31,56 +37,104 @@ const NotificationsPopover: FC<Props> = () => {
}
}
+ const onConfirmProjectRestart = async () => {
+ if (!projectToRestart || !targetNotification) return
+
+ const { id } = targetNotification
+ const { id: projectId, ref, region } = projectToRestart
+ const { error } = await post(`${API_URL}/projects/${ref}/restart-services`, {
+ restartRequest: {
+ region,
+ source_notification_id: id,
+ services: ['postgresql'],
+ },
+ })
+
+ if (error) {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to restart project: ${error.message}`,
+ error,
+ })
+ } else {
+ app.onProjectPostgrestStatusUpdated(projectId, 'OFFLINE')
+ ui.setNotification({ category: 'success', message: `Restarting project` })
+ }
+
+ setProjectToRestart(undefined)
+ setTargetNotification(undefined)
+ }
+
return (
- <Popover
- size="content"
- align="end"
- side="bottom"
- sideOffset={8}
- onOpenChange={onOpenChange}
- overlay={
- <div className="w-[540px]">
- <div className="flex items-center justify-between border-b border-gray-500 bg-gray-400 px-4 py-2">
- <p className="text-sm">Notifications</p>
- {/* Area for improvement: Paginate notifications and show in a side panel */}
- {/* <p className="text-scale-1000 hover:text-scale-1200 cursor-pointer text-sm transition">
+ <>
+ <Popover
+ size="content"
+ align="end"
+ side="bottom"
+ sideOffset={8}
+ onOpenChange={onOpenChange}
+ overlay={
+ <div className="w-[540px]">
+ <div className="flex items-center justify-between border-b border-gray-500 bg-gray-400 px-4 py-2">
+ <p className="text-sm">Notifications</p>
+ {/* Area for improvement: Paginate notifications and show in a side panel */}
+ {/* <p className="text-scale-1000 hover:text-scale-1200 cursor-pointer text-sm transition">
See all{' '}
{notifications.length > MAX_NOTIFICATIONS_TO_SHOW && `(${notifications.length})`}
</p> */}
+ </div>
+ <div className="max-h-[380px] overflow-y-auto py-2">
+ {notifications.length === 0 ? (
+ <div className="py-2 px-4">
+ <p className="text-scale-1000 text-sm">No notifications available</p>
+ </div>
+ ) : (
+ <>
+ {notifications.map((notification, i: number) => (
+ <Fragment key={notification.id}>
+ <NotificationRow
+ notification={notification}
+ onSelectRestartProject={(project, notification) => {
+ setProjectToRestart(project)
+ setTargetNotification(notification)
+ }}
+ />
+ {i !== notifications.length - 1 && <Popover.Seperator />}
+ </Fragment>
+ ))}
+ </>
+ )}
+ </div>
</div>
- <div className="max-h-[380px] overflow-y-auto py-2">
- {notifications.length === 0 ? (
- <div className="py-2 px-4">
- <p className="text-scale-1000 text-sm">No notifications available</p>
- </div>
- ) : (
- <>
- {notifications.map((notification, i: number) => (
- <Fragment key={notification.id}>
- <NotificationRow notification={notification} />
- {i !== notifications.length - 1 && <Popover.Seperator />}
- </Fragment>
- ))}
- </>
- )}
- </div>
+ }
+ >
+ <div className="relative">
+ <Button
+ as="span"
+ id="notification-button"
+ type="default"
+ icon={<IconBell size={16} strokeWidth={1.5} className="text-scale-1200" />}
+ />
+ {hasNewNotifications && (
+ <div className="absolute -top-1 -right-1 z-50 flex h-3 w-3 items-center justify-center">
+ <div className="h-full w-full animate-ping rounded-full bg-green-800 opacity-60"></div>
+ <div className="z-60 absolute top-0 right-0 h-full w-full rounded-full bg-green-900 opacity-80"></div>
+ </div>
+ )}
</div>
- }
- >
- <div className="relative">
- <Button
- as="span"
- type="default"
- icon={<IconBell size={16} strokeWidth={1.5} className="text-scale-1200" />}
- />
- {hasNewNotifications && (
- <div className="absolute -top-1 -right-1 z-50 flex h-3 w-3 items-center justify-center">
- <div className="h-full w-full animate-ping rounded-full bg-green-800 opacity-60"></div>
- <div className="z-60 absolute top-0 right-0 h-full w-full rounded-full bg-green-900 opacity-80"></div>
- </div>
- )}
- </div>
- </Popover>
+ </Popover>
+
+ <ConfirmModal
+ danger
+ visible={projectToRestart !== undefined}
+ title={`Restart project "${projectToRestart?.name}"`}
+ description={`Are you sure you want to restart the project? There will be a few minutes of downtime.`}
+ buttonLabel="Restart"
+ buttonLoadingLabel="Restarting"
+ onSelectCancel={() => setProjectToRestart(undefined)}
+ onSelectConfirm={onConfirmProjectRestart}
+ />
+ </>
)
}