fix: fix monitor list upPercent calc problem
This commit is contained in:
parent
eef78520e5
commit
0a3479d06c
@ -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,13 +79,7 @@ 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">
|
|
||||||
{monitors.map((monitor) => (
|
|
||||||
<div
|
<div
|
||||||
key={monitor.name}
|
key={monitor.name}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
@ -96,14 +118,9 @@ export const MonitorList: React.FC = React.memo(() => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
<MonitorHealthBar
|
<MonitorHealthBar monitorId={monitor.id} onBeatsItemUpdate={setBeats} />
|
||||||
monitorId={monitor.id}
|
|
||||||
onBeatsItemUpdate={setBeats}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
MonitorList.displayName = 'MonitorList';
|
MonitorListItem.displayName = 'MonitorListItem';
|
||||||
|
Loading…
Reference in New Issue
Block a user