Files
Alaister YoungandKevin Grüneberg 70da0f1d1d chore: cleanup packages (#27770)
* chore: cleanup packages

- Avoid circular imports
- Export API-types as types
- pg-format without depending on Node internal Buffer (not browser-compatible)
- Avoid importing from barrel files in ui dir

* chore: avoid barrel file imports in studio (#27771)

* chore: avoid barrel file imports

- Removes some unused imports
- Avoids barrel file import for faster builds + less memory

* add eslint rule

* type fixes

* delete layouts barrel

* delete components/grid barrel file

* delete components/grid/utils barrel file

* delete components/grid/components/common barrel file

* delete components/grid/components/editor barrel file

* delete components/grid/components/formatter barrel file

* delete components/grid/components/grid barrel file

* delete components/grid/components/header/filter barrel file

* remote components/grid/store barrel file

* remove components/interfaces/Auth/Policies barrel file

* delete components/interfaces/Settings/Logs barrel file

* delete components/ui/CodeEditor barrel file

* delete components/ui/Forms barrel file

* delete components/ui/Shimmers barrel file

* delete data/analytics barrel file

* delete hooks barrel file

* cleanup lib/common/fetch barrel file

* final * barral files cleanup

* global react-data-grid styles

* remove console.log

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>

* fix build

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2024-07-04 14:48:10 +08:00

179 lines
6.1 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useQueryClient } from '@tanstack/react-query'
import { useRouter } from 'next/router'
import { useState } from 'react'
import toast from 'react-hot-toast'
import {
useIsProjectActive,
useProjectContext,
} from 'components/layouts/ProjectLayout/ProjectContext'
import { useProjectRestartMutation } from 'data/projects/project-restart-mutation'
import { useProjectRestartServicesMutation } from 'data/projects/project-restart-services-mutation'
import { setProjectStatus } from 'data/projects/projects-query'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useFlag } from 'hooks/ui/useFlag'
import { ChevronDown } from 'lucide-react'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
TooltipContent_Shadcn_,
TooltipTrigger_Shadcn_,
Tooltip_Shadcn_,
} from 'ui'
import ConfirmModal from 'ui-patterns/Dialogs/ConfirmDialog'
const RestartServerButton = () => {
const router = useRouter()
const queryClient = useQueryClient()
const { project } = useProjectContext()
const isProjectActive = useIsProjectActive()
const [serviceToRestart, setServiceToRestart] = useState<'project' | 'database'>()
const projectRef = project?.ref ?? ''
const projectRegion = project?.region ?? ''
const projectRestartDisabled = useFlag('disableProjectRestarts')
const canRestartProject = useCheckPermissions(PermissionAction.INFRA_EXECUTE, 'reboot')
const { mutate: restartProject, isLoading: isRestartingProject } = useProjectRestartMutation({
onSuccess: () => {
onRestartSuccess()
},
onError: (error) => {
onRestartFailed(error, 'project')
},
})
const { mutate: restartProjectServices, isLoading: isRestartingServices } =
useProjectRestartServicesMutation({
onSuccess: () => {
onRestartSuccess()
},
onError: (error) => {
onRestartFailed(error, 'database')
},
})
const isLoading = isRestartingProject || isRestartingServices
const requestProjectRestart = () => {
if (!canRestartProject) {
return toast.error('You do not have the required permissions to restart this project')
}
restartProject({ ref: projectRef })
}
const requestDatabaseRestart = async () => {
if (!canRestartProject) {
return toast.error('You do not have the required permissions to restart this project')
}
restartProjectServices({ ref: projectRef, region: projectRegion, services: ['postgresql'] })
}
const onRestartFailed = (error: any, type: string) => {
toast.error(`Unable to restart ${type}: ${error.message}`)
setServiceToRestart(undefined)
}
const onRestartSuccess = () => {
setProjectStatus(queryClient, projectRef, 'RESTARTING')
toast.success('Restarting server...')
router.push(`/project/${projectRef}`)
setServiceToRestart(undefined)
}
return (
<>
<Tooltip_Shadcn_>
<TooltipTrigger_Shadcn_ asChild>
<div className="flex items-center">
<Button
type="default"
className={`px-3 ${canRestartProject && isProjectActive ? 'rounded-r-none' : ''}`}
disabled={
project === undefined ||
!canRestartProject ||
!isProjectActive ||
projectRestartDisabled
}
onClick={() => setServiceToRestart('project')}
>
Restart project
</Button>
{canRestartProject && isProjectActive && !projectRestartDisabled && (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
type="default"
className="rounded-l-none px-[4px] py-[5px]"
icon={<ChevronDown />}
disabled={!canRestartProject}
/>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" side="bottom">
<DropdownMenuItem
key="database"
disabled={isLoading}
onClick={() => {
setServiceToRestart('database')
}}
>
<div className="space-y-1">
<p className="block text-foreground">Fast database reboot</p>
<p className="block text-foreground-light">
Restarts only the database - faster but may not be able to recover from all
failure modes
</p>
</div>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
</TooltipTrigger_Shadcn_>
{((project !== undefined && (!canRestartProject || !isProjectActive)) ||
projectRestartDisabled) && (
<TooltipContent_Shadcn_ side="bottom">
{projectRestartDisabled
? 'Project restart is currently disabled'
: !canRestartProject
? 'You need additional permissions to restart this project'
: !isProjectActive
? 'Unable to restart project as project is not active'
: ''}
</TooltipContent_Shadcn_>
)}
</Tooltip_Shadcn_>
<ConfirmModal
danger
visible={serviceToRestart !== undefined}
title={`Restart ${serviceToRestart}`}
// @ts-ignore
description={
<>
Are you sure you want to restart the{' '}
<span className="text-foreground">{serviceToRestart}</span>? There will be a few minutes
of downtime.
</>
}
buttonLabel="Restart"
buttonLoadingLabel="Restarting"
onSelectCancel={() => setServiceToRestart(undefined)}
onSelectConfirm={async () => {
if (serviceToRestart === 'project') {
await requestProjectRestart()
} else if (serviceToRestart === 'database') {
await requestDatabaseRestart()
}
}}
/>
</>
)
}
export default RestartServerButton