mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 5 of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | **5** | **#54057 ← this PR** | **Remaining `components/interfaces/**` and `QueryInsightsTable` router events** | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates all remaining `apps/studio/components/interfaces/**` directories off `next/router`, including the last production `router.events` consumer (`QueryInsightsTable`) and the only default `next/router` import (`DiscordCTACard`). After this PR, zero files under `apps/studio/components/**` import `next/router` or `next/compat/router`. **Added:** - `apps/studio/components/interfaces/QueryInsights/QueryInsightsTable/QueryInsightsTable.test.tsx`: verifies that pathname changes clear selected query state without mutating destination or history search params, while same-pathname search updates preserve selection **Changed:** - Replaced the sole production `router.events.on('routeChangeStart', ...)` consumer in `QueryInsightsTable.tsx` with a `pathname` effect that clears `onCurrentSelectQuery` on pathname changes, and derived `searchQuery` directly from `nuqs` (`urlSearch`) instead of mirroring it in local state. Drops the legacy `setQueryStates({ search: null })` call on leave, which ran after `history.push` on TanStack Router (targeting the destination URL rather than the previous history entry, while browser Back is expected to restore previous search params). - Removed the `router: NextRouter` prop from `ReportWidgetRendererProps` in `Reports/ReportWidget.tsx` and updated its callers (`ChartBlock`, `ReportHeader`, `CreateReportModal`) - Updated `LOG_ROUTES_WITH_REPLICA_SUPPORT` in `Settings/Logs/Logs.constants.ts` from `[ref]` to `$ref` for TanStack route patterns - Replaced the default `import router from 'next/router'` in `Support/DiscordCTACard.tsx` with `useNavigate` - Replaced `useRouter` across `Account`, `Advisors`, `App`, `Auth`, `ErrorHandling`, `Home`, `HomePageActions`, `Integrations`, `Linter`, `LocalDropdown`, `Observability`, `Organization`, `OrganizationInvite`, `Platform/Webhooks`, `Project`, `ProjectCreation`, `ProjectHome`, `QueryPerformance`, `RedeemCredits`, `Settings`, `Sidebar`, `SignIn`, `Support`, `UnifiedLogs`, and `UserDropdown` - Updated affected tests (`Auth/Users/UserPanel`, `Home/ProjectList`, `LocalDropdown`, `Organization/*`, `OrganizationInvite`, `RedeemCredits`, `Settings/General/*`, `SignIn/SignInRecoveryCodeForm`, `UnifiedLogs/*`) **Simplified:** - `LocalDropdown`, `UserDropdown` and `RedeemCredits` build the current route from `location.href`, so the hash keeps its leading `#` - `NewOrgForm` navigates to the `returnTo` path instead of an absolute URL, which was being treated as external. It also uses a typed `/new/$slug` navigate. - Read search params from `useParams()` instead of `location.search` casts (`CronJobPage`, `QueuePage`, `LogsPreviewer`, `LinkSupportTicketPage`, `MarketplaceSidebar`, `PlanCards`, `SignUpForm`) - Dropped the dead `isReady`/`isRouterReady` constants and the `navigate<AnyRouter, string>` casts where typed routes work (`ForgotPasswordWizard`, `SignInPartner`, `ReportWidget`, `ProjectUpgradeAlert`) - Linter, `ReportHeader` and `LogsPreviewer` use `to: '.'` instead of a separate pathname subscription. `CreateReportModal` builds the preserved query params in `onSuccess` instead of a `useMemo`. - Tests use the shared `createTestRouter` and `customRender` router. `LocalDropdown` now tests that the hash is kept. Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [x] Query Insights: select a query and confirm search/filter updates keep selection while switching projects/routes clears it; navigating to Logs keeps Logs' query params intact and pressing Back restores Query Insights' `?search=` param cleanly - [x] Auth / Sign-in flows: sign in, sign up, MFA, recovery code, forgot/reset password, and partner sign-in preserve `returnTo` and redirect as expected - [x] Organization & Project flows: creating an org/project, accepting an org invite, claiming a project, redeeming credits, and deleting/pausing/restarting a project or branch navigate cleanly - [ ] Logs & Reports: replica selector appears on supported log routes (`LOG_ROUTES_WITH_REPLICA_SUPPORT`), saved log queries open, and custom reports create/navigate properly - [ ] Sidebar, LocalDropdown, UserDropdown, Integrations (Cron/Queues/Marketplace), Advisors/Linter, and Unified Logs navigate cleanly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
203 lines
7.2 KiB
TypeScript
203 lines
7.2 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
|
import { useFlag } from 'common'
|
|
import { ChevronDownIcon, RefreshCwIcon } from 'lucide-react'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
SplitButtonGroup,
|
|
} from 'ui'
|
|
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
|
|
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { useSetProjectStatus } from '@/data/projects/project-detail-query'
|
|
import { useProjectRestartMutation } from '@/data/projects/project-restart-mutation'
|
|
import { useProjectRestartServicesMutation } from '@/data/projects/project-restart-services-mutation'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
|
|
import { useIsProjectActive, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { PROJECT_STATUS } from '@/lib/constants'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
export const RestartServerButton = () => {
|
|
const navigate = useNavigate()
|
|
const track = useTrack()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const isProjectActive = useIsProjectActive()
|
|
|
|
const isBranch = Boolean(project?.parent_project_ref)
|
|
const entityLabel = isBranch ? 'branch' : 'project'
|
|
const entityLabelCapitalized = entityLabel.charAt(0).toUpperCase() + entityLabel.slice(1)
|
|
const canRestart = isProjectActive || project?.status === PROJECT_STATUS.ACTIVE_UNHEALTHY
|
|
const { setProjectStatus } = useSetProjectStatus()
|
|
|
|
const [serviceToRestart, setServiceToRestart] = useState<'project' | 'branch' | 'database'>()
|
|
|
|
const { projectSettingsRestartProject } = useIsFeatureEnabled([
|
|
'project_settings:restart_project',
|
|
])
|
|
|
|
const projectRef = project?.ref ?? ''
|
|
const projectRegion = project?.region ?? ''
|
|
|
|
const projectRestartDisabled = useFlag('disableProjectRestarts')
|
|
const { can: canRestartProject } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.OPERATIONS_WRITE
|
|
)
|
|
|
|
const { mutate: restartProject, isPending: isRestartingProject } = useProjectRestartMutation({
|
|
onSuccess: () => {
|
|
onRestartSuccess()
|
|
},
|
|
onError: (error) => {
|
|
onRestartFailed(error, 'project')
|
|
},
|
|
})
|
|
const { mutate: restartProjectServices, isPending: isRestartingServices } =
|
|
useProjectRestartServicesMutation({
|
|
onSuccess: () => {
|
|
onRestartSuccess()
|
|
},
|
|
onError: (error) => {
|
|
onRestartFailed(error, 'database')
|
|
},
|
|
})
|
|
|
|
const isLoading = isRestartingProject || isRestartingServices
|
|
const hasRestartDropdown = canRestartProject && canRestart && !projectRestartDisabled
|
|
|
|
const requestProjectRestart = () => {
|
|
if (!canRestartProject) {
|
|
return toast.error('You do not have the required permissions to restart this project')
|
|
}
|
|
track('project_lifecycle_action_submitted', {
|
|
action: 'restart_project',
|
|
source: 'settings_general',
|
|
})
|
|
restartProject({ ref: projectRef })
|
|
}
|
|
|
|
const requestDatabaseRestart = () => {
|
|
if (!canRestartProject) {
|
|
return toast.error('You do not have the required permissions to restart this project')
|
|
}
|
|
track('project_lifecycle_action_submitted', {
|
|
action: 'restart_database',
|
|
source: 'settings_general',
|
|
})
|
|
restartProjectServices({ ref: projectRef, region: projectRegion, services: ['postgresql'] })
|
|
}
|
|
|
|
const onRestartFailed = (error: StudioError, type: string) => {
|
|
toast.error(`Unable to restart ${type}: ${error.message}`)
|
|
setServiceToRestart(undefined)
|
|
}
|
|
|
|
const onRestartSuccess = () => {
|
|
setProjectStatus({ ref: projectRef, status: PROJECT_STATUS.RESTARTING })
|
|
toast.success('Restarting server...')
|
|
navigate({ to: '/project/$ref', params: { ref: projectRef } })
|
|
setServiceToRestart(undefined)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{projectSettingsRestartProject ? (
|
|
<SplitButtonGroup className="w-full @lg:w-auto">
|
|
<ButtonTooltip
|
|
type="button"
|
|
className="flex-1 px-3 @lg:flex-none"
|
|
disabled={
|
|
project === undefined || !canRestartProject || !canRestart || projectRestartDisabled
|
|
}
|
|
onClick={() => setServiceToRestart(entityLabel)}
|
|
tooltip={{
|
|
content: {
|
|
side: 'bottom',
|
|
text: projectRestartDisabled
|
|
? `${entityLabelCapitalized} restart is currently disabled`
|
|
: !canRestartProject
|
|
? `You need additional permissions to restart this ${entityLabel}`
|
|
: !canRestart
|
|
? `Unable to restart ${entityLabel} as ${entityLabel} is not active`
|
|
: undefined,
|
|
},
|
|
}}
|
|
>
|
|
Restart {entityLabel}
|
|
</ButtonTooltip>
|
|
{hasRestartDropdown && (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
type="button"
|
|
aria-label="Choose restart type"
|
|
className="shrink-0 px-1 py-1.25"
|
|
icon={<ChevronDownIcon />}
|
|
disabled={!canRestartProject}
|
|
/>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" side="bottom">
|
|
<DropdownMenuItem
|
|
key="database"
|
|
disabled={isLoading}
|
|
onClick={() => {
|
|
setServiceToRestart('database')
|
|
}}
|
|
>
|
|
<div className="space-y-0.5">
|
|
<p className="block text-foreground">Fast database reboot</p>
|
|
<p className="block text-foreground-lighter">
|
|
Restarts only the database service, with less downtime than a full project
|
|
restart. Other project services remain running.
|
|
</p>
|
|
</div>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)}
|
|
</SplitButtonGroup>
|
|
) : (
|
|
<Button
|
|
icon={<RefreshCwIcon />}
|
|
className="w-full @lg:w-auto"
|
|
disabled={isLoading}
|
|
onClick={() => {
|
|
setServiceToRestart('database')
|
|
}}
|
|
>
|
|
Restart database
|
|
</Button>
|
|
)}
|
|
|
|
<ConfirmationModal
|
|
visible={serviceToRestart !== undefined}
|
|
variant="destructive"
|
|
title={`Restart ${serviceToRestart}`}
|
|
description={
|
|
<>
|
|
Are you sure you want to restart your {serviceToRestart}? There will be a few minutes of
|
|
downtime.
|
|
</>
|
|
}
|
|
confirmLabel="Restart"
|
|
confirmLabelLoading="Restarting"
|
|
loading={isLoading}
|
|
onCancel={() => setServiceToRestart(undefined)}
|
|
onConfirm={() => {
|
|
if (serviceToRestart === 'project' || serviceToRestart === 'branch') {
|
|
requestProjectRestart()
|
|
} else if (serviceToRestart === 'database') {
|
|
requestDatabaseRestart()
|
|
}
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|