Skip to content

Commit 080123f

Browse files
committed
feat: add real-time update feature for AI Gateway logs
1 parent a83ee66 commit 080123f

File tree

1 file changed

+64
-10
lines changed
  • src/client/routes/aiGateway/$gatewayId

1 file changed

+64
-10
lines changed

src/client/routes/aiGateway/$gatewayId/index.tsx

+64-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import { defaultErrorHandler, trpc } from '@/api/trpc';
2-
import { CommonHeader } from '@/components/CommonHeader';
1+
import { createFileRoute } from '@tanstack/react-router';
2+
import { AIGatewayLogTable } from '@/components/aiGateway/AIGatewayLogTable';
3+
import { AIGatewayOverview } from '@/components/aiGateway/AIGatewayOverview';
4+
import { useState, useRef } from 'react';
5+
import { useTranslation } from '@i18next-toolkit/react';
6+
import { Switch } from '@/components/ui/switch';
7+
import { Label } from '@/components/ui/label';
8+
import { trpc, defaultErrorHandler } from '@/api/trpc';
9+
import { AlertConfirm } from '@/components/AlertConfirm';
10+
import { Button } from '@/components/ui/button';
311
import { CommonWrapper } from '@/components/CommonWrapper';
12+
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
13+
import { CommonHeader } from '@/components/CommonHeader';
414
import { ErrorTip } from '@/components/ErrorTip';
515
import { Loading } from '@/components/Loading';
6-
import { Button } from '@/components/ui/button';
7-
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
816
import { useCurrentWorkspaceId, useHasAdminPermission } from '@/store/user';
917
import { routeAuthBeforeLoad } from '@/utils/route';
10-
import { createFileRoute, useNavigate } from '@tanstack/react-router';
18+
import { useNavigate } from '@tanstack/react-router';
1119
import { LuPencil, LuTrash } from 'react-icons/lu';
12-
import { useTranslation } from '@i18next-toolkit/react';
13-
import { AlertConfirm } from '@/components/AlertConfirm';
1420
import { message } from 'antd';
1521
import { NotFoundTip } from '@/components/NotFoundTip';
1622
import { useEvent } from '@/hooks/useEvent';
17-
import { AIGatewayLogTable } from '@/components/aiGateway/AIGatewayLogTable';
18-
import { AIGatewayOverview } from '@/components/aiGateway/AIGatewayOverview';
23+
import { useWatch } from '@/hooks/useWatch';
1924

2025
export const Route = createFileRoute('/aiGateway/$gatewayId/')({
2126
beforeLoad: routeAuthBeforeLoad,
@@ -129,7 +134,10 @@ function PageComponent() {
129134
className="mt-4 w-full flex-1 space-y-4 overflow-hidden"
130135
>
131136
<div className="flex h-full w-full flex-col overflow-auto rounded-lg border p-4">
132-
<h3 className="mb-2 text-lg font-medium">{t('Gateway Logs')}</h3>
137+
<div className="mb-2 flex items-center justify-between">
138+
<h3 className="text-lg font-medium">{t('Gateway Logs')}</h3>
139+
<RealtimeUpdateButton gatewayId={gatewayId} />
140+
</div>
133141
<AIGatewayLogTable gatewayId={gatewayId} />
134142
</div>
135143
</TabsContent>
@@ -138,3 +146,49 @@ function PageComponent() {
138146
</CommonWrapper>
139147
);
140148
}
149+
150+
interface RealtimeUpdateButtonProps {
151+
gatewayId: string;
152+
}
153+
154+
function RealtimeUpdateButton({ gatewayId }: RealtimeUpdateButtonProps) {
155+
const { t } = useTranslation();
156+
const workspaceId = useCurrentWorkspaceId();
157+
const [isRealtime, setIsRealtime] = useState(false);
158+
const utils = trpc.useUtils();
159+
const timerRef = useRef<NodeJS.Timeout | null>(null);
160+
161+
useWatch([isRealtime, gatewayId], () => {
162+
if (isRealtime) {
163+
timerRef.current = setInterval(() => {
164+
utils.aiGateway.logs.invalidate({
165+
workspaceId,
166+
gatewayId,
167+
});
168+
}, 4000);
169+
} else {
170+
if (timerRef.current) {
171+
clearInterval(timerRef.current);
172+
timerRef.current = null;
173+
}
174+
}
175+
176+
return () => {
177+
if (timerRef.current) {
178+
clearInterval(timerRef.current);
179+
timerRef.current = null;
180+
}
181+
};
182+
});
183+
184+
return (
185+
<div className="flex items-center space-x-2">
186+
<Switch
187+
id="realtime-mode"
188+
checked={isRealtime}
189+
onCheckedChange={setIsRealtime}
190+
/>
191+
<Label htmlFor="realtime-mode">{t('Realtime Update')}</Label>
192+
</div>
193+
);
194+
}

0 commit comments

Comments
 (0)