Skip to content

Commit

Permalink
refactor(v2): add common sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Apr 7, 2024
1 parent 68ace91 commit f9a51e4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/client/components/CommonList.tsx
Expand Up @@ -46,7 +46,7 @@ export const CommonList: React.FC<CommonListProps> = React.memo((props) => {
const finalList = searchResult ?? props.items;

return (
<>
<div className="flex flex-col h-full">
{props.hasSearch && (
<div className="bg-background/95 p-4 backdrop-blur supports-[backdrop-filter]:bg-background/60">
<form>
Expand All @@ -63,7 +63,7 @@ export const CommonList: React.FC<CommonListProps> = React.memo((props) => {
</div>
)}

<ScrollArea className="h-screen">
<ScrollArea className="flex-1">
<div className="flex flex-col gap-2 p-4 pt-0">
{finalList.map((item) => {
const isSelected = item.href === location.pathname;
Expand Down Expand Up @@ -105,7 +105,7 @@ export const CommonList: React.FC<CommonListProps> = React.memo((props) => {
})}
</div>
</ScrollArea>
</>
</div>
);
});
CommonList.displayName = 'CommonList';
Expand Down
22 changes: 22 additions & 0 deletions src/client/components/CommonSidebar.tsx
@@ -0,0 +1,22 @@
import React from 'react';
import { Separator } from './ui/separator';

interface CommonSidebarProps extends React.PropsWithChildren {
header: React.ReactNode;
}
export const CommonSidebar: React.FC<CommonSidebarProps> = React.memo(
(props) => {
return (
<div className="h-full flex flex-col">
<div className="flex items-center px-4 py-2 h-[52px]">
{props.header}
</div>

<Separator />

<div className="flex-1 overflow-hidden">{props.children}</div>
</div>
);
}
);
CommonSidebar.displayName = 'CommonSidebar';
2 changes: 1 addition & 1 deletion src/client/components/website/AddWebsiteBtn.tsx
Expand Up @@ -24,7 +24,7 @@ import {
FormLabel,
FormMessage,
} from '@/components/ui/form';
import { useEvent, useEventWithLoading } from '@/hooks/useEvent';
import { useEvent } from '@/hooks/useEvent';
import { Input } from '../ui/input';
import { preventDefault } from '@/utils/dom';
import { trpc } from '@/api/trpc';
Expand Down
22 changes: 11 additions & 11 deletions src/client/pages/LayoutV2.tsx
Expand Up @@ -17,14 +17,10 @@ import { cn } from '@/utils/style';
import { Separator } from '@/components/ui/separator';
import { Nav } from './Layout/Nav';
import { WorkspaceSwitcher } from '@/components/WorkspaceSwitcher';
import { ColorSchemeSwitcher } from '@/components/ColorSchemeSwitcher';
import { LanguageSelector } from '@/components/LanguageSelector';
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
import { useUserInfo } from '@/store/user';
import { Button } from '@/components/ui/button';
import { UserConfig } from './Layout/UserConfig';
import { Outlet } from '@tanstack/react-router';
import { CommonList, CommonListItem } from '@/components/CommonList';
import { trpc } from '@/api/trpc';
import { useCurrentWorkspaceId } from '@/store/user';

const defaultLayout: [number, number, number] = [265, 440, 655];

Expand All @@ -41,6 +37,10 @@ export const LayoutV2: React.FC<{
defaultValue: false,
}
);
const workspaceId = useCurrentWorkspaceId();
const { data: serviceCount } = trpc.workspace.getServiceCount.useQuery({
workspaceId,
});

return (
<TooltipProvider delayDuration={0}>
Expand Down Expand Up @@ -83,13 +83,13 @@ export const LayoutV2: React.FC<{
links={[
{
title: 'Website',
label: '128',
label: String(serviceCount?.website ?? ''),
icon: LuAreaChart,
variant: 'default',
},
{
title: 'Monitor',
label: '9',
label: String(serviceCount?.monitor ?? ''),
icon: LuMonitorDot,
variant: 'ghost',
},
Expand All @@ -101,13 +101,13 @@ export const LayoutV2: React.FC<{
},
{
title: 'Telemetry',
label: '',
label: String(serviceCount?.telemetry ?? ''),
icon: LuWifi,
variant: 'ghost',
},
{
title: 'Pages',
label: '',
label: String(serviceCount?.page ?? ''),
icon: LuFilePieChart,
variant: 'ghost',
},
Expand All @@ -121,7 +121,7 @@ export const LayoutV2: React.FC<{
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel defaultSize={layout[1]} minSize={30}>
<div>{props.list}</div>
<div className="h-full overflow-hidden">{props.list}</div>
</ResizablePanel>
<ResizableHandle withHandle />
<ResizablePanel defaultSize={layout[2]}>
Expand Down
22 changes: 12 additions & 10 deletions src/client/routes/website.tsx
@@ -1,5 +1,6 @@
import { trpc } from '@/api/trpc';
import { CommonList } from '@/components/CommonList';
import { CommonSidebar } from '@/components/CommonSidebar';
import { Separator } from '@/components/ui/separator';
import { AddWebsiteBtn } from '@/components/website/AddWebsiteBtn';
import { useDataReady } from '@/hooks/useDataReady';
Expand Down Expand Up @@ -54,18 +55,19 @@ function WebsiteComponent() {
return (
<LayoutV2
list={
<div>
<div className="flex items-center px-4 py-2">
<h1 className="text-xl font-bold">{t('Website')}</h1>

<div className="ml-auto">
<AddWebsiteBtn />
</div>
</div>
<Separator />
<CommonSidebar
header={
<>
<h1 className="text-xl font-bold">{t('Website')}</h1>

<div className="ml-auto">
<AddWebsiteBtn />
</div>
</>
}
>
<CommonList hasSearch={true} items={items} />
</div>
</CommonSidebar>
}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/server/trpc/routers/website.ts
Expand Up @@ -64,7 +64,7 @@ export const websiteRouter = router({
.meta({
openapi: {
method: 'GET',
path: `/workspace/{workspaceId}/website/all`,
path: '/workspace/{workspaceId}/website/all',
tags: [OPENAPI_TAG.WEBSITE],
protect: true,
},
Expand Down
69 changes: 68 additions & 1 deletion src/server/trpc/routers/workspace.ts
@@ -1,8 +1,16 @@
import { publicProcedure, router, workspaceOwnerProcedure } from '../trpc';
import {
OpenApiMetaInfo,
publicProcedure,
router,
workspaceOwnerProcedure,
workspaceProcedure,
} from '../trpc';
import { z } from 'zod';
import { prisma } from '../../model/_client';
import { workspaceDashboardLayoutSchema } from '../../model/_schema';
import { Prisma } from '@prisma/client';
import { OPENAPI_TAG } from '../../utils/const';
import { OpenApiMeta } from 'trpc-openapi';

export const workspaceRouter = router({
getUserWorkspaceRole: publicProcedure
Expand All @@ -27,6 +35,54 @@ export const workspaceRouter = router({

return relation?.role ?? null;
}),
getServiceCount: workspaceProcedure
.meta(
buildWorkspaceOpenapi({
method: 'GET',
path: '/getServiceCount',
})
)
.output(
z.object({
website: z.number(),
monitor: z.number(),
telemetry: z.number(),
page: z.number(),
})
)
.query(async ({ input }) => {
const { workspaceId } = input;

const [website, monitor, telemetry, page] = await Promise.all([
prisma.website.count({
where: {
workspaceId,
},
}),
prisma.monitor.count({
where: {
workspaceId,
},
}),
prisma.telemetry.count({
where: {
workspaceId,
},
}),
prisma.monitorStatusPage.count({
where: {
workspaceId,
},
}),
]);

return {
website,
monitor,
telemetry,
page,
};
}),
updateDashboardOrder: workspaceOwnerProcedure
.input(
z.object({
Expand Down Expand Up @@ -64,3 +120,14 @@ export const workspaceRouter = router({
});
}),
});

function buildWorkspaceOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
return {
openapi: {
tags: [OPENAPI_TAG.WORKSPACE],
protect: true,
...meta,
path: `/workspace/{workspaceId}${meta.path}`,
},
};
}

0 comments on commit f9a51e4

Please sign in to comment.