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。

7.3 KB
Raw Blame
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { Copy, FileText, ListChecks, RefreshCcw, Settings } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import * as api from "@/lib/backend/api";
import type { MeilisearchIndexOverview } from "@/lib/backend/tauri";
import { formatBytes } from "@/lib/database/serverMetrics";
import { useToast } from "@/composables/useToast";
import { useTabUiState } from "@/lib/tabs/tabUiState";
import MeilisearchDocumentsPage from "./MeilisearchDocumentsPage.vue";
import MeilisearchSettingsPage from "./MeilisearchSettingsPage.vue";
import MeilisearchTasksPage from "./MeilisearchTasksPage.vue";
import { formatMeilisearchTaskDateTime } from "@/types/meilisearchManagement";

const props = defineProps<{
  connectionId: string;
  index: string;
}>();

type ActiveSection = "documents" | "tasks" | "settings";

const { initialState: restoredUiState, track: trackUiState } = useTabUiState<{ activeSection?: ActiveSection }>({}, "MeilisearchIndexView");

const { t, locale } = useI18n();
const { toast } = useToast();

const activeSection = ref<ActiveSection>(restoredUiState.activeSection ?? "documents");
const overview = ref<MeilisearchIndexOverview | null>(null);
const refreshing = ref(false);
let refreshRequestId = 0;

trackUiState(() => ({ activeSection: activeSection.value }));

const navSections = computed<Array<{ value: ActiveSection; label: string; icon: typeof FileText }>>(() => [
  { value: "documents", label: t("meilisearch.documents"), icon: FileText },
  { value: "tasks", label: t("meilisearch.tasks"), icon: ListChecks },
  { value: "settings", label: t("meilisearch.settings"), icon: Settings },
]);

const updatedAtLabel = computed(() => {
  const value = overview.value?.updatedAt;
  if (!value) return "-";
  return formatMeilisearchTaskDateTime(value, locale.value);
});

async function refreshStats(notifyOnError = false) {
  const requestId = ++refreshRequestId;
  refreshing.value = true;
  try {
    const nextOverview = await api.meilisearchGetIndexOverview(props.connectionId, props.index);
    if (requestId === refreshRequestId) overview.value = nextOverview;
  } catch (cause: any) {
    // Overview is best-effort; the tab still works without it.
    if (requestId === refreshRequestId && notifyOnError) toast(cause?.message || String(cause), 5000);
  } finally {
    if (requestId === refreshRequestId) refreshing.value = false;
  }
}

async function copyText(value: string | null | undefined) {
  if (!value) return;
  try {
    await navigator.clipboard.writeText(value);
    toast(t("meilisearch.copied"));
  } catch {
    // Clipboard may be unavailable (e.g. permission denied); stay silent.
  }
}

onMounted(() => {
  void refreshStats();
});
</script>

<template>
  <div class="h-full flex overflow-hidden">
    <!-- Left column: index meta + navigation -->
    <nav class="w-44 shrink-0 select-none border-r flex flex-col gap-1 overflow-y-auto p-2">
      <div class="px-1 pb-2">
        <div class="flex items-center gap-1">
          <div class="min-w-0 flex-1 truncate text-sm font-semibold text-foreground" :title="index">{{ index }}</div>
          <Button variant="ghost" size="icon" class="h-6 w-6 shrink-0" :disabled="refreshing" :title="t('meilisearch.refresh')" :aria-label="t('meilisearch.refresh')" @click="refreshStats(true)">
            <RefreshCcw class="h-3.5 w-3.5" :class="{ 'animate-spin': refreshing }" />
          </Button>
        </div>
        <div v-if="overview?.isIndexing" class="mt-0.5 text-xs text-primary">{{ t("meilisearch.isIndexing") }}</div>
      </div>

      <div class="space-y-2.5 px-1 pb-2 text-xs">
        <div>
          <div class="text-muted-foreground">{{ t("meilisearch.uid") }}</div>
          <div class="mt-0.5 flex items-center gap-1">
            <span class="min-w-0 truncate font-mono text-foreground/80" :title="overview?.uid ?? index">{{ overview?.uid ?? index }}</span>
            <button type="button" class="shrink-0 rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-foreground" :title="t('common.copy')" @click="copyText(overview?.uid ?? index)">
              <Copy class="h-3 w-3" />
            </button>
          </div>
        </div>
        <div>
          <div class="text-muted-foreground">{{ t("meilisearch.updatedAt") }}</div>
          <div class="mt-0.5 text-foreground/80" :title="overview?.updatedAt ?? undefined">{{ updatedAtLabel }}</div>
        </div>
        <div>
          <div class="text-muted-foreground">{{ t("meilisearch.primaryKey") }}</div>
          <div class="mt-0.5 flex items-center gap-1">
            <span class="min-w-0 truncate font-mono text-foreground/80" :title="overview?.primaryKey ?? undefined">{{ overview?.primaryKey ?? "-" }}</span>
            <button v-if="overview?.primaryKey" type="button" class="shrink-0 rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-foreground" :title="t('common.copy')" @click="copyText(overview.primaryKey)">
              <Copy class="h-3 w-3" />
            </button>
          </div>
        </div>
        <div>
          <div class="text-muted-foreground">{{ t("meilisearch.documentCountLabel") }}</div>
          <div class="mt-0.5 tabular-nums text-foreground/80">{{ overview ? overview.numberOfDocuments : "-" }}</div>
        </div>
        <div v-if="overview?.documentSize != null">
          <div class="text-muted-foreground">{{ t("meilisearch.documentSize") }}</div>
          <div class="mt-0.5 tabular-nums text-foreground/80">{{ formatBytes(overview.documentSize) }}</div>
        </div>
        <div v-if="overview?.avgDocumentSize != null">
          <div class="text-muted-foreground">{{ t("meilisearch.avgDocumentSize") }}</div>
          <div class="mt-0.5 tabular-nums text-foreground/80">{{ formatBytes(overview.avgDocumentSize) }}</div>
        </div>
        <!-- Older servers report no per-index size; label the instance-wide number so it is not read as this index's size. -->
        <div v-else-if="overview?.databaseSize != null">
          <div class="text-muted-foreground">{{ t("meilisearch.instanceDatabaseSize") }}</div>
          <div class="mt-0.5 tabular-nums text-foreground/80">{{ formatBytes(overview.databaseSize) }}</div>
        </div>
      </div>

      <div class="mx-1 border-t border-border/60" />

      <button
        v-for="section in navSections"
        :key="section.value"
        type="button"
        class="mt-1 flex items-center gap-2 rounded-md px-2.5 py-1.5 text-left text-xs transition-colors"
        :class="activeSection === section.value ? 'bg-accent text-foreground font-medium' : 'text-muted-foreground hover:bg-muted/70 hover:text-foreground'"
        @click="activeSection = section.value"
      >
        <component :is="section.icon" class="h-3.5 w-3.5 shrink-0" />
        <span>{{ section.label }}</span>
      </button>
    </nav>

    <!-- Content -->
    <div class="flex-1 min-h-0 min-w-0 overflow-hidden">
      <MeilisearchDocumentsPage v-if="activeSection === 'documents'" :connection-id="connectionId" :index="index" @refresh-stats="refreshStats" />
      <MeilisearchTasksPage v-else-if="activeSection === 'tasks'" :connection-id="connectionId" :fixed-index-uid="index" />
      <MeilisearchSettingsPage v-else :connection-id="connectionId" :index="index" @refresh-stats="refreshStats" />
    </div>
  </div>
</template>