Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions components/Home/HomeRunRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ import { getRunHref } from "@/lib/tasks/getRunHref";
* by `GET /api/tasks/runs` yet, so this shows what is available: run
* name, status, and finished-at (recoupable/chat#1850).
*/
const HomeRunRow = ({ run }: { run: TaskRunItem }) => (
const HomeRunRow = ({
run,
accountId,
}: {
run: TaskRunItem;
accountId?: string;
}) => (
<Link
href={getRunHref(run.id)}
href={getRunHref(run.id, accountId)}
className="group -mx-2 flex items-center justify-between gap-3 rounded-lg px-2 py-2.5 transition-colors hover:bg-muted"
>
<div className="min-w-0">
Expand Down
5 changes: 3 additions & 2 deletions components/Home/TasksModule.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import Link from "next/link";
import { getAccountScopedHref } from "@/lib/tasks/getAccountScopedHref";
import useHomeTasksModuleState from "@/hooks/useHomeTasksModuleState";
import HomeRunRow from "./HomeRunRow";
import StarterTaskCard from "./StarterTaskCard";
Expand Down Expand Up @@ -28,7 +29,7 @@ const TasksModule = () => {
Your label at work
</h2>
<Link
href="/tasks"
href={getAccountScopedHref("/tasks", state.accountId)}
className="text-xs font-medium text-foreground hover:underline"
>
View all
Expand All @@ -37,7 +38,7 @@ const TasksModule = () => {
{state.view === "runs" ? (
<div className="flex flex-col">
{state.runs.map((run) => (
<HomeRunRow key={run.id} run={run} />
<HomeRunRow key={run.id} run={run} accountId={state.accountId} />
))}
</div>
) : state.view === "scheduled" ? (
Expand Down
10 changes: 8 additions & 2 deletions components/TasksPage/RecentRunsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ interface RecentRunsListProps {
runs: TaskRunItem[];
isLoading: boolean;
isError: boolean;
accountId?: string;
}

const RecentRunsList: React.FC<RecentRunsListProps> = ({ runs, isLoading, isError }) => {
const RecentRunsList: React.FC<RecentRunsListProps> = ({
runs,
isLoading,
isError,
accountId,
}) => {
if (isError) {
return (
<div className="text-sm text-red-600 dark:text-red-400">
Expand Down Expand Up @@ -42,7 +48,7 @@ const RecentRunsList: React.FC<RecentRunsListProps> = ({ runs, isLoading, isErro
key={run.id}
className={index !== runs.length - 1 ? "border-b border-border" : ""}
>
<RunCard run={run} />
<RunCard run={run} accountId={accountId} />
</div>
))}
</div>
Expand Down
11 changes: 9 additions & 2 deletions components/TasksPage/Run/RunBreadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from "next/link";
import { getAccountScopedHref } from "@/lib/tasks/getAccountScopedHref";
import {
Breadcrumb,
BreadcrumbList,
Expand All @@ -10,15 +11,21 @@ import {

interface RunBreadcrumbProps {
runId: string;
accountId?: string;
}

export default function RunBreadcrumb({ runId }: RunBreadcrumbProps) {
export default function RunBreadcrumb({
runId,
accountId,
}: RunBreadcrumbProps) {
return (
<Breadcrumb className="px-6 pt-4">
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/tasks?tab=recents">Tasks</Link>
<Link href={getAccountScopedHref("/tasks?tab=recents", accountId)}>
Tasks
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
Expand Down
20 changes: 18 additions & 2 deletions components/TasksPage/Run/RunDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import Link from "next/link";
import { useAccountOverride } from "@/providers/AccountOverrideProvider";
import type { TaskRunStatus } from "@/lib/tasks/getTaskRunStatus";
import { getTaskDisplayName } from "@/lib/tasks/getTaskDisplayName";
import { getRunWorkflowLink } from "@/lib/tasks/getRunWorkflowLink";
Expand All @@ -15,6 +16,7 @@ import AccountIdDisplay from "@/components/ArtistSetting/AccountIdDisplay";
interface RunDetailsProps {
runId: string;
data: TaskRunStatus;
accountId?: string;
}

const openChatClass =
Expand All @@ -25,9 +27,19 @@ const openChatClass =
* work off; status and timeline come from the workflow run linked in its
* metadata, and the transcript is the chat that run wrote to.
*/
export default function RunDetails({ runId, data }: RunDetailsProps) {
export default function RunDetails({
runId,
data,
accountId,
}: RunDetailsProps) {
const selection = useAccountOverride();
const canOpenChat =
accountId === (selection.accountIdOverride ?? undefined) &&
(!selection.email || Boolean(selection.accountIdOverride));
const link = getRunWorkflowLink(data.metadata);
const { data: workflow, isError } = useChatRunStatus(link?.workflowRunId);
const { data: workflow, isError } = useChatRunStatus(link?.workflowRunId, {
accountIdOverride: accountId,
});
const state = getRunPageState({
triggerRun: data,
workflow,
Expand Down Expand Up @@ -68,6 +80,10 @@ export default function RunDetails({ runId, data }: RunDetailsProps) {
No chat recorded for this run. It fired before run pages linked to
their chats, so only the schedule status is available.
</p>
) : !canOpenChat ? (
<p role="status" className="text-sm text-muted-foreground">
Select the matching workspace account before opening this chat.
</p>
) : (
<Link href={state.chatHref} className={openChatClass}>
Open chat
Expand Down
24 changes: 20 additions & 4 deletions components/TasksPage/Run/RunPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use client";

import { useSearchParams } from "next/navigation";
import RunAccountNotice from "../RunAccountNotice";
import { XCircle } from "lucide-react";
import { useTaskRunStatus } from "@/hooks/useTaskRunStatus";
import RunBreadcrumb from "./RunBreadcrumb";
Expand All @@ -12,11 +14,18 @@ interface RunPageProps {
}

export default function RunPage({ runId }: RunPageProps) {
const { data, isPending, error } = useTaskRunStatus(runId);
const searchParams = useSearchParams();
const accountIdOverride = searchParams.get("account_id") ?? undefined;
const { data, isPending, error, scopeReady, accountId } = useTaskRunStatus(
runId,
{ accountIdOverride },
);

let content;

if (isPending) {
if (scopeReady === false) {
content = null;
} else if (isPending) {
content = <RunPageSkeleton />;
} else if (error || !data) {
content = (
Expand All @@ -28,12 +37,19 @@ export default function RunPage({ runId }: RunPageProps) {
</div>
);
} else {
content = <RunDetails runId={runId} data={data} />;
content = <RunDetails runId={runId} data={data} accountId={accountId} />;
}

return (
<PageContainer className="h-screen">
<RunBreadcrumb runId={runId} />
<RunBreadcrumb runId={runId} accountId={accountIdOverride ?? accountId} />
<div className="px-6 pt-4">
<RunAccountNotice
scopeReady={scopeReady}
accountId={accountId}
explicit={accountIdOverride !== undefined}
/>
</div>
{content}
</PageContainer>
);
Expand Down
119 changes: 119 additions & 0 deletions components/TasksPage/Run/__tests__/RunNavigation.scope.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// @vitest-environment jsdom
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import { expect, it, vi } from "vitest";
import {
context,
fetchMock,
wrapper,
response,
} from "@/hooks/__tests__/taskRunScope.fixture";
import RecentRunsList from "@/components/TasksPage/RecentRunsList";
import RunPage from "../RunPage";
import RunDetails from "../RunDetails";
import HomeRunRow from "@/components/Home/HomeRunRow";
import type { TaskRunItem } from "@/lib/tasks/getTaskRuns";
import type { TaskRunStatus } from "@/lib/tasks/getTaskRunStatus";
const route = vi.hoisted(() => ({ query: "account_id=target-b" }));
vi.mock("next/navigation", () => ({
useSearchParams: () => new URLSearchParams(route.query),
}));
vi.mock("next/link", () => ({
default: ({
href,
children,
...props
}: React.AnchorHTMLAttributes<HTMLAnchorElement>) => (
<a href={href} {...props}>
{children}
</a>
),
}));
const run = {
id: "run-a",
status: "COMPLETED",
taskIdentifier: "scheduled-task",
createdAt: "2026-10-25T12:00:00Z",
startedAt: null,
finishedAt: null,
durationMs: null,
} satisfies TaskRunItem;
const detail = {
...run,
metadata: {
workflowRunId: "workflow-a",
sessionId: "session-a",
chatId: "chat-a",
},
} satisfies TaskRunStatus;

it("retains the list target through detail and the back link despite a conflicting global selection", async () => {
context.account = "global-c";
const list = render(
<RecentRunsList
runs={[run]}
accountId="target-b"
isLoading={false}
isError={false}
/>,
);
const href = screen.getByRole("link").getAttribute("href")!;
expect(href).toBe("/runs/run-a?account_id=target-b");
route.query = href.split("?")[1];
list.unmount();
fetchMock.mockResolvedValue(response());
render(<RunPage runId="run-a" />, { wrapper });
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
expect(
new URL(fetchMock.mock.calls[0][0]).searchParams.get("account_id"),
).toBe("target-b");
expect(screen.getByRole("link", { name: "Tasks" }).getAttribute("href")).toBe(
"/tasks?tab=recents&account_id=target-b",
);
expect(screen.getByText(/Runs for account target-b/).textContent).toContain(
"takes precedence",
);
});
it("homepage run rows preserve the fetched target", () => {
render(<HomeRunRow run={run} accountId="target-b" />);
expect(screen.getByRole("link").getAttribute("href")).toBe(
"/runs/run-a?account_id=target-b",
);
});
it("unresolved detail selection is not an infinite skeleton or empty history", () => {
route.query = "";
context.email = "pending@example.test";
render(<RunPage runId="run-a" />, { wrapper });
expect(screen.getByRole("status").textContent).toContain("resolve or clear");
expect(fetchMock).not.toHaveBeenCalled();
});
it.each(["self", "matching", "conflicting", "unresolved"] as const)(
"keeps workflow metadata but opens chat only with a %s compatible selection",
async (mode) => {
context.account =
mode === "matching"
? "target-b"
: mode === "conflicting"
? "global-c"
: null;
context.email = mode === "unresolved" ? "pending@example.test" : null;
const accountId = mode === "self" ? undefined : "target-b";
render(<RunDetails runId="run-a" data={detail} accountId={accountId} />, {
wrapper,
});
await waitFor(() => expect(screen.getByText("Completed")).toBeTruthy());
expect(String(fetchMock.mock.calls[0][0])).toContain(
"/api/chat/runs/workflow-a",
);
if (mode === "self" || mode === "matching")
expect(
screen.getByRole("link", { name: "Open chat" }).getAttribute("href"),
).toBe("/sessions/session-a/chats/chat-a");
else {
expect(screen.queryByRole("link", { name: "Open chat" })).toBeNull();
expect(
screen.getByText(/Select the matching workspace account/),
).toBeTruthy();
}
},
);
14 changes: 13 additions & 1 deletion components/TasksPage/Run/__tests__/RunPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import RunPage from "@/components/TasksPage/Run/RunPage";

vi.mock("next/navigation", () => ({
useSearchParams: () => new URLSearchParams(),
}));

const runQuery: {
data: { status: string } | undefined;
isPending: boolean;
isLoading: boolean;
error: Error | null;
} = { data: undefined, isPending: true, isLoading: false, error: null };
scopeReady: boolean;
} = {
data: undefined,
isPending: true,
isLoading: false,
error: null,
scopeReady: true,
};
vi.mock("@/hooks/useTaskRunStatus", () => ({
useTaskRunStatus: () => runQuery,
}));
Expand All @@ -30,6 +41,7 @@ describe("RunPage", () => {
isPending: true,
isLoading: false,
error: null,
scopeReady: true,
});
});

Expand Down
26 changes: 26 additions & 0 deletions components/TasksPage/RunAccountNotice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface RunAccountNoticeProps {
scopeReady: boolean;
accountId?: string;
explicit?: boolean;
}

export default function RunAccountNotice({
scopeReady,
accountId,
explicit,
}: RunAccountNoticeProps) {
if (!scopeReady)
return (
<p role="status" className="text-sm text-muted-foreground">
Sign in and resolve or clear the selected account to view runs.
</p>
);
if (!accountId) return null;
return (
<p className="text-sm text-muted-foreground break-all">
Runs for account {accountId}.
{explicit &&
" The account in this link takes precedence over the workspace selection."}
</p>
);
}
5 changes: 3 additions & 2 deletions components/TasksPage/RunCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import AccountIdDisplay from "@/components/ArtistSetting/AccountIdDisplay";

interface RunCardProps {
run: TaskRunItem;
accountId?: string;
}

const RunCard: React.FC<RunCardProps> = ({ run }) => {
const RunCard: React.FC<RunCardProps> = ({ run, accountId }) => {
const duration = formatDuration(run.durationMs);

return (
<Link
href={getRunHref(run.id)}
href={getRunHref(run.id, accountId)}
className="group flex items-center justify-between py-4 px-4 hover:bg-muted dark:hover:bg-[#1a1a1a] transition-colors -mx-4 cursor-pointer"
>
<div className="flex items-center space-x-4">
Expand Down
Loading
Loading