Skip to content
Closed
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
23 changes: 23 additions & 0 deletions src/components/PaginatedTablePicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ describe("paginated table picker contract", () => {
).toBe(false);
});

test("restores the selected row after returning from its detail screen", async () => {
const selected = harness({ harnessName: "third-harness", harnessId: "third-harness" });
const core = coreWith([
harness({ harnessName: "first-harness", harnessId: "first-harness" }),
harness({ harnessName: "second-harness", harnessId: "second-harness" }),
selected,
]);
core.harness.setGetResponse(getResponse(selected));
const r = renderScreen("/agentcore/harness/list", { core });

await waitForText(r.lastFrame, "third-harness");
await r.press("down");
await r.press("down");
await waitForText(r.lastFrame, "❯ third-harness");
await r.press("return");
await waitForText(r.lastFrame, "agentcore → harness → get → third-harness");

await r.press("escape");
await waitForText(r.lastFrame, "updated UTC");
expect(r.lastFrame()).toContain("❯ third-harness");
expect(r.lastFrame()).not.toContain("❯ first-harness");
});

test("retains rows and disables selection and paging during a transition", async () => {
const core = new TestCoreClient();
core.harness.setListResponse({
Expand Down
32 changes: 31 additions & 1 deletion src/components/PaginatedTablePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { Text, useInput } from "ink";
import { useLocation, useNavigate } from "react-router";
import { Layout } from "./Layout";
import { usePagedList } from "./usePagedList";
import { darkTheme } from "./ui/_core.js";
Expand All @@ -11,6 +12,10 @@ export interface TokenPage<TItem> {
nextToken?: string;
}

interface PaginatedTableLocationState extends Record<string, unknown> {
paginatedTableSelection?: string;
}

export interface PaginatedTablePickerProps<TItem, TRow extends Record<string, unknown>> {
breadcrumb: string[];
description?: string;
Expand Down Expand Up @@ -46,6 +51,8 @@ export function PaginatedTablePicker<TItem, TRow extends Record<string, unknown>
emptyPageMessage,
maxPageSize,
}: PaginatedTablePickerProps<TItem, TRow>) {
const location = useLocation();
const navigate = useNavigate();
const paging = usePagedList(maxPageSize);
const list = useQuery({
queryKey: [...queryKey, paging.pageSize, paging.token],
Expand All @@ -57,6 +64,16 @@ export function PaginatedTablePicker<TItem, TRow extends Record<string, unknown>
const pageTransition = list.isFetching && !list.isPending;
const mappedRows = (list.data?.items ?? []).map(toRow);
const rows = sortRows ? sortRows(mappedRows) : mappedRows;
const locationState =
location.state && typeof location.state === "object"
? (location.state as PaginatedTableLocationState)
: {};
const initialSelectedRow = locationState.paginatedTableSelection
? Math.max(
0,
rows.findIndex((row) => getValue(row) === locationState.paginatedTableSelection),
)
: 0;

useInput(
(input, key) => {
Expand Down Expand Up @@ -106,14 +123,27 @@ export function PaginatedTablePicker<TItem, TRow extends Record<string, unknown>
showFooter={false}
showDivider={true}
pageSize={paging.pageSize}
initialSelectedRow={initialSelectedRow}
selectionResetKey={paging.pageSize}
focus={!pageTransition}
columns={columns}
data={rows}
emptyMessage={paginated ? emptyPageMessage : emptyMessage}
onSelect={(row) => {
const value = getValue(row);
if (value) onSelect(value);
if (!value) return;
navigate(
{
pathname: location.pathname,
search: location.search,
hash: location.hash,
},
{
replace: true,
state: { ...locationState, paginatedTableSelection: value },
},
);
onSelect(value);
}}
onEscape={onBack}
onPrevPage={!pageTransition && paging.pageIndex > 0 ? paging.prev : undefined}
Expand Down
9 changes: 7 additions & 2 deletions src/components/ui/data-table/DataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import cliTruncate from "cli-truncate";
import { Box, Text, useInput, useWindowSize } from "ink";
import stringWidth from "string-width";
Expand Down Expand Up @@ -47,6 +47,7 @@ export interface DataTableProps<T> {
showFooter?: boolean;
emptyMessage?: string;
focus?: boolean;
initialSelectedRow?: number;
selectionResetKey?: string | number;
theme?: InkUITheme;
}
Expand All @@ -70,16 +71,20 @@ export function DataTable<T extends Record<string, unknown>>({
showFooter = true,
emptyMessage = "No data",
focus = true,
initialSelectedRow = 0,
selectionResetKey,
theme = darkTheme,
}: DataTableProps<T>): React.ReactElement {
const { columns: terminalWidth } = useWindowSize();
const [selectedRow, setSelectedRow] = useState(0);
const [selectedRow, setSelectedRow] = useState(() => Math.max(0, initialSelectedRow));
const [currentPage, setCurrentPage] = useState(0);
const [searchQuery, setSearchQuery] = useState("");
const [searchMode, setSearchMode] = useState(false);
const selectionResetKeyRef = useRef(selectionResetKey);

useEffect(() => {
if (selectionResetKeyRef.current === selectionResetKey) return;
selectionResetKeyRef.current = selectionResetKey;
setSelectedRow(0);
setCurrentPage(0);
}, [selectionResetKey]);
Expand Down
24 changes: 24 additions & 0 deletions src/handlers/memory/event/event.screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ describe("Memory event list flow", () => {
});
});

test("restores the selected Event after returning from its JSON", async () => {
const selectedEvent = event({ eventId: "event-3" });
const core = new TestCoreClient();
core.memory.setListEventsResponse({
events: [event({ eventId: "event-1" }), event({ eventId: "event-2" }), selectedEvent],
});
core.memory.setGetEventResponse({ event: selectedEvent });
const screen = renderScreen("/agentcore/memory/event/list/memory-1/actor-1/session-1", {
core,
});

await waitForText(screen.lastFrame, "event-3");
await screen.press("down");
await screen.press("down");
await waitForText(screen.lastFrame, "❯ event-3");
await screen.press("return");
await waitForText(screen.lastFrame, '"eventId": "event-3"');

await screen.press("escape");
await waitForText(screen.lastFrame, "occurred UTC");
expect(screen.lastFrame()).toContain("❯ event-3");
expect(screen.lastFrame()).not.toContain("❯ event-1");
});

test("paginates events and distinguishes a later-page empty state", async () => {
const core = new TestCoreClient();
core.memory.setListEventsResponse({
Expand Down
Loading