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。

3.4 KB
Raw Blame
<script setup lang="ts">
import { computed, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import { Lock, Unlock } from "@lucide/vue";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { useConnectionStore } from "@/stores/connectionStore";
import { useReadOnlyUnlockStore } from "@/stores/readOnlyUnlockStore";
import { formatWriteUnlockRemaining } from "@/lib/database/readOnlyWriteAccess";
import { mysqlDataCharsetEnabled } from "@/lib/connection/mysqlDataCharset";

const props = defineProps<{
  connectionId: string;
  compact?: boolean;
  showLabel?: boolean;
}>();

const { t } = useI18n();
const connectionStore = useConnectionStore();
const unlockStore = useReadOnlyUnlockStore();

const compatibilityReadOnly = computed(() => mysqlDataCharsetEnabled(connectionStore.getConfig(props.connectionId)));
const persistentlyReadOnly = computed(() => compatibilityReadOnly.value || connectionStore.getConfig(props.connectionId)?.read_only === true);
const remainingMs = computed(() => unlockStore.remainingMs(props.connectionId));
const unlocked = computed(() => !compatibilityReadOnly.value && remainingMs.value > 0);
const remainingLabel = computed(() => formatWriteUnlockRemaining(remainingMs.value));
const tooltip = computed(() => {
  if (compatibilityReadOnly.value) return t("connection.mysqlDataCharsetReadOnly");
  if (unlocked.value) return `${t("readOnlyUnlock.unlockedBadge")} · ${t("readOnlyUnlock.remaining", { time: remainingLabel.value })}`;
  return t("connection.readOnlyBadge");
});

async function onActivate() {
  if (compatibilityReadOnly.value) return;
  if (!persistentlyReadOnly.value) return;
  if (unlocked.value) {
    await unlockStore.lockNow(props.connectionId);
    return;
  }
  await unlockStore.requestUnlock({
    connectionId: props.connectionId,
    connectionName: connectionStore.getConfig(props.connectionId)?.name,
    source: t("readOnlyUnlock.sourceStatus"),
  });
}

watch(
  () => props.connectionId,
  (connectionId) => {
    if (persistentlyReadOnly.value) void unlockStore.refreshFromBackend(connectionId);
  },
);

onMounted(() => {
  if (persistentlyReadOnly.value) void unlockStore.refreshFromBackend(props.connectionId);
});
</script>

<template>
  <Tooltip v-if="persistentlyReadOnly">
    <TooltipTrigger as-child>
      <button
        type="button"
        class="inline-flex items-center gap-0.5 rounded text-muted-foreground hover:bg-muted-foreground/15 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        :class="compact ? 'h-4 px-0.5' : 'h-4 px-1'"
        :aria-disabled="compatibilityReadOnly"
        :aria-label="compatibilityReadOnly ? tooltip : unlocked ? t('readOnlyUnlock.lockNow') : t('readOnlyUnlock.unlockAction')"
        :title="compatibilityReadOnly ? tooltip : unlocked ? t('readOnlyUnlock.lockNow') : t('readOnlyUnlock.unlockAction')"
        @click.stop.prevent="onActivate"
      >
        <Unlock v-if="unlocked" class="h-3 w-3 shrink-0 text-amber-600 dark:text-amber-400" />
        <Lock v-else class="h-3 w-3 shrink-0" />
        <span v-if="showLabel && !unlocked" class="text-[10px] leading-none">{{ t("connection.readOnlyBadge") }}</span>
        <span v-if="unlocked" class="text-[10px] tabular-nums leading-none">{{ remainingLabel }}</span>
      </button>
    </TooltipTrigger>
    <TooltipContent>{{ tooltip }}</TooltipContent>
  </Tooltip>
</template>