import { useEffect, useState } from "react"; import { useApp } from "../state/store"; import { api } from "../lib/api"; import { wizardApi } from "../lib/wizardApi"; import { Branch, Layers, Pulse } from "../components/icons"; export type HubKey = "procurement" | "hr" | "it"; interface HubSpec { title: string; eyebrow: string; intro: string; match: RegExp; quickActions: { label: string; subject: string; processHint: string }[]; } const HUBS: Record = { procurement: { title: "Procurement Hub", eyebrow: "Purchase requests, vendor approvals, three-way match", intro: "Everything a store manager needs to buy something — from a laptop to a forklift — and watch it move through the approval chain.", match: /procure|purchase|vendor|\bpo\b|\bp2p\b|requisition|payment|invoice/i, quickActions: [ { label: "Request a new laptop", subject: "store-204-laptop", processHint: "procurement" }, { label: "Submit a quote for review", subject: "vendor-quote-Q3", processHint: "procurement" }, ], }, hr: { title: "People Hub", eyebrow: "Hiring, onboarding, leave, performance", intro: "All people movements run here. Start an onboarding for a new hire, file a leave request, or kick off a review cycle.", match: /onboard|offboard|leave|\bhr\b|hiring|payroll|employee|people operations/i, quickActions: [ { label: "Onboard a new hire", subject: "new-hire-2026Q3", processHint: "onboard" }, { label: "Request annual leave", subject: "leave-7d-may", processHint: "leave" }, ], }, it: { title: "IT Hub", eyebrow: "Tickets, access requests, incident response", intro: "When something breaks, request access, or you need a new account — file it here and route it to the right on-call.", match: /\bit\b|ticket|incident|service operations|access request|sap access|laptop request|hardware request|software request|password reset|account provisioning/i, quickActions: [ { label: "Open an incident", subject: "incident-store-204", processHint: "service" }, { label: "Request system access", subject: "access-sap-RW", processHint: "access" }, ], }, }; interface PublishedFlow { _key: string; display_name: string; description?: string; name?: string; } const NON_BUSINESS_SOURCE_CONTEXTS = new Set([ "EA2_CHAT_THREAD", "EA2_CHAT_MSG", "EA2_WIZARD_STEP", ]); async function loadPublishedFlows(): Promise { const res = await fetch(`${api.config.baseUrl}/api/ea2/flow/processes?limit=200`, { headers: { Authorization: `Bearer ${sessionStorage.getItem("fm.mc.token.v1")}` }, }); if (!res.ok) throw new Error(`processes ${res.status}`); const body = await res.json(); return ((body?.items || []) as any[]) .filter( (it) => it.status === "published" && it.kind === "definition" && it.display_name && !NON_BUSINESS_SOURCE_CONTEXTS.has(it.source_context) ) .map((it) => ({ _key: it._key, display_name: it.display_name, description: it.description, name: it.name, })); } export default function Hub({ hub }: { hub: HubKey }) { const spec = HUBS[hub]; const pushToast = useApp((s) => s.pushToast); const setScene = useApp((s) => s.setScene); const [flows, setFlows] = useState(null); const [busy, setBusy] = useState(null); useEffect(() => { let cancelled = false; loadPublishedFlows() .then((rows) => !cancelled && setFlows(rows)) .catch((err) => !cancelled && pushToast("err", `Catalogue failed: ${err.message}`)); return () => { cancelled = true; }; }, [hub]); const matching = (flows || []).filter((f) => spec.match.test(`${f.display_name} ${f.description || ""} ${f.name || ""}`)).slice(0, 8); const fallback = (flows || []).slice(0, 6); const visible = matching.length > 0 ? matching : fallback; const startInstance = async (flowKey: string, subject: string, label: string) => { setBusy(flowKey); try { const res = await wizardApi.startInstance(flowKey, subject); pushToast("ok", `Started ${label}${subject ? ` for ${subject}` : ""} — ${res.transaction_id?.slice(0, 8) || "ok"}`); setScene("mission"); } catch (err: any) { pushToast("err", `Start failed: ${err.message}`); } finally { setBusy(null); } }; const tryQuickAction = async (label: string, subject: string, processHint: string) => { if (!flows) return; const match = flows.find((f) => new RegExp(processHint, "i").test(`${f.display_name} ${f.description || ""} ${f.name || ""}`) ); if (!match) { pushToast("err", `No published process matching "${processHint}". Build one in Studio.`); return; } await startInstance(match._key, subject, label); }; return (
{spec.eyebrow}

{spec.title}

{spec.intro}

QUICK ACTIONS
{spec.quickActions.map((qa) => ( ))}
RELATED PROCESSES
{flows === null &&
Loading catalogue…
} {flows !== null && visible.length === 0 && (
No matching published processes yet. Open the to build one.
)}
{visible.map((f) => (
{f.display_name}
{f.description &&
{f.description.slice(0, 160)}
}
))}
); }