Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Resources: nothing is read again for a deleted item, and packages of one name both show

Simon Flack4be1b2aaafe0
diff --git a/console/apps/switch-console-desktop/src/renderer/features/library/library-item-view.tsx b/console/apps/switch-console-desktop/src/renderer/features/library/library-item-view.tsx
index fb487f979..6e5cd3758 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/library/library-item-view.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/library/library-item-view.tsx
@@ -597,19 +597,21 @@ function PackagesLine({ workspaceId, item }: { workspaceId: string; item: Librar
queryKey: ['library', workspaceId, 'packages'],
queryFn: () => rpc.workspaces.listLibrary({ workspaceId, kind: 'packages' }),
});
- const names = item.packageIds.map((id) => packages.data?.find((p) => p.id === id)?.name ?? null);
- const hidden = names.filter((name) => name === null).length;
+ // Keyed by id: two packages may share a name.
+ const named = item.packageIds.flatMap((id) => {
+ const name = packages.data?.find((p) => p.id === id)?.name;
+ return name === undefined ? [] : [{ id, name }];
+ });
+ const hidden = item.packageIds.length - named.length;
return (
<Section title="In packages">
<div className="flex flex-wrap items-center gap-1.5">
- {names
- .filter((name): name is string => name !== null)
- .map((name) => (
- <Badge key={name} variant="outline">
- <Package className="size-3" />
- {name}
- </Badge>
- ))}
+ {named.map((pkg) => (
+ <Badge key={pkg.id} variant="outline">
+ <Package className="size-3" />
+ {pkg.name}
+ </Badge>
+ ))}
{hidden > 0 && !packages.isPending && (
<span className="text-xs text-foreground-muted">
{hidden === 1 ? 'and 1 you cannot see' : `and ${hidden} you cannot see`}
diff --git a/console/apps/switch-console-desktop/src/renderer/features/library/library-modals.tsx b/console/apps/switch-console-desktop/src/renderer/features/library/library-modals.tsx
index 7f2fac49b..71ddea174 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/library/library-modals.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/library/library-modals.tsx
@@ -18,7 +18,12 @@ import type { LibraryItem, LibraryKind } from '@shared/core/switch-servers/libra
import { emptyForm, formProblem, inputOf, type LibraryForm } from './library-form';
import { LibraryItemFields } from './library-item-form';
import { hiddenRoomsText, KIND_TEXT } from './library-model';
-import { invalidateLibrary, libraryRoomsKey, referenceTypesKey } from './library-queries';
+import {
+ forgetLibraryItem,
+ invalidateLibrary,
+ libraryRoomsKey,
+ referenceTypesKey,
+} from './library-queries';
type NewProps = BaseModalProps<LibraryItem> & { workspaceId: string; kind: LibraryKind };
@@ -136,9 +141,11 @@ export function DeleteLibraryItemModal({ workspaceId, item, onSuccess, onClose }
setError(null);
try {
await rpc.workspaces.deleteLibraryItem({ workspaceId, kind: item.kind, itemId: item.id });
- await invalidateLibrary(queryClient, workspaceId, item.kind);
toast({ title: `"${item.name}" deleted` });
+ // Leave first (closing this and, from its page, the page itself), then
+ // drop what was read for the item rather than reading it again: it is gone.
onSuccess();
+ await forgetLibraryItem(queryClient, workspaceId, item.kind, item.id);
} catch (e) {
setError(failureText(e, `Could not delete the ${noun}.`));
setDeleting(false);
diff --git a/console/apps/switch-console-desktop/src/renderer/features/library/library-queries.ts b/console/apps/switch-console-desktop/src/renderer/features/library/library-queries.ts
index 19483a4f5..45e9fe94f 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/library/library-queries.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/library/library-queries.ts
@@ -40,3 +40,22 @@ export async function invalidateLibrary(
queryClient.invalidateQueries({ queryKey: ['room-resources', workspaceId] }),
]);
}
+
+/**
+ * A library item was deleted: what was read for it is dropped rather than
+ * read again (it would only fail), and what lists items reads again.
+ */
+export async function forgetLibraryItem(
+ queryClient: QueryClient,
+ workspaceId: string,
+ kind: LibraryKind,
+ itemId: string
+): Promise<void> {
+ queryClient.removeQueries({ queryKey: libraryItemKey(workspaceId, kind, itemId), exact: true });
+ queryClient.removeQueries({ queryKey: libraryRoomsKey(workspaceId, kind, itemId), exact: true });
+ await Promise.all([
+ queryClient.invalidateQueries({ queryKey: ['library-items', workspaceId] }),
+ queryClient.invalidateQueries({ queryKey: ['library', workspaceId] }),
+ queryClient.invalidateQueries({ queryKey: ['room-resources', workspaceId] }),
+ ]);
+}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/library-view.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/library-view.test.tsx
index 59fa40495..828bd9ab7 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/library-view.test.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/library-view.test.tsx
@@ -32,6 +32,7 @@ const rpcMock = vi.hoisted(() => ({
listLibraryItemRooms: vi.fn(),
listReferenceTypes: vi.fn(),
listLibrary: vi.fn(),
+ deleteLibraryItem: vi.fn(),
setRoomResourceAttached: vi.fn(),
}));
const showModal = vi.hoisted(() => vi.fn());
@@ -143,6 +144,7 @@ beforeEach(() => {
},
]);
rpcMock.listLibrary.mockResolvedValue([]);
+ rpcMock.deleteLibraryItem.mockResolvedValue({ detachedRoomIds: [], affectedPackageIds: [] });
rpcMock.updateLibraryItem.mockResolvedValue({});
rpcMock.setRoomResourceAttached.mockResolvedValue(undefined);
showModal.mockReset();
@@ -351,6 +353,17 @@ describe('an item’s page', () => {
expect(container.querySelector('[aria-label="Detach from support"]')).not.toBeNull();
});
+ it('shows two packages of the same name as two', async () => {
+ state.item = item({ packageIds: ['pkg-a', 'pkg-b'] });
+ rpcMock.listLibrary.mockResolvedValue([
+ { id: 'pkg-a', name: 'Onboarding' },
+ { id: 'pkg-b', name: 'Onboarding' },
+ ]);
+ await render(libraryItemView.MainPanel);
+ const section = container.querySelector('[aria-label="In packages"]')!;
+ expect(section.textContent?.match(/Onboarding/g)).toHaveLength(2);
+ });
+
it('renders a document’s content as markdown', async () => {
state.params = { serverId: 'srv', kind: 'documents', itemId: 'doc-1' };
state.item = {
@@ -387,6 +400,52 @@ describe('deleting', () => {
expect(dialogText()).toContain('and 2 private rooms you can’t open');
});
+ it('leaves, then reads nothing more about the item it deleted', async () => {
+ state.params = { serverId: 'srv', kind: 'references', itemId: 'ref-1' };
+ state.item = item({});
+ await render(libraryItemView.MainPanel);
+ await act(async () => button('Delete')!.click());
+ const args = showModal.mock.calls.at(-1)![1] as { onSuccess: () => void };
+ await act(async () => root.unmount());
+ container.remove();
+ const onSuccess = vi.fn(args.onSuccess);
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ // The page and the dialog, as the app shows them, sharing one cache.
+ const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
+ const Panel = libraryItemView.MainPanel;
+ await act(async () =>
+ root.render(
+ <QueryClientProvider client={client}>
+ <Panel />
+ <Dialog open>
+ <DialogContent>
+ <DeleteLibraryItemModal
+ workspaceId="ws"
+ item={item({})}
+ onSuccess={onSuccess}
+ onClose={() => {}}
+ />
+ </DialogContent>
+ </Dialog>
+ </QueryClientProvider>
+ )
+ );
+ await settle();
+ rpcMock.getLibraryItem.mockClear();
+ rpcMock.listLibraryItemRooms.mockClear();
+ const confirm = [
+ ...document.querySelectorAll<HTMLButtonElement>('[data-slot="dialog-content"] button'),
+ ].find((b) => b.textContent === 'Delete')!;
+ await act(async () => confirm.click());
+ await settle();
+ expect(rpcMock.deleteLibraryItem).toHaveBeenCalled();
+ expect(onSuccess).toHaveBeenCalled();
+ expect(rpcMock.getLibraryItem).not.toHaveBeenCalled();
+ expect(rpcMock.listLibraryItemRooms).not.toHaveBeenCalled();
+ });
+
it('counts the rooms you cannot open when they are all it is in', async () => {
state.rooms = [];
await renderDelete({ hiddenRoomsCount: 1 });