fix: fix monitor list upPercent calc problem

This commit is contained in:
moonrailgun 2023-10-28 15:01:23 +08:00
parent eef78520e5
commit 0a3479d06c

View File

@ -1,17 +1,18 @@
import clsx from 'clsx'; import clsx from 'clsx';
import React, { useMemo, useState } from 'react'; import React, { useMemo, useState } from 'react';
import { useNavigate } from 'react-router'; import { useNavigate } from 'react-router';
import { trpc } from '../../api/trpc'; import { AppRouterOutput, trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user'; import { useCurrentWorkspaceId } from '../../store/user';
import { NoWorkspaceTip } from '../NoWorkspaceTip'; import { NoWorkspaceTip } from '../NoWorkspaceTip';
import { MonitorHealthBar } from './MonitorHealthBar'; import { MonitorHealthBar } from './MonitorHealthBar';
type MonitorType = AppRouterOutput['monitor']['all'][number];
export const MonitorList: React.FC = React.memo(() => { export const MonitorList: React.FC = React.memo(() => {
const currentWorkspaceId = useCurrentWorkspaceId()!; const workspaceId = useCurrentWorkspaceId();
const { data: monitors = [] } = trpc.monitor.all.useQuery({ const { data: monitors = [] } = trpc.monitor.all.useQuery({
workspaceId: currentWorkspaceId, workspaceId,
}); });
const navigate = useNavigate();
const initMonitorId = useMemo(() => { const initMonitorId = useMemo(() => {
const pathname = window.location.pathname; const pathname = window.location.pathname;
const re = /^\/monitor\/([^\/]+?)$/; const re = /^\/monitor\/([^\/]+?)$/;
@ -25,6 +26,37 @@ export const MonitorList: React.FC = React.memo(() => {
return null; return null;
}, []); }, []);
const [selectedMonitorId, setSelectedMonitorId] = useState<string | null>(
initMonitorId
);
if (!workspaceId) {
return <NoWorkspaceTip />;
}
return (
<div className="p-2">
{monitors.map((monitor) => (
<MonitorListItem
key={monitor.id}
monitor={monitor}
selectedMonitorId={selectedMonitorId}
setSelectedMonitorId={setSelectedMonitorId}
/>
))}
</div>
);
});
MonitorList.displayName = 'MonitorList';
export const MonitorListItem: React.FC<{
monitor: MonitorType;
selectedMonitorId: string | null;
setSelectedMonitorId: (monitorId: string) => void;
}> = React.memo((props) => {
const { monitor, selectedMonitorId, setSelectedMonitorId } = props;
const navigate = useNavigate();
const [beats, setBeats] = useState< const [beats, setBeats] = useState<
({ ({
value: number; value: number;
@ -32,10 +64,6 @@ export const MonitorList: React.FC = React.memo(() => {
} | null)[] } | null)[]
>([]); >([]);
const [selectedMonitorId, setSelectedMonitorId] = useState<string | null>(
initMonitorId
);
const upPercent = useMemo(() => { const upPercent = useMemo(() => {
let up = 0; let up = 0;
beats.forEach((b) => { beats.forEach((b) => {
@ -51,59 +79,48 @@ export const MonitorList: React.FC = React.memo(() => {
return parseFloat(((up / beats.length) * 100).toFixed(1)); return parseFloat(((up / beats.length) * 100).toFixed(1));
}, [beats]); }, [beats]);
if (!currentWorkspaceId) {
return <NoWorkspaceTip />;
}
return ( return (
<div className="p-2"> <div
{monitors.map((monitor) => ( key={monitor.name}
<div className={clsx(
key={monitor.name} 'flex rounded-lg py-3 px-4 cursor-pointer mb-1',
selectedMonitorId === monitor.id
? 'bg-green-500 bg-opacity-20'
: 'bg-green-500 bg-opacity-0 hover:bg-opacity-10'
)}
onClick={() => {
navigate(`/monitor/${monitor.id}`);
setSelectedMonitorId(monitor.id);
}}
>
<div>
<span
className={clsx( className={clsx(
'flex rounded-lg py-3 px-4 cursor-pointer mb-1', 'min-w-[62px] p-0.5 rounded-full text-white inline-block text-center',
selectedMonitorId === monitor.id upPercent === 100 ? 'bg-green-400' : 'bg-amber-400'
? 'bg-green-500 bg-opacity-20'
: 'bg-green-500 bg-opacity-0 hover:bg-opacity-10'
)} )}
onClick={() => {
navigate(`/monitor/${monitor.id}`);
setSelectedMonitorId(monitor.id);
}}
> >
<div> {upPercent}%
<span </span>
className={clsx( </div>
'min-w-[62px] p-0.5 rounded-full text-white inline-block text-center', <div className="flex-1 pl-2">
upPercent === 100 ? 'bg-green-400' : 'bg-amber-400' <div className="text-base">{monitor.name}</div>
)} {/* <div>
> {monitor.tags.map((tag) => (
{upPercent}% <span
</span> className="py-0.5 px-1 rounded-full text-white text-sm"
</div> style={{ backgroundColor: tag.color }}
<div className="flex-1 pl-2"> >
<div className="text-base">{monitor.name}</div> {tag.label}
{/* <div> </span>
{monitor.tags.map((tag) => (
<span
className="py-0.5 px-1 rounded-full text-white text-sm"
style={{ backgroundColor: tag.color }}
>
{tag.label}
</span>
))}
</div> */}
</div>
<div className="flex items-center">
<MonitorHealthBar
monitorId={monitor.id}
onBeatsItemUpdate={setBeats}
/>
</div>
</div>
))} ))}
</div> */}
</div>
<div className="flex items-center">
<MonitorHealthBar monitorId={monitor.id} onBeatsItemUpdate={setBeats} />
</div>
</div> </div>
); );
}); });
MonitorList.displayName = 'MonitorList'; MonitorListItem.displayName = 'MonitorListItem';