mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
feat(onboarding): step-agent-waiting polls real agent lastContact via useQuery
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
"use server";
|
||||
|
||||
import { userAction } from "@/lib/safe-actions/actions";
|
||||
import { z } from "zod";
|
||||
import { getAgentAction } from "@/features/agents/agents.action";
|
||||
|
||||
export const getAgentStatusAction = userAction
|
||||
.schema(z.object({ agentId: z.string() }))
|
||||
.action(async ({ parsedInput }) => {
|
||||
const result = await getAgentAction(parsedInput.agentId);
|
||||
if (!result?.data?.data) return { connected: false };
|
||||
const agent = result.data.data;
|
||||
const lastContact = agent.lastContact ? new Date(agent.lastContact) : null;
|
||||
const connected = lastContact !== null && Date.now() - lastContact.getTime() < 60_000;
|
||||
return { connected };
|
||||
});
|
||||
@@ -1,23 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useEffect } from "react";
|
||||
import { useOnboarding } from "@onboardjs/react";
|
||||
import { simulateAgentPing } from "@/features/onboarding/onboarding.mock";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { getAgentStatusAction } from "@/features/onboarding/actions/get-agent-status.action";
|
||||
import type { OnboardingAgent } from "@/features/onboarding/onboarding.types";
|
||||
|
||||
export const StepAgentWaiting = () => {
|
||||
const { next } = useOnboarding();
|
||||
const started = useRef(false);
|
||||
const { next, state } = useOnboarding();
|
||||
const agents = (state?.context.flowData.agents ?? []) as OnboardingAgent[];
|
||||
const firstAgentId = agents[0]?.id;
|
||||
|
||||
const { data } = useQuery({
|
||||
queryKey: ["agent-status", firstAgentId],
|
||||
queryFn: async () => {
|
||||
if (!firstAgentId) return { connected: false };
|
||||
const result = await getAgentStatusAction({ agentId: firstAgentId });
|
||||
return result?.data ?? { connected: false };
|
||||
},
|
||||
refetchInterval: 3000,
|
||||
enabled: !!firstAgentId,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (started.current) return;
|
||||
started.current = true;
|
||||
simulateAgentPing().then(() => next());
|
||||
}, [next]);
|
||||
if (data?.connected) {
|
||||
next();
|
||||
}
|
||||
}, [data?.connected, next]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center gap-2 h-full text-center">
|
||||
<div className="flex flex-col items-center justify-center gap-4 h-full text-center">
|
||||
<Loader2 className="size-10 animate-spin text-primary" />
|
||||
<h1 className="text-xl font-semibold">Waiting for your agent</h1>
|
||||
<p className="text-sm text-muted-foreground">Checking connectivity and database health...</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Checking connectivity every 3 seconds…
|
||||
</p>
|
||||
{agents.length > 0 && (
|
||||
<p className="text-xs text-zinc-500">Agent: {agents[0].name}</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user