mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
fix(frontend): support static export dynamic routes compliance via client useParams
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import {usePathname, useRouter, useSearchParams} from 'next/navigation';
|
||||
import {useCallback, useMemo, useState} from 'react';
|
||||
import {useParams, usePathname, useRouter, useSearchParams} from 'next/navigation';
|
||||
import {useCallback, useEffect, useMemo, useState} from 'react';
|
||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query';
|
||||
import {ArrowLeft, Globe, Pencil, Trash2} from 'lucide-react';
|
||||
import {toast} from 'sonner';
|
||||
@@ -40,7 +40,14 @@ function getZonePageTab(value: string | null | undefined): ZonePageTab {
|
||||
return zoneTabs.includes(value as ZonePageTab) ? (value as ZonePageTab) : 'overview';
|
||||
}
|
||||
|
||||
export function ZonePageClient({zoneId}: {zoneId: number}) {
|
||||
export function ZonePageClient() {
|
||||
const params = useParams();
|
||||
const zoneId = Number(params?.zoneId);
|
||||
|
||||
const [mounted, setMounted] = useState(false);
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
@@ -96,7 +103,13 @@ export function ZonePageClient({zoneId}: {zoneId: number}) {
|
||||
onError: (error) => toast.error(getErrorMessage(error)),
|
||||
});
|
||||
|
||||
|
||||
if (!mounted) {
|
||||
return (
|
||||
<div className="py-6 px-1">
|
||||
<LoadingStateWithBorder icon={Globe} description="加载 Zone 详情中..." />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!Number.isInteger(zoneId) || zoneId <= 0) {
|
||||
return (
|
||||
|
||||
@@ -1,29 +1,20 @@
|
||||
import {Suspense} from 'react'
|
||||
import {Globe} from 'lucide-react'
|
||||
import dynamic from 'next/dynamic'
|
||||
import {LoadingStateWithBorder} from '@/components/layout/loading'
|
||||
import {ZonePageClient} from './page-client'
|
||||
|
||||
const ZonePageClient = dynamic(
|
||||
() => import('./page-client').then((mod) => mod.ZonePageClient),
|
||||
{
|
||||
ssr: false,
|
||||
loading: () => (
|
||||
<div className="py-6 px-1">
|
||||
<LoadingStateWithBorder icon={Globe} description="加载 Zone 详情中..." />
|
||||
</div>
|
||||
),
|
||||
}
|
||||
)
|
||||
export async function generateStaticParams() {
|
||||
return [{zoneId: '1'}];
|
||||
}
|
||||
|
||||
export default async function ZonePage({params}: PageProps<'/websites/[zoneId]'>) {
|
||||
const {zoneId} = await params
|
||||
export default async function ZonePage() {
|
||||
return (
|
||||
<Suspense fallback={
|
||||
<div className="py-6 px-1">
|
||||
<LoadingStateWithBorder icon={Globe} description="加载 Zone 详情中..." />
|
||||
</div>
|
||||
}>
|
||||
<ZonePageClient zoneId={Number(zoneId)} />
|
||||
<ZonePageClient />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ class ResizeObserverMock {
|
||||
|
||||
vi.stubGlobal('ResizeObserver', ResizeObserverMock);
|
||||
|
||||
let mockZoneId = '42';
|
||||
|
||||
const replaceMock = vi.fn();
|
||||
|
||||
vi.mock('next/link', () => ({
|
||||
@@ -23,8 +25,9 @@ vi.mock('next/link', () => ({
|
||||
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({replace: replaceMock, back: vi.fn()}),
|
||||
usePathname: () => '/websites/42',
|
||||
usePathname: () => `/websites/${mockZoneId}`,
|
||||
useSearchParams: () => new URLSearchParams(),
|
||||
useParams: () => ({zoneId: mockZoneId}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/services/openflare', async (importOriginal) => {
|
||||
@@ -47,6 +50,7 @@ vi.mock('@/lib/services/openflare', async (importOriginal) => {
|
||||
});
|
||||
|
||||
function renderPage(zoneId: number) {
|
||||
mockZoneId = String(zoneId);
|
||||
const client = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {retry: false, gcTime: 0},
|
||||
@@ -54,7 +58,7 @@ function renderPage(zoneId: number) {
|
||||
});
|
||||
return render(
|
||||
<QueryClientProvider client={client}>
|
||||
<ZonePageClient zoneId={zoneId} />
|
||||
<ZonePageClient />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user