refactor: change status page data scope

This commit is contained in:
moonrailgun 2023-12-18 00:57:51 +08:00
parent 3cdbb948da
commit f61ca1a207
5 changed files with 28 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import type { SubscribeEventMap, SocketEventMap } from '../../server/ws/shared';
import { create } from 'zustand'; import { create } from 'zustand';
import { useEvent } from '../hooks/useEvent'; import { useEvent } from '../hooks/useEvent';
import { useEffect, useReducer, useState } from 'react'; import { useEffect, useReducer, useState } from 'react';
import { useIsLogined, useUserInfo } from '../store/user';
const useSocketStore = create<{ const useSocketStore = create<{
socket: Socket | null; socket: Socket | null;
@ -120,6 +121,7 @@ export function useSocketSubscribeList<
>(name: K, options: UseSocketSubscribeListOptions<K, T> = {}): T[] { >(name: K, options: UseSocketSubscribeListOptions<K, T> = {}): T[] {
const { filter = defaultFilter } = options; const { filter = defaultFilter } = options;
const { subscribe } = useSocket(); const { subscribe } = useSocket();
const isLogined = useIsLogined();
const [list, push] = useReducer( const [list, push] = useReducer(
(state: T[], data: T) => [...state, data], (state: T[], data: T) => [...state, data],
[] as T[] [] as T[]
@ -132,6 +134,11 @@ export function useSocketSubscribeList<
}); });
useEffect(() => { useEffect(() => {
if (!isLogined) {
console.warn('Skip socket subscribe login because of not login');
return;
}
const unsubscribe = subscribe(name, cb); const unsubscribe = subscribe(name, cb);
return () => { return () => {

View File

@ -37,7 +37,8 @@ export const MonitorListItem: React.FC<{
<div <div
className={clsx( className={clsx(
className, className,
'flex rounded-lg py-3 px-4 cursor-pointer mb-1 bg-green-500 bg-opacity-0 hover:bg-opacity-10' 'flex rounded-lg py-3 px-4 mb-1 bg-green-500 bg-opacity-0 hover:bg-opacity-10',
onClick && 'cursor-pointer'
)} )}
onClick={onClick} onClick={onClick}
> >

View File

@ -24,13 +24,12 @@ export const StatusPageServices: React.FC<StatusPageServicesProps> = React.memo(
<div className="shadow-2xl p-2.5 flex flex-col gap-4"> <div className="shadow-2xl p-2.5 flex flex-col gap-4">
{list.length > 0 ? ( {list.length > 0 ? (
list.map((item) => ( list.map((item) => (
<div key={item.id} className="hover:bg-black hover:bg-opacity-20">
<MonitorListItem <MonitorListItem
key={item.id}
workspaceId={workspaceId} workspaceId={workspaceId}
monitorId={item.id} monitorId={item.id}
monitorName={item.name} monitorName={item.name}
/> />
</div>
)) ))
) : ( ) : (
<Empty description="No any monitor has been set" /> <Empty description="No any monitor has been set" />

View File

@ -35,6 +35,10 @@ export function useUserInfo(): UserLoginInfo | null {
return useUserStore((state) => state.info); return useUserStore((state) => state.info);
} }
export function useIsLogined() {
return !!useUserInfo();
}
export function useCurrentWorkspace() { export function useCurrentWorkspace() {
const currentWorkspace = useUserStore( const currentWorkspace = useUserStore(
(state) => state.info?.currentWorkspace (state) => state.info?.currentWorkspace

View File

@ -256,15 +256,18 @@ export const monitorRouter = router({
return monitor; return monitor;
}), }),
recentData: workspaceProcedure recentData: publicProcedure
.meta( .meta({
buildMonitorOpenapi({ openapi: {
tags: [OPENAPI_TAG.MONITOR],
protect: false,
method: 'GET', method: 'GET',
path: '/{monitorId}/recentData', path: `/monitor/{monitorId}/recentData`,
},
}) })
)
.input( .input(
z.object({ z.object({
workspaceId: z.string().cuid2(),
monitorId: z.string().cuid2(), monitorId: z.string().cuid2(),
take: z.number(), take: z.number(),
}) })