refactor(desktop): organize lib modules by domain

This commit is contained in:
t8y2 committed 2026-07-04 13:53:52 +08:00
1 parent 90f0594e93
commit 7e7c4944e9
700 files changed
+1292 -1264

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
export const APP_FONT_SANS_CSS_VAR = "--font-sans";
export const DEFAULT_UI_FONT_FAMILY = `"Geist Variable", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif`;
// Native-feeling UI option without DBX's bundled/brand font at the front of the stack.
export const SYSTEM_UI_FONT_FAMILY = `system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
+20
View File
@@ -0,0 +1,20 @@
import type { Theme } from "@tauri-apps/api/window";
export const APP_THEME_STORAGE_KEY = "dbx-theme";
export type AppThemeMode = "light" | "dark" | "system";
export type AppThemeAppearance = "light" | "dark";
export function normalizeAppThemeMode(value: string | null): AppThemeMode {
if (value === "dark" || value === "light" || value === "system") return value;
return "light";
}
export function resolveAppThemeAppearance(mode: AppThemeMode, systemPrefersDark: boolean): AppThemeAppearance {
if (mode === "system") return systemPrefersDark ? "dark" : "light";
return mode;
}
export function getTauriThemeForMode(mode: AppThemeMode): Theme | null {
return mode === "system" ? null : mode;
}
@@ -0,0 +1,376 @@
import type { ComposerTranslation } from "vue-i18n";
import type { DatabaseType } from "@/types/database";
import { quoteUnquotedObjectKeys } from "@/lib/mongo/mongoShellCommand";
import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues";
export type DocumentStoreKind = "mongodb" | "elasticsearch";
export type DocumentFilterMode = "equals" | "not-equals" | "like" | "not-like" | "greater-than" | "less-than" | "is-null" | "is-not-null";
export type DocumentFilterRule = {
id: string;
fieldName: string;
mode: DocumentFilterMode;
rawValue: string;
conjunction: "AND" | "OR";
};
export type DocumentStoreQueryPreviewOptions = {
collection: string;
filterJson?: string;
sortJson?: string;
skip: number;
limit: number;
};
export type DocumentStoreProvider = {
kind: DocumentStoreKind;
filterInputLabel: string;
sortInputLabel: string;
documentsLabel(options: { total: number; t: ComposerTranslation }): string;
queryPreview(options: DocumentStoreQueryPreviewOptions): string;
sortInputForColumn(column: string, direction: "asc" | "desc" | null): string;
};
export const documentFilterModeOptions: Array<{ value: DocumentFilterMode; labelKey: string }> = [
{ value: "equals", labelKey: "grid.filterBuilderEquals" },
{ value: "not-equals", labelKey: "grid.filterBuilderNotEquals" },
{ value: "like", labelKey: "grid.filterBuilderContains" },
{ value: "not-like", labelKey: "grid.filterBuilderNotContains" },
{ value: "greater-than", labelKey: "grid.filterBuilderGreaterThan" },
{ value: "less-than", labelKey: "grid.filterBuilderLessThan" },
{ value: "is-null", labelKey: "grid.filterBuilderIsNull" },
{ value: "is-not-null", labelKey: "grid.filterBuilderIsNotNull" },
];
const mongoDocumentProvider: DocumentStoreProvider = {
kind: "mongodb",
filterInputLabel: "find",
sortInputLabel: "sort",
documentsLabel: ({ total, t }) => t("mongo.documents", { count: total }),
queryPreview: ({ collection, filterJson, sortJson, skip, limit }) => {
const collectionRef = `db.getCollection(${JSON.stringify(collection)})`;
const parts = [`${collectionRef}.find(${mongoShellPreviewLiteral(filterJson || "{}")})`];
if (sortJson?.trim()) parts.push(`.sort(${mongoShellPreviewLiteral(sortJson)})`);
parts.push(`.skip(${skip}).limit(${limit})`);
return parts.join("");
},
sortInputForColumn: (column, direction) => (direction ? JSON.stringify({ [column]: direction === "asc" ? 1 : -1 }) : ""),
};
function mongoShellPreviewLiteral(json: string): string {
const trimmed = json.trim();
if (!trimmed) return "{}";
try {
return formatMongoShellLiteral(JSON.parse(trimmed));
} catch {
return trimmed;
}
}
const elasticsearchDocumentProvider: DocumentStoreProvider = {
kind: "elasticsearch",
filterInputLabel: "filter",
sortInputLabel: "sort",
documentsLabel: () => "Documents",
queryPreview: ({ collection, filterJson, sortJson, skip, limit }) => {
const body = safeElasticsearchSearchBodyFromDocumentQuery({ filterJson, sortJson, skip, limit });
return `POST /${collection}/_search\n${JSON.stringify(body, null, 2)}`;
},
sortInputForColumn: mongoDocumentProvider.sortInputForColumn,
};
export function documentStoreProviderFor(databaseType?: DatabaseType): DocumentStoreProvider {
return databaseType === "elasticsearch" ? elasticsearchDocumentProvider : mongoDocumentProvider;
}
export function defaultDocumentFilterRule(id: string, fieldName = ""): DocumentFilterRule {
return {
id,
fieldName,
mode: "equals",
rawValue: "",
conjunction: "AND",
};
}
export function documentFilterModeNeedsValue(mode: DocumentFilterMode): boolean {
return mode !== "is-null" && mode !== "is-not-null";
}
type DocumentFilterParseOptions = {
kind?: DocumentStoreKind;
};
export function buildDocumentFilterCondition(rule: DocumentFilterRule, options: DocumentFilterParseOptions = {}): Record<string, unknown> | null {
if (!rule.fieldName) return null;
if (documentFilterModeNeedsValue(rule.mode) && !rule.rawValue.trim()) return null;
const value = documentFilterModeNeedsValue(rule.mode) ? parseDocumentFilterValue(rule.rawValue, options) : null;
const textValue = documentFilterModeNeedsValue(rule.mode) ? String(parseDocumentFilterValue(rule.rawValue)) : "";
switch (rule.mode) {
case "equals":
return { [rule.fieldName]: value };
case "not-equals":
return { [rule.fieldName]: { $ne: value } };
case "like":
return { [rule.fieldName]: { $regex: textValue, $options: "i" } };
case "not-like":
return { [rule.fieldName]: { $not: { $regex: textValue, $options: "i" } } };
case "greater-than":
return { [rule.fieldName]: { $gt: value } };
case "less-than":
return { [rule.fieldName]: { $lt: value } };
case "is-null":
return { [rule.fieldName]: null };
case "is-not-null":
return { [rule.fieldName]: { $ne: null } };
}
}
export function combineDocumentFilterConditions(conditions: Record<string, unknown>[], rules: Pick<DocumentFilterRule, "conjunction">[]): Record<string, unknown> | null {
if (conditions.length === 0) return null;
let result = conditions[0];
for (let i = 1; i < conditions.length; i++) {
const operator = rules[i]?.conjunction === "OR" ? "$or" : "$and";
result = { [operator]: [result, conditions[i]] };
}
return result;
}
const MAX_SAFE_BIGINT = 9007199254740991n;
const MIN_BSON_INT64 = -9223372036854775808n;
const MAX_BSON_INT64 = 9223372036854775807n;
function parseJsonPreservingLargeIntegers(json: string, options: DocumentFilterParseOptions = {}): unknown {
return JSON.parse(rewriteUnsafeIntegerTokens(json, options));
}
function rewriteUnsafeIntegerTokens(json: string, options: DocumentFilterParseOptions): string {
let output = "";
let i = 0;
while (i < json.length) {
const ch = json[i];
if (ch === '"') {
const start = i;
i++;
while (i < json.length) {
const current = json[i++];
if (current === "\\") {
i++;
} else if (current === '"') {
break;
}
}
output += json.slice(start, i);
continue;
}
if (ch === "-" || isDigit(ch)) {
const start = i;
let end = i;
if (json[end] === "-") end++;
if (!isDigit(json[end])) {
output += ch;
i++;
continue;
}
if (json[end] === "0") {
end++;
} else {
while (isDigit(json[end])) end++;
}
let decimalOrExponent = false;
if (json[end] === ".") {
decimalOrExponent = true;
end++;
while (isDigit(json[end])) end++;
}
if (json[end] === "e" || json[end] === "E") {
decimalOrExponent = true;
end++;
if (json[end] === "+" || json[end] === "-") end++;
while (isDigit(json[end])) end++;
}
const token = json.slice(start, end);
if (!decimalOrExponent) {
try {
const n = BigInt(token);
if (n > MAX_SAFE_BIGINT || n < -MAX_SAFE_BIGINT) {
output += unsafeIntegerReplacement(token, n, options);
i = end;
continue;
}
} catch {
/* not a valid integer */
}
}
output += token;
i = end;
continue;
}
output += ch;
i++;
}
return output;
}
function unsafeIntegerReplacement(token: string, value: bigint, options: DocumentFilterParseOptions): string {
if (options.kind === "mongodb" && value >= MIN_BSON_INT64 && value <= MAX_BSON_INT64) {
// MongoDB int64 filters must use Extended JSON so JS Number never rounds snowflake-style IDs.
return `{"$numberLong":${JSON.stringify(token)}}`;
}
return JSON.stringify(token);
}
function isDigit(value: string | undefined): boolean {
return value !== undefined && value >= "0" && value <= "9";
}
export function parseDocumentFilterInput(input: string, options: DocumentFilterParseOptions = {}): Record<string, unknown> {
const trimmed = input.trim();
if (!trimmed) return {};
const safe = quoteUnquotedObjectKeys(trimmed);
const parsed = parseJsonPreservingLargeIntegers(safe, options);
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {};
}
export function currentDocumentFilterJson(input: string, structured: Record<string, unknown> | null, kind?: DocumentStoreKind): string | undefined {
const manual = parseDocumentFilterInput(input, { kind });
const filter = structured ? (Object.keys(manual).length ? { $and: [manual, structured] } : structured) : manual;
return Object.keys(filter).length ? JSON.stringify(filter) : undefined;
}
export function currentDocumentSortJson(input: string): string | undefined {
const sort = parseDocumentFilterInput(input);
return Object.keys(sort).length ? JSON.stringify(sort) : undefined;
}
function parseDocumentFilterValue(raw: string, options: DocumentFilterParseOptions = {}): unknown {
const trimmed = raw.trim();
if (!trimmed) return "";
try {
return parseJsonPreservingLargeIntegers(trimmed, options);
} catch {
return trimmed;
}
}
export function elasticsearchSearchBodyFromDocumentQuery(options: Pick<DocumentStoreQueryPreviewOptions, "filterJson" | "sortJson" | "skip" | "limit">): Record<string, unknown> {
const body: Record<string, unknown> = {
from: options.skip,
size: options.limit,
};
const query = elasticsearchQueryFromDocumentFilter(options.filterJson);
if (query) body.query = query;
body.sort = elasticsearchSortFromDocumentSort(options.sortJson);
return body;
}
function safeElasticsearchSearchBodyFromDocumentQuery(options: Pick<DocumentStoreQueryPreviewOptions, "filterJson" | "sortJson" | "skip" | "limit">): Record<string, unknown> {
try {
return elasticsearchSearchBodyFromDocumentQuery(options);
} catch {
return {
from: options.skip,
size: options.limit,
sort: ["_doc"],
};
}
}
function elasticsearchQueryFromDocumentFilter(filterJson?: string): Record<string, unknown> | null {
const trimmed = filterJson?.trim();
if (!trimmed || trimmed === "{}") return null;
const parsed = JSON.parse(trimmed);
if (!isPlainRecord(parsed) || Object.keys(parsed).length === 0) return null;
return translateDocumentFilterToElasticsearchQuery(parsed);
}
function translateDocumentFilterToElasticsearchQuery(filter: Record<string, unknown>): Record<string, unknown> {
const filters: Record<string, unknown>[] = [];
for (const [key, value] of Object.entries(filter)) {
if (key === "$and") {
filters.push(...translateLogicalArrayToElasticsearch("$and", value));
} else if (key === "$or") {
const should = translateLogicalArrayToElasticsearch("$or", value);
if (should.length > 0) filters.push({ bool: { should, minimum_should_match: 1 } });
} else {
filters.push(translateFieldFilterToElasticsearchQuery(key, value));
}
}
if (filters.length === 1) return filters[0];
return { bool: { filter: filters } };
}
function translateLogicalArrayToElasticsearch(operator: "$and" | "$or", value: unknown): Record<string, unknown>[] {
if (!Array.isArray(value)) throw new Error(`${operator} must be an array`);
return value.filter(isPlainRecord).map(translateDocumentFilterToElasticsearchQuery);
}
function translateFieldFilterToElasticsearchQuery(field: string, value: unknown): Record<string, unknown> {
if (!isPlainRecord(value) || !Object.keys(value).some((key) => key.startsWith("$"))) {
return termOrNullQuery(field, value);
}
const filter: Record<string, unknown>[] = [];
const mustNot: Record<string, unknown>[] = [];
const range: Record<string, unknown> = {};
for (const [operator, operatorValue] of Object.entries(value)) {
if (operator === "$options") continue;
if (operator === "$ne") {
if (operatorValue === null) filter.push({ exists: { field } });
else mustNot.push({ term: { [field]: operatorValue } });
} else if (operator === "$gt" || operator === "$gte" || operator === "$lt" || operator === "$lte") {
range[operator.slice(1)] = operatorValue;
} else if (operator === "$regex") {
filter.push(wildcardQuery(field, operatorValue, value.$options));
} else if (operator === "$not") {
if (isPlainRecord(operatorValue) && "$regex" in operatorValue) {
mustNot.push(wildcardQuery(field, operatorValue.$regex, operatorValue.$options ?? value.$options));
}
}
}
if (Object.keys(range).length > 0) filter.push({ range: { [field]: range } });
if (filter.length === 1 && mustNot.length === 0) return filter[0];
if (filter.length === 0 && mustNot.length > 0) return { bool: { must_not: mustNot } };
const bool: Record<string, unknown> = {};
if (filter.length > 0) bool.filter = filter;
if (mustNot.length > 0) bool.must_not = mustNot;
return { bool };
}
function termOrNullQuery(field: string, value: unknown): Record<string, unknown> {
if (value === null) return { bool: { must_not: [{ exists: { field } }] } };
return { term: { [field]: value } };
}
function wildcardQuery(field: string, value: unknown, options: unknown): Record<string, unknown> {
const pattern = String(value ?? "");
const wildcard = pattern.startsWith("*") || pattern.endsWith("*") ? pattern : `*${pattern}*`;
return {
wildcard: {
[field]: {
value: wildcard,
case_insensitive: typeof options === "string" && options.toLowerCase().includes("i"),
},
},
};
}
function elasticsearchSortFromDocumentSort(sortJson?: string): unknown[] {
const trimmed = sortJson?.trim();
if (!trimmed || trimmed === "{}") return ["_doc"];
const parsed = JSON.parse(trimmed);
if (!isPlainRecord(parsed)) return ["_doc"];
return Object.entries(parsed).map(([field, direction]) => ({
[field]: { order: direction === -1 || (typeof direction === "string" && direction.toLowerCase() === "desc") ? "desc" : "asc" },
}));
}
function isPlainRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
+11
View File
@@ -0,0 +1,11 @@
import type { Locale } from "@/i18n";
export const LOCALE_OPTIONS: { value: Locale; flag: string; label: string }[] = [
{ value: "en", flag: "🇺🇸", label: "English" },
{ value: "es", flag: "🇪🇸", label: "Español" },
{ value: "it", flag: "🇮🇹", label: "Italiano" },
{ value: "ja", flag: "🇯🇵", label: "日本語" },
{ value: "pt-BR", flag: "🇧🇷", label: "Português" },
{ value: "zh-CN", flag: "🇨🇳", label: "简体中文" },
{ value: "zh-TW", flag: "繁", label: "繁體中文" },
];
@@ -0,0 +1,187 @@
import type { QueryTab } from "@/types/database";
export const OPEN_TABS_STORAGE_KEY = "dbx-open-tabs";
export const ACTIVE_TAB_STORAGE_KEY = "dbx-active-tab";
export interface SavedQueryResultRun {
id: string;
title: string;
sequence: number;
sql: string;
createdAt: number;
activeResultIndex?: number;
resultCacheKey?: string;
resultEvicted?: boolean;
}
export interface SavedOpenTab {
id: string;
title: string;
customTitle?: boolean;
connectionId: string;
database: string;
schema?: string;
sql: string;
savedSqlId?: string;
externalSqlPath?: string;
lastExecutedSql?: string;
resultBaseSql?: string;
resultSortedSql?: string;
resultSortColumn?: string;
resultSortColumnIndex?: number;
resultSortDirection?: QueryTab["resultSortDirection"];
resultSortMode?: QueryTab["resultSortMode"];
orderByInput?: string;
resultPageLimit?: number;
resultPageOffset?: number;
whereInput?: string;
pinned?: boolean;
mode?: QueryTab["mode"];
mqTenant?: string;
mqInitialTab?: QueryTab["mqInitialTab"];
nacosNamespace?: string;
nacosNamespaceName?: string;
structureTableName?: string;
objectBrowser?: QueryTab["objectBrowser"];
objectSource?: QueryTab["objectSource"];
tableMeta?: QueryTab["tableMeta"];
mongoEditTarget?: QueryTab["mongoEditTarget"];
resultEvicted?: boolean;
resultCacheKey?: string;
resultRuns?: SavedQueryResultRun[];
activeResultRunId?: string;
resultAutoSave?: boolean;
}
export interface RestoredOpenTabs {
tabs: QueryTab[];
activeTabId: string | null;
}
export type OpenTabsRestoreFilter = "all" | "pinned";
function shouldPersistTabSql(tab: QueryTab) {
if (!tab.savedSqlId) return true;
return tab.originalSql !== undefined && tab.sql !== tab.originalSql;
}
function restoredOriginalSql(tab: SavedOpenTab, mode: QueryTab["mode"], sql: string) {
if (mode !== "query") return undefined;
if (tab.externalSqlPath) return sql;
if (tab.savedSqlId) return sql ? "" : undefined;
return "";
}
export function serializeOpenTabs(tabs: QueryTab[]): SavedOpenTab[] {
return tabs.map((tab) => ({
id: tab.id,
title: tab.title,
...(tab.customTitle ? { customTitle: true } : {}),
connectionId: tab.connectionId,
database: tab.database,
schema: tab.schema,
sql: shouldPersistTabSql(tab) ? tab.sql : "",
savedSqlId: tab.savedSqlId,
externalSqlPath: tab.externalSqlPath,
...(tab.lastExecutedSql !== undefined ? { lastExecutedSql: tab.lastExecutedSql } : {}),
...(tab.resultBaseSql !== undefined ? { resultBaseSql: tab.resultBaseSql } : {}),
...(tab.resultSortedSql !== undefined ? { resultSortedSql: tab.resultSortedSql } : {}),
...(tab.resultSortColumn !== undefined ? { resultSortColumn: tab.resultSortColumn } : {}),
...(tab.resultSortColumnIndex !== undefined ? { resultSortColumnIndex: tab.resultSortColumnIndex } : {}),
...(tab.resultSortDirection !== undefined ? { resultSortDirection: tab.resultSortDirection } : {}),
...(tab.resultSortMode !== undefined ? { resultSortMode: tab.resultSortMode } : {}),
...(tab.orderByInput !== undefined ? { orderByInput: tab.orderByInput } : {}),
...(tab.resultPageLimit !== undefined ? { resultPageLimit: tab.resultPageLimit } : {}),
...(tab.resultPageOffset !== undefined ? { resultPageOffset: tab.resultPageOffset } : {}),
...(tab.whereInput !== undefined ? { whereInput: tab.whereInput } : {}),
pinned: tab.pinned,
mode: tab.mode,
...(tab.mqTenant !== undefined ? { mqTenant: tab.mqTenant } : {}),
...(tab.mqInitialTab !== undefined ? { mqInitialTab: tab.mqInitialTab } : {}),
...(tab.nacosNamespace !== undefined ? { nacosNamespace: tab.nacosNamespace } : {}),
...(tab.nacosNamespaceName !== undefined ? { nacosNamespaceName: tab.nacosNamespaceName } : {}),
...(tab.structureTableName !== undefined ? { structureTableName: tab.structureTableName } : {}),
objectBrowser: tab.objectBrowser,
objectSource: tab.objectSource,
tableMeta: tab.tableMeta,
...(tab.mongoEditTarget !== undefined ? { mongoEditTarget: tab.mongoEditTarget } : {}),
...(tab.mode !== "data" && tab.resultEvicted ? { resultEvicted: true } : {}),
...(tab.mode !== "data" && tab.resultEvicted && tab.resultCacheKey !== undefined ? { resultCacheKey: tab.resultCacheKey } : {}),
...(tab.mode === "query" && tab.resultRuns?.length
? {
resultRuns: tab.resultRuns.map((run) => ({
id: run.id,
title: run.title,
sequence: run.sequence,
sql: run.sql,
createdAt: run.createdAt,
activeResultIndex: run.activeResultIndex,
...(run.resultCacheKey !== undefined ? { resultCacheKey: run.resultCacheKey } : {}),
...(run.resultEvicted ? { resultEvicted: true } : {}),
})),
}
: {}),
...(tab.mode === "query" && tab.activeResultRunId !== undefined ? { activeResultRunId: tab.activeResultRunId } : {}),
...(tab.mode === "query" && tab.resultAutoSave ? { resultAutoSave: true } : {}),
}));
}
function isSavedOpenTab(value: unknown): value is SavedOpenTab {
if (!value || typeof value !== "object") return false;
const tab = value as Record<string, unknown>;
return typeof tab.id === "string" && typeof tab.title === "string" && typeof tab.connectionId === "string" && typeof tab.database === "string" && (typeof tab.sql === "string" || typeof tab.savedSqlId === "string");
}
export function restoreOpenTabsState(rawTabs: string | null, rawActiveTabId: string | null, options: { queryOnly?: boolean; filter?: OpenTabsRestoreFilter } = {}): RestoredOpenTabs {
if (!rawTabs) return { tabs: [], activeTabId: null };
try {
const parsed = JSON.parse(rawTabs);
if (!Array.isArray(parsed)) return { tabs: [], activeTabId: null };
const saved = parsed.filter(isSavedOpenTab);
const filtered = saved.filter((tab) => {
if (options.queryOnly && (tab.mode ?? "query") !== "query") return false;
if (options.filter === "pinned" && !tab.pinned) return false;
return true;
});
const tabs: QueryTab[] = filtered.map((tab) => {
const mode = tab.mode ?? "query";
const resultRuns =
mode === "query"
? tab.resultRuns?.map((run) => ({
...run,
result: undefined,
results: undefined,
resultCacheState: run.resultCacheKey ? ("disk" as const) : undefined,
}))
: undefined;
return {
...tab,
mode,
sql: typeof tab.sql === "string" ? tab.sql : "",
isExecuting: false,
isCancelling: false,
queryExecutionStartedAt: undefined,
editorViewport: undefined,
editorSelection: undefined,
isExplaining: false,
originalSql: restoredOriginalSql(tab, mode, typeof tab.sql === "string" ? tab.sql : ""),
resultEvicted: mode === "data" ? undefined : tab.resultEvicted,
resultCacheKey: mode === "data" ? undefined : tab.resultCacheKey,
resultCacheState: mode !== "data" && tab.resultCacheKey ? "disk" : undefined,
resultRuns,
activeResultRunId: resultRuns?.some((run) => run.id === tab.activeResultRunId) ? tab.activeResultRunId : resultRuns?.[0]?.id,
resultAutoSave: mode === "query" && tab.resultAutoSave ? true : undefined,
};
});
const activeTabId = rawActiveTabId || null;
return {
tabs,
activeTabId: tabs.some((tab) => tab.id === activeTabId) ? activeTabId : tabs[0]?.id || null,
};
} catch {
return { tabs: [], activeTabId: null };
}
}
+54
View File
@@ -0,0 +1,54 @@
import type { TreeNode } from "@/types/database";
export type PinnedTreeNodeUpdateScope = "missing" | "root" | "siblings";
export function orderPinnedFirst<T>(items: T[], isPinned: (item: T) => boolean): T[] {
const pinned: T[] = [];
const unpinned: T[] = [];
for (const item of items) {
if (isPinned(item)) pinned.push(item);
else unpinned.push(item);
}
return [...pinned, ...unpinned];
}
function findTreeNodeLocation(nodes: TreeNode[], id: string, parent: TreeNode | null = null): { node: TreeNode; parent: TreeNode | null } | null {
for (const node of nodes) {
if (node.id === id) return { node, parent };
if (node.children) {
const found = findTreeNodeLocation(node.children, id, node);
if (found) return found;
}
}
return null;
}
export function updatePinnedTreeNodeInPlace(nodes: TreeNode[], id: string, pinned: boolean): PinnedTreeNodeUpdateScope {
const location = findTreeNodeLocation(nodes, id);
if (!location) return "missing";
location.node.pinned = pinned;
const siblings = location.parent?.children ?? nodes;
const ordered = orderPinnedFirst(siblings, (node) => !!node.pinned);
if (location.parent) {
location.parent.children = ordered;
return "siblings";
}
nodes.splice(0, nodes.length, ...ordered);
return "root";
}
export function applyPinnedTreeNodeState(nodes: TreeNode[], pinnedIds: Set<string>): TreeNode[] {
return orderPinnedFirst(
nodes.map((node) => ({
...node,
pinned: pinnedIds.has(node.id),
children: node.children ? applyPinnedTreeNodeState(node.children, pinnedIds) : node.children,
})),
(node) => !!node.pinned,
);
}
@@ -0,0 +1,10 @@
export function prunePinnedTreeNodeIdsForConnection(ids: Set<string>, connectionId: string): Set<string> {
const next = new Set<string>();
const prefix = `${connectionId}:`;
for (const id of ids) {
if (id === connectionId) continue;
if (id.startsWith(prefix)) continue;
next.add(id);
}
return next;
}
@@ -0,0 +1,32 @@
export interface TransferTerminalState {
done: boolean;
cancelled: boolean;
error: boolean;
}
export interface TransferStatusLike {
status: "running" | "tableDone" | "done" | "error" | "cancelled";
}
export interface TransferDialogRunState extends TransferTerminalState {
transferring: boolean;
}
export function nextTransferTerminalState(state: TransferTerminalState, progress: TransferStatusLike): TransferTerminalState {
if (progress.status === "error") return { ...state, error: true };
if (progress.status === "done") return { ...state, done: true, error: state.error };
if (progress.status === "cancelled") return { ...state, cancelled: true };
return state;
}
export function isTransferRunActive(state: TransferDialogRunState): boolean {
return state.transferring && !state.done && !state.cancelled && !state.error;
}
export function shouldResetTransferDialogOnOpen(state: TransferDialogRunState): boolean {
return state.transferring && !isTransferRunActive(state);
}
export function shouldKeepTransferDraftOnOpen(preserveDraft: boolean, state: TransferDialogRunState): boolean {
return preserveDraft || shouldResetTransferDialogOnOpen(state);
}