From d97c6719133ee1c2d8c6faa29d18e4520036722d Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 30 Mar 2024 00:08:02 +0800 Subject: [PATCH] feat(v2): add page list/add/detail --- .../components/monitor/StatusPage/index.tsx | 1 + src/client/routeTree.gen.ts | 31 +++++++ src/client/routes/__root.tsx | 2 - src/client/routes/monitor/add.tsx | 4 +- src/client/routes/page.tsx | 86 +++++++++++++++++++ src/client/routes/page/$slug.tsx | 42 +++++++++ src/client/routes/page/add.tsx | 61 +++++++++++++ src/server/trpc/routers/monitor.ts | 3 + 8 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 src/client/routes/page.tsx create mode 100644 src/client/routes/page/$slug.tsx create mode 100644 src/client/routes/page/add.tsx diff --git a/src/client/components/monitor/StatusPage/index.tsx b/src/client/components/monitor/StatusPage/index.tsx index 8d5461c..8e311a4 100644 --- a/src/client/components/monitor/StatusPage/index.tsx +++ b/src/client/components/monitor/StatusPage/index.tsx @@ -75,6 +75,7 @@ export const MonitorStatusPage: React.FC = React.memo( /> )} +
rootRoute, } as any) +const PageRoute = PageImport.update({ + path: '/page', + getParentRoute: () => rootRoute, +} as any) + const MonitorRoute = MonitorImport.update({ path: '/monitor', getParentRoute: () => rootRoute, @@ -82,6 +90,16 @@ const TelemetryTelemetryIdRoute = TelemetryTelemetryIdImport.update({ getParentRoute: () => TelemetryRoute, } as any) +const PageAddRoute = PageAddImport.update({ + path: '/add', + getParentRoute: () => PageRoute, +} as any) + +const PageSlugRoute = PageSlugImport.update({ + path: '/$slug', + getParentRoute: () => PageRoute, +} as any) + const MonitorAddRoute = MonitorAddImport.update({ path: '/add', getParentRoute: () => MonitorRoute, @@ -112,6 +130,10 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof MonitorImport parentRoute: typeof rootRoute } + '/page': { + preLoaderRoute: typeof PageImport + parentRoute: typeof rootRoute + } '/register': { preLoaderRoute: typeof RegisterImport parentRoute: typeof rootRoute @@ -132,6 +154,14 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof MonitorAddImport parentRoute: typeof MonitorImport } + '/page/$slug': { + preLoaderRoute: typeof PageSlugImport + parentRoute: typeof PageImport + } + '/page/add': { + preLoaderRoute: typeof PageAddImport + parentRoute: typeof PageImport + } '/telemetry/$telemetryId': { preLoaderRoute: typeof TelemetryTelemetryIdImport parentRoute: typeof TelemetryImport @@ -158,6 +188,7 @@ export const routeTree = rootRoute.addChildren([ DashboardRoute, LoginRoute, MonitorRoute.addChildren([MonitorMonitorIdRoute, MonitorAddRoute]), + PageRoute.addChildren([PageSlugRoute, PageAddRoute]), RegisterRoute, TelemetryRoute.addChildren([TelemetryTelemetryIdRoute, TelemetryAddRoute]), WebsiteRoute.addChildren([WebsiteWebsiteIdRoute, WebsiteAddRoute]), diff --git a/src/client/routes/__root.tsx b/src/client/routes/__root.tsx index 0324e36..7f08345 100644 --- a/src/client/routes/__root.tsx +++ b/src/client/routes/__root.tsx @@ -6,8 +6,6 @@ interface RouterContext { userInfo: any; } -const defaultLayout: [number, number, number] = [265, 440, 655]; - export const Route = createRootRouteWithContext()({ component: () => { return ( diff --git a/src/client/routes/monitor/add.tsx b/src/client/routes/monitor/add.tsx index c52531b..1f1b4c7 100644 --- a/src/client/routes/monitor/add.tsx +++ b/src/client/routes/monitor/add.tsx @@ -21,9 +21,9 @@ export const Route = createFileRoute('/monitor/add')({ function MonitorAddComponent() { const { t } = useTranslation(); const workspaceId = useCurrentWorkspaceId(); - const addWebsiteMutation = trpc.website.add.useMutation(); const navigate = useNavigate(); const mutation = useMonitorUpsert(); + const utils = trpc.useUtils(); const handleSubmit = useEvent(async (values: MonitorInfoEditorValues) => { const res = await mutation.mutateAsync({ @@ -31,6 +31,8 @@ function MonitorAddComponent() { workspaceId, }); + utils.monitor.all.refetch(); + navigate({ to: '/monitor/$monitorId', params: { diff --git a/src/client/routes/page.tsx b/src/client/routes/page.tsx new file mode 100644 index 0000000..6e78728 --- /dev/null +++ b/src/client/routes/page.tsx @@ -0,0 +1,86 @@ +import { trpc } from '@/api/trpc'; +import { CommonHeader } from '@/components/CommonHeader'; +import { CommonList } from '@/components/CommonList'; +import { CommonWrapper } from '@/components/CommonWrapper'; +import { Button } from '@/components/ui/button'; +import { useDataReady } from '@/hooks/useDataReady'; +import { useEvent } from '@/hooks/useEvent'; +import { LayoutV2 } from '@/pages/LayoutV2'; +import { useCurrentWorkspaceId } from '@/store/user'; +import { routeAuthBeforeLoad } from '@/utils/route'; +import { useTranslation } from '@i18next-toolkit/react'; +import { + createFileRoute, + useNavigate, + useRouterState, +} from '@tanstack/react-router'; +import { LuPlus } from 'react-icons/lu'; + +export const Route = createFileRoute('/page')({ + beforeLoad: routeAuthBeforeLoad, + component: PageComponent, +}); + +function PageComponent() { + const workspaceId = useCurrentWorkspaceId(); + const { t } = useTranslation(); + const { data = [] } = trpc.monitor.getAllPages.useQuery({ + workspaceId, + }); + const navigate = useNavigate(); + const pathname = useRouterState({ + select: (state) => state.location.pathname, + }); + + const items = data.map((item) => ({ + id: item.id, + title: item.title, + content: item.slug, + href: `/page/${item.slug}`, + })); + + useDataReady( + () => data.length > 0, + () => { + if (pathname === Route.fullPath) { + navigate({ + to: '/page/$slug', + params: { + slug: data[0].slug, + }, + }); + } + } + ); + + const handleClickAdd = useEvent(() => { + navigate({ + to: '/page/add', + }); + }); + + return ( + + {t('Add')} + + } + /> + } + > + + + } + /> + ); +} diff --git a/src/client/routes/page/$slug.tsx b/src/client/routes/page/$slug.tsx new file mode 100644 index 0000000..5b7e7ed --- /dev/null +++ b/src/client/routes/page/$slug.tsx @@ -0,0 +1,42 @@ +import { trpc } from '@/api/trpc'; +import { CommonHeader } from '@/components/CommonHeader'; +import { CommonWrapper } from '@/components/CommonWrapper'; +import { ErrorTip } from '@/components/ErrorTip'; +import { Loading } from '@/components/Loading'; +import { NotFoundTip } from '@/components/NotFoundTip'; +import { MonitorStatusPage } from '@/components/monitor/StatusPage'; +import { ScrollArea } from '@/components/ui/scroll-area'; +import { routeAuthBeforeLoad } from '@/utils/route'; +import { createFileRoute } from '@tanstack/react-router'; + +export const Route = createFileRoute('/page/$slug')({ + beforeLoad: routeAuthBeforeLoad, + component: PageDetailComponent, +}); + +function PageDetailComponent() { + const { slug } = Route.useParams<{ slug: string }>(); + const { data: pageInfo, isLoading } = trpc.monitor.getPageInfo.useQuery({ + slug, + }); + + if (!slug) { + return ; + } + + if (isLoading) { + return ; + } + + if (!pageInfo) { + return ; + } + + return ( + }> + {/* */} + + {/* */} + + ); +} diff --git a/src/client/routes/page/add.tsx b/src/client/routes/page/add.tsx new file mode 100644 index 0000000..fd35979 --- /dev/null +++ b/src/client/routes/page/add.tsx @@ -0,0 +1,61 @@ +import { createFileRoute, useNavigate } from '@tanstack/react-router'; +import { useTranslation } from '@i18next-toolkit/react'; +import { useEvent } from '@/hooks/useEvent'; +import { useCurrentWorkspaceId } from '@/store/user'; +import { trpc } from '@/api/trpc'; +import { Card, CardContent } from '@/components/ui/card'; +import { CommonWrapper } from '@/components/CommonWrapper'; +import { routeAuthBeforeLoad } from '@/utils/route'; +import { + MonitorStatusPageEditForm, + MonitorStatusPageEditFormValues, +} from '@/components/monitor/StatusPage/EditForm'; + +export const Route = createFileRoute('/page/add')({ + beforeLoad: routeAuthBeforeLoad, + component: PageAddComponent, +}); + +function PageAddComponent() { + const { t } = useTranslation(); + const workspaceId = useCurrentWorkspaceId(); + const createPageMutation = trpc.monitor.createPage.useMutation(); + const navigate = useNavigate(); + const utils = trpc.useUtils(); + + const handleSubmit = useEvent( + async (values: MonitorStatusPageEditFormValues) => { + const res = await createPageMutation.mutateAsync({ + ...values, + workspaceId, + }); + + utils.monitor.getAllPages.refetch(); + + navigate({ + to: '/page/$slug', + params: { + slug: res.slug, + }, + }); + } + ); + + return ( + {t('Add Page')}} + > +
+ + + + + +
+
+ ); +} diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index 5176668..6ab2b4f 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -554,6 +554,9 @@ export const monitorRouter = router({ where: { workspaceId, }, + orderBy: { + updatedAt: 'desc', + }, }); }), getPageInfo: publicProcedure