Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

5.7 KB
Raw Blame
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { AlertTriangle, Loader2 } from "@lucide/vue";
import PluginFileManager from "@/components/plugins/PluginFileManager.vue";
import PluginRefreshState from "@/components/plugins/PluginRefreshState.vue";
import * as api from "@/lib/backend/api";
import { createFrontendPluginRegistry } from "@/lib/plugins/frontendPlugin";
import type { InstalledPlugin } from "@/types/database";
import { useI18n } from "vue-i18n";
import { useConnectionStore } from "@/stores/connectionStore";

const props = defineProps<{
  pluginId: string;
  providerId: string;
  connectionId?: string;
  rootUri?: string;
  initialUri?: string;
}>();

const { t, locale: appLocale } = useI18n();
const connectionStore = useConnectionStore();

const plugins = ref<InstalledPlugin[]>([]);
const loading = ref(true);
const error = ref("");
const deferred = ref(false);
let loadGeneration = 0;
const entry = computed(() =>
  createFrontendPluginRegistry(plugins.value, appLocale.value)
    .listFilesystemProviders()
    .find((candidate) => candidate.plugin.manifest.id === props.pluginId && candidate.contribution.id === props.providerId),
);
const provider = computed(() => (entry.value ? { ...entry.value.contribution, root_uri: props.rootUri || entry.value.contribution.root_uri } : undefined));
const fileManagerRef = ref<InstanceType<typeof PluginFileManager>>();

async function load() {
  const generation = ++loadGeneration;
  loading.value = true;
  error.value = "";
  try {
    const installed = await api.listPlugins();
    if (generation !== loadGeneration) return;
    plugins.value = installed;
    if (!entry.value) throw new Error(t("pluginPlatform.filesystemUnavailable", { pluginId: props.pluginId, providerId: props.providerId }));
  } catch (cause) {
    if (generation !== loadGeneration) return;
    error.value = cause instanceof Error ? cause.message : String(cause);
  } finally {
    if (generation === loadGeneration) loading.value = false;
  }
}

async function refresh(options: { silent?: boolean } = {}) {
  const silent = options.silent === true;
  deferred.value = false;
  loading.value = true;
  error.value = "";
  try {
    if (props.connectionId) {
      if (connectionStore.connectedIds.has(props.connectionId)) {
        // Mirror PluginWorkbenchTab: an open connection's sidecar registry can
        // still be wiped (iframe reload, tab restore) and ensureConnected's
        // health fast-path would not heal that — force a re-push; it no-ops
        // when a push just happened. Silent restore overrides the recent-health
        // TTL (the SPA's boot restore can mark a connection healthy without
        // delivering credentials to the sidecar) and parks on the prompt when
        // the push cannot run (dbx-plugin-ssh#144).
        const pushed = await connectionStore.repushPluginConnection(props.connectionId, { ignoreRecentHealthCheck: silent });
        if (silent && pushed === false) {
          loading.value = false;
          deferred.value = true;
          return;
        }
      } else {
        // 被动唤醒(页签恢复/重新挂载):用户显式关掉的连接不在这里偷偷重连,
        // 由下面 silent 分支停在「重新加载」提示上等用户自己决定。
        await connectionStore.ensureConnected(props.connectionId, { allowPasswordPrompt: silent ? false : undefined, skipIfClosedByUser: true });
      }
    }
    if (fileManagerRef.value?.refresh) {
      loading.value = false;
      return await fileManagerRef.value.refresh();
    }
    await load();
  } catch (cause) {
    loading.value = false;
    if (silent) {
      // Silent recovery (tab restore after a page refresh, or a filesystem
      // tab opened for a not-yet-connected connection) must not surface an
      // error card nor trigger an interactive credential prompt from the
      // background: park on the explicit reload prompt so the user can retry
      // interactively (dbx-plugin-ssh#144).
      error.value = "";
      deferred.value = !!props.connectionId;
      return;
    }
    error.value = cause instanceof Error ? cause.message : String(cause);
  }
}

function start() {
  if (props.connectionId && !connectionStore.connectedIds.has(props.connectionId)) {
    // A restored tab (page refresh) or a link for a closed connection used to
    // park here from a one-shot snapshot: even when the SPA's boot connect
    // replay landed a moment later nothing re-checked, so the tab dead-ended
    // on the reload prompt (dbx-plugin-ssh#144, same reproduction as the
    // workbench tab). Self-heal silently first and only park when silent
    // recovery cannot proceed (interactive credentials required, connection
    // gone).
    void refresh({ silent: true });
    return;
  }
  void load();
}

onMounted(start);
watch(() => [props.pluginId, props.providerId, props.connectionId], start);

defineExpose({
  refresh,
});
</script>

<template>
  <div class="flex size-full min-h-0">
    <PluginRefreshState v-if="deferred" @refresh="void refresh()" />
    <div v-else-if="loading" class="m-auto flex items-center text-sm text-muted-foreground"><Loader2 class="mr-2 size-4 animate-spin" />{{ t("pluginPlatform.loadingFilesystem") }}</div>
    <div v-else-if="error || !provider" class="m-auto flex max-w-lg items-start gap-3 rounded-lg border border-destructive/40 bg-destructive/5 p-4 text-sm text-destructive">
      <AlertTriangle class="mt-0.5 size-4 shrink-0" />
      <span>{{ error || t("pluginPlatform.filesystemUnavailableFallback") }}</span>
    </div>
    <PluginFileManager v-else ref="fileManagerRef" class="min-h-0 flex-1" :plugin-id="pluginId" :provider="provider" :connection-id="connectionId" :initial-uri="initialUri" />
  </div>
</template>