From 7484cfe8a91b3043c9d3ca842de13966a6e91aa5 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Thu, 7 Aug 2025 15:52:08 +0000 Subject: [PATCH] refactor(site): remove usage of `throw` in `TaskApps.tsx` --- site/src/pages/TaskPage/TaskApps.tsx | 57 ++++++++++++++-------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/site/src/pages/TaskPage/TaskApps.tsx b/site/src/pages/TaskPage/TaskApps.tsx index 1fd31bd3b1481..83cd01f37c004 100644 --- a/site/src/pages/TaskPage/TaskApps.tsx +++ b/site/src/pages/TaskPage/TaskApps.tsx @@ -23,6 +23,11 @@ type TaskAppsProps = { task: Task; }; +type AppWithAgent = { + app: WorkspaceApp; + agent: WorkspaceAgent; +}; + export const TaskApps: FC = ({ task }) => { const agents = task.workspace.latest_build.resources .flatMap((r) => r.agents) @@ -31,27 +36,34 @@ export const TaskApps: FC = ({ task }) => { // The Chat UI app will be displayed in the sidebar, so we don't want to show // it here const apps = agents - .flatMap((a) => a?.apps) + .flatMap((agent) => + agent.apps.map((app) => ({ + app, + agent, + })), + ) .filter( - (a) => !!a && a.id !== task.workspace.latest_build.ai_task_sidebar_app_id, + ({ app }) => + !!app && app.id !== task.workspace.latest_build.ai_task_sidebar_app_id, ); - const embeddedApps = apps.filter((app) => !app.external); - const externalApps = apps.filter((app) => app.external); + const embeddedApps = apps.filter(({ app }) => !app.external); + const externalApps = apps.filter(({ app }) => app.external); const [activeAppId, setActiveAppId] = useState( - embeddedApps[0]?.id, + embeddedApps[0]?.app.id, ); return (
- {embeddedApps.map((app) => ( + {embeddedApps.map(({ app, agent }) => ( { e.preventDefault(); @@ -72,7 +84,7 @@ export const TaskApps: FC = ({ task }) => { {embeddedApps.length > 0 ? (
- {embeddedApps.map((app) => { + {embeddedApps.map(({ app }) => { return ( = ({ task }) => { type TaskExternalAppsDropdownProps = { task: Task; agents: WorkspaceAgent[]; - externalApps: WorkspaceApp[]; + externalApps: AppWithAgent[]; }; const TaskExternalAppsDropdown: FC = ({ @@ -126,16 +138,7 @@ const TaskExternalAppsDropdown: FC = ({ - {externalApps.map((app) => { - const agent = agents.find((agent) => - agent.apps.some((a) => a.id === app.id), - ); - if (!agent) { - throw new Error( - `Agent for app ${app.id} not found in task workspace`, - ); - } - + {externalApps.map(({ app, agent }) => { const link = useAppLink(app, { agent, workspace: task.workspace, @@ -163,20 +166,18 @@ const TaskExternalAppsDropdown: FC = ({ type TaskAppTabProps = { task: Task; app: WorkspaceApp; + agent: WorkspaceAgent; active: boolean; onClick: (e: React.MouseEvent) => void; }; -const TaskAppTab: FC = ({ task, app, active, onClick }) => { - const agent = task.workspace.latest_build.resources - .flatMap((r) => r.agents) - .filter((a) => !!a) - .find((a) => a.apps.some((a) => a.id === app.id)); - - if (!agent) { - throw new Error(`Agent for app ${app.id} not found in task workspace`); - } - +const TaskAppTab: FC = ({ + task, + app, + agent, + active, + onClick, +}) => { const link = useAppLink(app, { agent, workspace: task.workspace,