diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue
index 8918dd847..9a6641f2a 100644
--- a/apps/desktop/src/App.vue
+++ b/apps/desktop/src/App.vue
@@ -2294,7 +2294,7 @@ onUnmounted(() => {
diff --git a/apps/desktop/src/components/editor/QueryHistory.vue b/apps/desktop/src/components/editor/QueryHistory.vue
index 424e209dd..1abfc04a2 100644
--- a/apps/desktop/src/components/editor/QueryHistory.vue
+++ b/apps/desktop/src/components/editor/QueryHistory.vue
@@ -2,7 +2,7 @@
import { ref, computed, nextTick, onBeforeUnmount, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useSqlHighlighter } from "@/composables/useSqlHighlighter";
-import { CalendarClock, Copy, Database, RotateCcw, Search, Sparkles, Trash2, X } from "@lucide/vue";
+import { CalendarClock, Check, Copy, Database, ListFilter, LoaderCircle, Minus, RotateCcw, Search, Sparkles, Trash2, X } from "@lucide/vue";
import { RecycleScroller } from "vue-virtual-scroller";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
@@ -13,9 +13,10 @@ import { useConnectionStore } from "@/stores/connectionStore";
import { useToast } from "@/composables/useToast";
import { resolveHistoryActivityKind } from "@/lib/history/historyActivityKind";
import { canRollbackHistoryEntry } from "@/lib/history/historyAiAnalysis";
-import { hasHistoryDateRange, historyDateRangeIsValid, historyEntryMatchesDateRange, type HistoryDateRange } from "@/lib/history/historyTimeRange";
+import { hasHistoryDateRange, historyDateRangeIsValid, type HistoryDateRange } from "@/lib/history/historyTimeRange";
import { HISTORY_ROW_HEIGHT, HISTORY_SCROLL_BUFFER, shouldVirtualizeHistory } from "@/lib/history/historyVirtualList";
-import type { HistoryEntry } from "@/lib/backend/api";
+import { historyConnectionHasSelectedDatabase } from "@/lib/history/historySearch";
+import type { HistoryConnectionFilter, HistoryDatabaseFilter, HistoryEntry, HistorySearchRequest } from "@/lib/backend/api";
import { copyToClipboard } from "@/lib/common/clipboard";
import { executeWithProductionSqlGuard } from "@/lib/database/productionExecutionGuard";
import * as api from "@/lib/backend/api";
@@ -26,6 +27,11 @@ const { highlight } = useSqlHighlighter();
const store = useHistoryStore();
const connectionStore = useConnectionStore();
+const props = defineProps<{
+ currentConnectionId?: string;
+ currentDatabase?: string;
+}>();
+
const emit = defineEmits<{
restore: [sql: string, entry: HistoryEntry];
analyzeAi: [entry: HistoryEntry];
@@ -39,6 +45,10 @@ const activeFilter = ref
("all");
const dateRange = ref({ startDate: "", endDate: "" });
const dateRangeDraft = ref({ startDate: "", endDate: "" });
const dateRangeOpen = ref(false);
+const scopeFilterOpen = ref(false);
+const scopeSearchText = ref("");
+const selectedConnections = ref([]);
+const selectedDatabases = ref([]);
const startDateInputRef = ref(null);
const endDateInputRef = ref(null);
const selectedEntry = ref(null);
@@ -49,6 +59,7 @@ const deleteTargetId = ref(null);
const filterScrollRef = ref(null);
const filtersScrollable = ref(false);
let filterScrollResizeObserver: ResizeObserver | null = null;
+let searchDebounceTimer: ReturnType | null = null;
const filters: HistoryFilter[] = ["all", "query", "data_change", "schema_change", "failed"];
const hasDateFilter = computed(() => hasHistoryDateRange(dateRange.value));
@@ -60,19 +71,15 @@ const dateRangeSummary = computed(() => {
return `${start} -> ${end}`;
});
-const filtered = computed(() => {
- const q = searchText.value.toLowerCase();
- return store.entries.filter((entry) => {
- if (activeFilter.value === "failed" && entry.success) return false;
- if (activeFilter.value !== "all" && activeFilter.value !== "failed" && activityKind(entry) !== activeFilter.value) {
- return false;
- }
- if (!historyEntryMatchesDateRange(entry.executed_at, dateRange.value)) return false;
- if (!q) return true;
- return [entry.sql, entry.connection_name, entry.database, entry.operation, entry.target].filter(Boolean).some((value) => String(value).toLowerCase().includes(q));
- });
+const hasScopeFilter = computed(() => selectedConnections.value.length > 0 || selectedDatabases.value.length > 0);
+const wholeConnectionCount = computed(() => selectedConnections.value.filter((connection) => !isConnectionNarrowed(connection)).length);
+const scopeSummary = computed(() => {
+ const parts: string[] = [];
+ if (wholeConnectionCount.value > 0) parts.push(t("history.scope.connectionCount", { count: wholeConnectionCount.value }));
+ if (selectedDatabases.value.length > 0) parts.push(t("history.scope.databaseCount", { count: selectedDatabases.value.length }));
+ return parts.join(" / ");
});
-const emptyMessage = computed(() => (store.entries.length > 0 ? t("history.emptyFilteredRecent") : t("history.empty")));
+const emptyMessage = computed(() => (hasScopeFilter.value || hasDateFilter.value || activeFilter.value !== "all" || searchText.value ? t("history.emptyFiltered") : t("history.empty")));
function activityKind(entry: HistoryEntry) {
return resolveHistoryActivityKind(entry);
@@ -106,7 +113,7 @@ function executeDelete() {
}
function confirmClearHistory() {
- if (store.entries.length > 0) {
+ if (store.total > 0 || store.connectionOptions.length > 0) {
showClearConfirm.value = true;
}
}
@@ -145,6 +152,135 @@ function filterLabel(filter: HistoryFilter) {
return t(`history.filters.${filter}`);
}
+function connectionKey(connection: HistoryConnectionFilter) {
+ return connection.connection_id ? `id:${connection.connection_id}` : `legacy:${connection.connection_name}`;
+}
+
+function databaseKey(database: HistoryDatabaseFilter) {
+ return `${connectionKey(database)}\0${database.database}`;
+}
+
+function isConnectionSelected(connection: HistoryConnectionFilter) {
+ const key = connectionKey(connection);
+ return selectedConnections.value.some((candidate) => connectionKey(candidate) === key);
+}
+
+function isConnectionNarrowed(connection: HistoryConnectionFilter) {
+ return historyConnectionHasSelectedDatabase(connection, selectedDatabases.value);
+}
+
+function toggleConnection(connection: HistoryConnectionFilter) {
+ const key = connectionKey(connection);
+ if (isConnectionNarrowed(connection)) {
+ selectedDatabases.value = selectedDatabases.value.filter((database) => connectionKey(database) !== key);
+ return;
+ }
+ if (isConnectionSelected(connection)) {
+ selectedConnections.value = selectedConnections.value.filter((candidate) => connectionKey(candidate) !== key);
+ selectedDatabases.value = selectedDatabases.value.filter((database) => connectionKey(database) !== key);
+ } else {
+ selectedConnections.value = [...selectedConnections.value, { ...connection }];
+ }
+}
+
+function isDatabaseSelected(database: HistoryDatabaseFilter) {
+ const key = databaseKey(database);
+ return selectedDatabases.value.some((candidate) => databaseKey(candidate) === key);
+}
+
+function toggleDatabase(database: HistoryDatabaseFilter) {
+ const key = databaseKey(database);
+ if (isDatabaseSelected(database)) {
+ selectedDatabases.value = selectedDatabases.value.filter((candidate) => databaseKey(candidate) !== key);
+ if (!historyConnectionHasSelectedDatabase(database, selectedDatabases.value)) {
+ const parentKey = connectionKey(database);
+ selectedConnections.value = selectedConnections.value.filter((connection) => connectionKey(connection) !== parentKey);
+ }
+ return;
+ }
+ if (!isConnectionSelected(database)) {
+ selectedConnections.value = [...selectedConnections.value, { connection_id: database.connection_id, connection_name: database.connection_name }];
+ }
+ selectedDatabases.value = [...selectedDatabases.value, { ...database }];
+}
+
+const visibleConnectionOptions = computed(() => {
+ const query = scopeSearchText.value.trim().toLowerCase();
+ return store.connectionOptions.filter((option) => {
+ if (!query) return true;
+ return option.connection_name.toLowerCase().includes(query) || option.databases.some((database) => database.toLowerCase().includes(query));
+ });
+});
+
+const visibleDatabaseOptions = computed(() => {
+ const selectedKeys = new Set(selectedConnections.value.map(connectionKey));
+ const query = scopeSearchText.value.trim().toLowerCase();
+ return store.connectionOptions
+ .filter((connection) => selectedKeys.size === 0 || selectedKeys.has(connectionKey(connection)))
+ .flatMap((connection) =>
+ connection.databases
+ .filter((database) => !query || database.toLowerCase().includes(query) || connection.connection_name.toLowerCase().includes(query))
+ .map((database) => ({
+ connection_id: connection.connection_id,
+ connection_name: connection.connection_name,
+ database,
+ })),
+ );
+});
+
+const currentConnectionOption = computed(() => store.connectionOptions.find((option) => option.connection_id === props.currentConnectionId));
+
+function selectCurrentScope() {
+ const connection = currentConnectionOption.value;
+ if (!connection) return;
+ selectedConnections.value = [{ connection_id: connection.connection_id, connection_name: connection.connection_name }];
+ selectedDatabases.value = props.currentDatabase && connection.databases.includes(props.currentDatabase) ? [{ connection_id: connection.connection_id, connection_name: connection.connection_name, database: props.currentDatabase }] : [];
+ scopeFilterOpen.value = false;
+}
+
+function resetScopeFilter() {
+ selectedConnections.value = [];
+ selectedDatabases.value = [];
+}
+
+// Preserve local-day boundaries before converting date-only input to UTC ISO timestamps.
+function localDateBoundary(value: string, endOfDay: boolean): string | undefined {
+ if (!value) return undefined;
+ const [year, month, day] = value.split("-").map(Number);
+ if (!year || !month || !day) return undefined;
+ return new Date(year, month - 1, day, endOfDay ? 23 : 0, endOfDay ? 59 : 0, endOfDay ? 59 : 0, endOfDay ? 999 : 0).toISOString();
+}
+
+function buildHistorySearchRequest(): HistorySearchRequest {
+ return {
+ search_text: searchText.value,
+ connections: selectedConnections.value.map((connection) => ({ ...connection })),
+ databases: selectedDatabases.value.map((database) => ({ ...database })),
+ activity_kind: activeFilter.value !== "all" && activeFilter.value !== "failed" ? activeFilter.value : undefined,
+ success: activeFilter.value === "failed" ? false : undefined,
+ started_at: localDateBoundary(dateRange.value.startDate, false),
+ ended_at: localDateBoundary(dateRange.value.endDate, true),
+ limit: 100,
+ };
+}
+
+async function runHistorySearch() {
+ try {
+ await store.search(buildHistorySearchRequest());
+ } catch {
+ // The panel already renders the backend error; avoid a duplicate toast.
+ }
+}
+
+// Debounce all filter changes through one timer to avoid redundant history scans.
+function scheduleHistorySearch() {
+ if (searchDebounceTimer) clearTimeout(searchDebounceTimer);
+ searchDebounceTimer = setTimeout(() => {
+ searchDebounceTimer = null;
+ void runHistorySearch();
+ }, 250);
+}
+
function updateFilterScrollability() {
const el = filterScrollRef.value;
if (!el) return;
@@ -282,8 +418,12 @@ watch(
},
);
+watch([searchText, activeFilter, () => dateRange.value.startDate, () => dateRange.value.endDate, () => selectedConnections.value.map(connectionKey).join("\0"), () => selectedDatabases.value.map(databaseKey).join("\0")], scheduleHistorySearch);
+
onMounted(() => {
- store.load();
+ store.setHistoryPanelActive(true);
+ void store.loadConnectionOptions().catch(() => {});
+ void runHistorySearch();
void nextTick(updateFilterScrollability);
if (filterScrollRef.value) {
@@ -293,6 +433,9 @@ onMounted(() => {
});
onBeforeUnmount(() => {
+ store.setHistoryPanelActive(false);
+ if (searchDebounceTimer) clearTimeout(searchDebounceTimer);
+ searchDebounceTimer = null;
filterScrollResizeObserver?.disconnect();
filterScrollResizeObserver = null;
});
@@ -302,8 +445,9 @@ onBeforeUnmount(() => {
{{ t("history.title") }}
+ {{ store.total }}
-
+
+
+
+
+
+
+
+
+
{{ t("history.scope.title") }}
+
+ {{ t("history.scope.reset") }}
+
+
+
+
+
+
+
+
+ {{ t("history.scope.allConnections") }}
+
+
+ {{ t("history.scope.current") }}
+
+
+ {{ t("history.scope.connections") }}
+
+
+
+
+
+
+ {{ connection.connection_name || t("history.scope.unknownConnection") }}
+ {{ connection.databases.length }}
+
+
{{ t("history.scope.noOptions") }}
+
+ {{ t("history.scope.databases") }}
+
+
+
+
+
+ {{ database.database }}
+ {{ database.connection_name }}
+
+
{{ t("history.scope.noOptions") }}
+
+
+
-
-
+
+
{{ t("history.dateRange.label") }}
{{ dateRangeSummary }}
-
+
+
+
+
+
+ {{ scopeSummary }}
+
+
-
-
+
+
@@ -418,9 +624,20 @@ onBeforeUnmount(() => {
-
+
+
+ {{ t("common.loading") }}
+
+
+ {{ store.error }}
+
+
{{ emptyMessage }}
+
+
+ {{ store.loadingMore ? t("common.loading") : t("history.loadMore") }}
+