import type { Routine } from "@rakazo/contracts"; import { Stack, useLocalSearchParams, useRouter } from "expo-router"; import { useEffect, useState } from "react"; import { ActivityIndicator, Pressable, ScrollView, Text, View } from "react-native"; import { rpc } from "../lib/api"; export default function RoutineDetail() { const { botId, botName, routineId } = useLocalSearchParams<{ botId?: string; botName?: string; routineId?: string; }>(); const router = useRouter(); const [routine, setRoutine] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!botId || !routineId) { setError("Routine link is incomplete"); setLoading(false); return; } let cancelled = false; setLoading(true); void rpc("routines/list", { botId }) .then((routines) => { if (cancelled) return; const match = routines.find((item) => item.id === routineId); if (match) setRoutine(match); else setError("This routine no longer exists"); }) .catch((loadError) => { if (!cancelled) { setError(loadError instanceof Error ? loadError.message : "Could not load routine"); } }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [botId, routineId]); return ( {loading ? : null} {error ? {error} : null} {routine ? ( <> {routine.name} {routine.active ? "Active" : "Paused"} · {routine.crons.join(", ")} ·{" "} {routine.timezone} Prompt {routine.prompt} router.push({ pathname: "/thread", params: { botId: botId ?? "", name: botName ?? "Bot" }, }) } style={{ alignItems: "center", borderRadius: 12, backgroundColor: "#F1F1EF", padding: 14, }} > Open conversation ) : null} ); }