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"; import { useI18n } from "../lib/i18n"; import { useMobileTokens } from "../lib/native"; export default function RoutineDetail() { const tokens = useMobileTokens(); const { t } = useI18n(); 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(t("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(t("This routine no longer exists")); }) .catch((loadError) => { if (!cancelled) { setError(loadError instanceof Error ? loadError.message : t("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 ? t("Active") : t("Paused"), [ ...routine.crons, ...(routine.webhookEnabled ? [t("Webhook")] : []), ...(routine.githubEnabled ? [t("Git event")] : []), ...(routine.messageProvider === "slack" ? [t("Slack message")] : routine.messageProvider === "teams" ? [t("Teams message")] : routine.messageProvider ? [t("Message event")] : []), ].join(", "), routine.timezone, ].join(" ยท ")} {t("Prompt")} {routine.prompt} router.push({ pathname: "/thread", params: { botId: botId ?? "", name: botName ?? t("Bot") }, }) } style={{ alignItems: "center", borderRadius: 12, backgroundColor: tokens.primary, padding: 14, }} > {t("Open conversation")} ) : null} ); }