fix(sidebar): improve connection multi-select actions
This commit is contained in:
24 files changed
+510
-44
No files matched your search
Vendored
+67
-3
@@ -8,7 +8,9 @@ const mocks = vi.hoisted(() => ({
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
useI18n: () => ({
|
||||
t: (key: string, params?: Record<string, unknown>) => (params ? `${key}:${JSON.stringify(params)}` : key),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/composables/useToast", () => ({
|
||||
@@ -39,16 +41,17 @@ function connectionStore(selectedTreeNodeIds: string[]) {
|
||||
createConnectionGroup: vi.fn(() => "group-new"),
|
||||
connectedIds: new Set<string>(),
|
||||
connectingIds: new Set<string>(),
|
||||
disconnect: vi.fn().mockResolvedValue(undefined),
|
||||
isTreeNodeChildrenLoaded: vi.fn(() => false),
|
||||
getConfig: vi.fn(() => undefined),
|
||||
};
|
||||
}
|
||||
|
||||
function runtime(activeNode: TreeNode, store: ReturnType<typeof connectionStore>) {
|
||||
function runtime(activeNode: TreeNode, store: ReturnType<typeof connectionStore>, selectedNodes: TreeNode[] = []) {
|
||||
return useSidebarConnectionMutationRuntime({
|
||||
activeNode: shallowRef(activeNode),
|
||||
releaseActiveNodeReference: vi.fn(),
|
||||
selectedTreeNodesInVisibleOrder: () => [],
|
||||
selectedTreeNodesInVisibleOrder: () => selectedNodes,
|
||||
connectionStore: store as any,
|
||||
queryStore: { openDatabaseKeys: new Set<string>() } as any,
|
||||
requestGroupRename: vi.fn(),
|
||||
@@ -127,3 +130,64 @@ describe("sidebar connection move selection", () => {
|
||||
expect(store.connectionMultiSelectActive).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sidebar connection disconnect selection", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
sidebarFormTarget.value = null;
|
||||
});
|
||||
|
||||
it("disconnects every connected connection in the right-clicked selection", async () => {
|
||||
const nodes = [connectionNode("conn-1"), connectionNode("conn-2"), connectionNode("conn-3")];
|
||||
const store = connectionStore(nodes.map((node) => node.id));
|
||||
store.connectedIds = new Set(["conn-1", "conn-3"]);
|
||||
const { disconnectConnection, connectionDisconnectMenuLabel } = runtime(nodes[1], store, nodes);
|
||||
|
||||
expect(connectionDisconnectMenuLabel()).toBe('contextMenu.closeSelectedConnections:{"count":2}');
|
||||
await disconnectConnection();
|
||||
|
||||
expect(store.disconnect.mock.calls).toEqual([["conn-1"], ["conn-3"]]);
|
||||
expect(mocks.toast).toHaveBeenCalledWith('connection.disconnectedSelected:{"count":2}', 2000);
|
||||
});
|
||||
|
||||
it("recomputes disconnect availability when the selected connections change", () => {
|
||||
const nodes = [connectionNode("conn-1"), connectionNode("conn-2")];
|
||||
const selected = [nodes[0]];
|
||||
const store = connectionStore(selected.map((node) => node.id));
|
||||
store.connectedIds = new Set(["conn-2"]);
|
||||
const { canDisconnectConnection, connectionDisconnectMenuLabel } = runtime(nodes[0], store, selected);
|
||||
|
||||
expect(canDisconnectConnection()).toBe(false);
|
||||
|
||||
selected.push(nodes[1]);
|
||||
|
||||
expect(canDisconnectConnection()).toBe(true);
|
||||
expect(connectionDisconnectMenuLabel()).toBe('contextMenu.closeSelectedConnections:{"count":1}');
|
||||
});
|
||||
|
||||
it("continues disconnecting after one selected connection fails", async () => {
|
||||
const nodes = [connectionNode("conn-1"), connectionNode("conn-2")];
|
||||
const store = connectionStore(nodes.map((node) => node.id));
|
||||
store.connectedIds = new Set(nodes.map((node) => node.connectionId!));
|
||||
store.disconnect.mockRejectedValueOnce(new Error("failed")).mockResolvedValueOnce(undefined);
|
||||
const { disconnectConnection } = runtime(nodes[0], store, nodes);
|
||||
|
||||
await disconnectConnection();
|
||||
|
||||
expect(store.disconnect).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.toast).toHaveBeenCalledWith('connection.disconnectSelectedPartial:{"succeeded":1,"failed":1}', 5000);
|
||||
});
|
||||
|
||||
it("disconnects only the right-clicked connection outside the current selection", async () => {
|
||||
const selected = [connectionNode("conn-1"), connectionNode("conn-2")];
|
||||
const current = connectionNode("conn-3");
|
||||
const store = connectionStore(selected.map((node) => node.id));
|
||||
store.connectedIds = new Set(["conn-1", "conn-2", "conn-3"]);
|
||||
const { disconnectConnection } = runtime(current, store, selected);
|
||||
|
||||
await disconnectConnection();
|
||||
|
||||
expect(store.disconnect).toHaveBeenCalledWith("conn-3");
|
||||
expect(store.disconnect).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -14,10 +14,11 @@ import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import { revealPathInFileManager } from "@/lib/backend/tauri";
|
||||
import { canConfigureVisibleSchemasForTreeNode } from "@/lib/database/databaseFeatureSupport";
|
||||
import { canCloseSidebarDatabaseConnection } from "@/lib/sidebar/sidebarDatabaseOpenState";
|
||||
import { selectedConnectionDeleteTargets, selectedConnectionDuplicateTargets } from "@/lib/sidebar/sidebarConnectionSelection";
|
||||
import { selectedConnectionDeleteTargets, selectedConnectionDisconnectTargets, selectedConnectionDuplicateTargets } from "@/lib/sidebar/sidebarConnectionSelection";
|
||||
import { releaseConnectionFromMultiSelection } from "@/lib/sidebar/sidebarConnectionMultiSelect";
|
||||
import { connectionDeleteTargetSnapshot, showDeleteConfirm, showDeleteGroupConfirm, sidebarFormTarget } from "@/components/sidebar/sidebarTreeDialogState";
|
||||
import { connectionCanConfigureSidebarVisibleDatabases } from "@/lib/sidebar/sidebarVisibleFilterMenu";
|
||||
import { disconnectSidebarConnections } from "@/lib/sidebar/sidebarConnectionDisconnect";
|
||||
|
||||
interface SidebarConnectionMutationRuntimeOptions {
|
||||
activeNode: ShallowRef<TreeNode>;
|
||||
@@ -212,16 +213,37 @@ export function useSidebarConnectionMutationRuntime(options: SidebarConnectionMu
|
||||
}
|
||||
|
||||
async function disconnectConnection() {
|
||||
const node = activeNode.value;
|
||||
if (!node.connectionId) return;
|
||||
try {
|
||||
await connectionStore.disconnect(node.connectionId);
|
||||
node.isExpanded = false;
|
||||
node.children = [];
|
||||
toast(t("connection.disconnected"), 2000);
|
||||
} catch (error: any) {
|
||||
toast(t("connection.saveFailed", { message: error?.message || String(error) }), 5000);
|
||||
const targets = selectedConnectionDisconnectTargets(activeNode.value, selectedTreeNodesInVisibleOrder()).filter((target) => connectionStore.connectedIds.has(target.connectionId));
|
||||
if (!targets.length) return;
|
||||
|
||||
const result = await disconnectSidebarConnections(
|
||||
targets.map((target) => target.connectionId),
|
||||
(connectionId) => connectionStore.disconnect(connectionId),
|
||||
);
|
||||
if (!result.failed) {
|
||||
toast(targets.length > 1 ? t("connection.disconnectedSelected", { count: targets.length }) : t("connection.disconnected"), 2000);
|
||||
return;
|
||||
}
|
||||
if (result.succeeded > 0) {
|
||||
toast(t("connection.disconnectSelectedPartial", { succeeded: result.succeeded, failed: result.failed }), 5000);
|
||||
return;
|
||||
}
|
||||
const message = result.firstError instanceof Error ? result.firstError.message : String(result.firstError);
|
||||
toast(t("connection.saveFailed", { message }), 5000);
|
||||
}
|
||||
|
||||
function connectionDisconnectTargets() {
|
||||
return selectedConnectionDisconnectTargets(activeNode.value, selectedTreeNodesInVisibleOrder());
|
||||
}
|
||||
|
||||
function connectionDisconnectMenuLabel(): string {
|
||||
const targets = connectionDisconnectTargets();
|
||||
const connectedCount = targets.filter((target) => connectionStore.connectedIds.has(target.connectionId)).length;
|
||||
return targets.length > 1 ? t("contextMenu.closeSelectedConnections", { count: connectedCount }) : t("contextMenu.closeConnection");
|
||||
}
|
||||
|
||||
function canDisconnectConnection(): boolean {
|
||||
return connectionDisconnectTargets().some((target) => connectionStore.connectedIds.has(target.connectionId));
|
||||
}
|
||||
|
||||
async function cancelConnectionAttempt() {
|
||||
@@ -342,6 +364,8 @@ export function useSidebarConnectionMutationRuntime(options: SidebarConnectionMu
|
||||
canBackupSqliteDatabase,
|
||||
backupSqliteDatabase,
|
||||
disconnectConnection,
|
||||
connectionDisconnectMenuLabel,
|
||||
canDisconnectConnection,
|
||||
cancelConnectionAttempt,
|
||||
closeDatabaseConnection,
|
||||
isPinned,
|
||||
|
||||
Reference in new issue
Block a user