|
| 1 | +import type { MemorySummary } from "@aws-sdk/client-bedrock-agentcore-control"; |
| 2 | +import { useNavigate } from "react-router"; |
| 3 | +import type { ScreenProps } from "../handlers/types"; |
| 4 | +import { coreOptsFromCtx } from "../handlers/utils"; |
| 5 | +import { formatTimestamp } from "./formatTimestamp"; |
| 6 | +import { PaginatedTablePicker } from "./PaginatedTablePicker"; |
| 7 | +import type { DataTableColumn } from "./ui/data-table"; |
| 8 | + |
| 9 | +interface MemoryRow extends Record<string, unknown> { |
| 10 | + memoryId: string; |
| 11 | + status: string; |
| 12 | + updatedAt: string; |
| 13 | +} |
| 14 | + |
| 15 | +export const memoryColumns = [ |
| 16 | + { key: "memoryId", header: "id", flex: true }, |
| 17 | + { key: "status", header: "status", width: 13 }, |
| 18 | + { |
| 19 | + key: "updatedAt", |
| 20 | + header: "updated UTC", |
| 21 | + width: 16, |
| 22 | + render: formatTimestamp, |
| 23 | + }, |
| 24 | +] satisfies DataTableColumn<MemoryRow>[]; |
| 25 | + |
| 26 | +function toRow(memory: MemorySummary): MemoryRow { |
| 27 | + return { |
| 28 | + memoryId: memory.id ?? "", |
| 29 | + status: memory.status ?? "-", |
| 30 | + updatedAt: memory.updatedAt?.toISOString() ?? "-", |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +export interface MemoryPickerProps extends ScreenProps { |
| 35 | + breadcrumb: string[]; |
| 36 | + description?: string; |
| 37 | + onSelect: (memoryId: string) => void; |
| 38 | +} |
| 39 | + |
| 40 | +export function MemoryPicker({ ctx, core, breadcrumb, description, onSelect }: MemoryPickerProps) { |
| 41 | + const opts = coreOptsFromCtx(ctx); |
| 42 | + const navigate = useNavigate(); |
| 43 | + const goBack = () => navigate("/" + breadcrumb.slice(0, -1).join("/")); |
| 44 | + |
| 45 | + return ( |
| 46 | + <PaginatedTablePicker |
| 47 | + breadcrumb={breadcrumb} |
| 48 | + description={description} |
| 49 | + queryKey={["memories", opts.region]} |
| 50 | + loadPage={async (token, pageSize) => { |
| 51 | + const response = await core.memory.listMemories(token, pageSize, opts); |
| 52 | + return { |
| 53 | + items: response.memories ?? [], |
| 54 | + nextToken: response.nextToken, |
| 55 | + }; |
| 56 | + }} |
| 57 | + toRow={toRow} |
| 58 | + columns={memoryColumns} |
| 59 | + getValue={(row) => row.memoryId} |
| 60 | + onSelect={onSelect} |
| 61 | + onBack={goBack} |
| 62 | + loadingMessage="Loading Memories..." |
| 63 | + errorMessage={(error) => `Error: ${error.message}`} |
| 64 | + emptyMessage="No Memories found in this Region." |
| 65 | + emptyPageMessage="No Memories on this page." |
| 66 | + /> |
| 67 | + ); |
| 68 | +} |
0 commit comments