feat(sql-file): add configurable file name filter

This commit is contained in:
andystenhe authored and GitHub committed 2026-08-31 22:55:42 +08:00
1 parent 8f3134dce8
commit f725c50b2c
18 files changed
+243 -23

No files matched your search

@@ -1,10 +1,11 @@
<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import { FolderOpen, FileCode, FolderClosed, ChevronRight, ChevronDown, X, Trash2, RefreshCw, FolderSearch, Copy, Play, ChevronsUpDown, ChevronsDownUp, FilePlus, Pencil } from "@lucide/vue";
import { FolderOpen, FileCode, FolderClosed, ChevronRight, ChevronDown, X, Trash2, RefreshCw, FolderSearch, Copy, Play, ChevronsUpDown, ChevronsDownUp, Settings, FilePlus, Pencil } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import LightTooltip from "@/components/ui/LightTooltip.vue";
import CustomContextMenu, { type ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";
import { useQueryStore } from "@/stores/queryStore";
@@ -17,7 +18,7 @@ import { forgetExternalSqlFileTarget, moveExternalSqlFileTarget, resolveExternal
import { externalSqlFileOpenErrorMessage, formatSqlFileSize, isExternalSqlFileTooLargeError } from "@/lib/sql/sqlFileOpen";
import * as api from "@/lib/backend/api";
import type { SqlFileEntry } from "@/lib/backend/api";
import { getSqlFileFolderPaths, saveSqlFileFolderPaths, notifySqlFileFoldersChanged } from "@/lib/sqlFile/sqlFileFolders";
import { getSqlFileFilter, getSqlFileFolderPaths, saveSqlFileFilter, saveSqlFileFolderPaths, notifySqlFileFoldersChanged } from "@/lib/sqlFile/sqlFileFolders";
import { orderedListRangeAnchorIndex, orderedListSelectionIntent, type OrderedListSelectionItem } from "@/lib/selection/orderedListSelection";
const emit = defineEmits<{
@@ -38,6 +39,9 @@ interface FolderState {
}
const folders = ref<FolderState[]>([]);
const filterSettingsOpen = ref(false);
const fileFilterDraft = ref(getSqlFileFilter());
const fileFilterRegexExample = ".*[.](sql|sh|py)$";
// Right-click target. `kind` discriminates between a folder header, a tree
// directory entry, and a tree file entry. `folderPath` is the owning top-level
@@ -112,12 +116,12 @@ async function addFolder(folderPath: string) {
// Re-scan a single top-level folder and replace its entries. Mutated via the
// reactive proxy (folders.value[idx]) so Vue tracks the change — see the note
// in addFolder above.
async function loadFolderEntries(folderPath: string) {
async function loadFolderEntries(folderPath: string): Promise<string | null> {
const idx = folders.value.findIndex((f) => f.path === folderPath);
if (idx === -1) return;
if (idx === -1) return null;
folders.value[idx].loading = true;
try {
const entries = await api.listSqlFilesInFolder(folderPath);
const entries = await api.listSqlFilesInFolder(folderPath, getSqlFileFilter());
const target = folders.value.findIndex((f) => f.path === folderPath);
if (target !== -1) {
folders.value[target].entries = entries;
@@ -132,13 +136,16 @@ async function loadFolderEntries(folderPath: string) {
folders.value[target].expanded = nextExpanded;
}
} catch (e: any) {
toast(t("sqlFileTree.loadFailed", { message: e?.message || String(e) }), 5000);
const message = e?.message || String(e);
toast(t("sqlFileTree.loadFailed", { message }), 5000);
return message;
} finally {
const target = folders.value.findIndex((f) => f.path === folderPath);
if (target !== -1) {
folders.value[target].loading = false;
}
}
return null;
}
function collectPaths(entries: SqlFileEntry[], into: Set<string>) {
@@ -160,6 +167,24 @@ async function refreshAll() {
toast(t("sqlFileTree.refreshed"), 1500);
}
async function saveFileFilter() {
const previousFilter = getSqlFileFilter();
saveSqlFileFilter(fileFilterDraft.value);
const errors = await Promise.all(folders.value.map((f) => loadFolderEntries(f.path)));
// An unparsable pattern must not survive in localStorage: it would fail
// every later refresh and silently empty Quick Open until it is fixed.
const invalidFilter = errors.find((message) => message?.startsWith("Invalid file filter"));
if (invalidFilter) {
saveSqlFileFilter(previousFilter);
await Promise.all(folders.value.map((f) => loadFolderEntries(f.path)));
toast(t("sqlFileTree.filterInvalid", { message: invalidFilter }), 5000);
return;
}
filterSettingsOpen.value = false;
notifySqlFileFoldersChanged();
toast(t("sqlFileTree.refreshed"), 1500);
}
async function reloadFolderAfterMutation(folderPath: string) {
await loadFolderEntries(folderPath);
notifySqlFileFoldersChanged();
@@ -507,6 +532,19 @@ function clearContextTarget() {
<div class="h-9 flex items-center gap-1 px-2 border-b shrink-0 bg-muted/20">
<span class="text-[13px] font-medium">{{ t("sqlFileTree.title") }}</span>
<span class="flex-1" />
<LightTooltip :text="t('sqlFileTree.filterSettings')" side="bottom" :delay="0" :close-delay="0" nowrap>
<Button
variant="ghost"
size="icon"
class="h-5 w-5"
@click="
fileFilterDraft = getSqlFileFilter();
filterSettingsOpen = true;
"
>
<Settings class="h-3 w-3" />
</Button>
</LightTooltip>
<LightTooltip v-if="folders.length > 0" :text="t('sqlFileTree.refreshAll')" side="bottom" :delay="0" :close-delay="0" nowrap>
<Button variant="ghost" size="icon" class="h-5 w-5" @click="refreshAll">
<RefreshCw class="h-3 w-3" />
@@ -649,6 +687,23 @@ function clearContextTarget() {
</template>
</CustomContextMenu>
<Dialog v-model:open="filterSettingsOpen">
<DialogContent class="sm:max-w-md">
<DialogHeader>
<DialogTitle>{{ t("sqlFileTree.filterSettings") }}</DialogTitle>
</DialogHeader>
<div class="grid gap-2 py-2">
<Label for="sql-file-filter">{{ t("sqlFileTree.fileFilter") }}</Label>
<Input id="sql-file-filter" v-model="fileFilterDraft" :placeholder="t('sqlFileTree.fileFilterPlaceholder')" @keydown.enter="saveFileFilter" />
<p class="text-xs text-muted-foreground">{{ t("sqlFileTree.fileFilterHint", { regex: fileFilterRegexExample }) }}</p>
</div>
<DialogFooter>
<Button type="button" variant="outline" @click="filterSettingsOpen = false">{{ t("common.cancel") }}</Button>
<Button type="button" @click="saveFileFilter">{{ t("common.save") }}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog :open="showCreateDialog" @update:open="(open) => (showCreateDialog = open)">
<DialogContent class="sm:max-w-[380px]">
<DialogHeader>
@@ -46,3 +46,16 @@ describe("SqlFilePanel directory actions", () => {
expect(panelSource).toContain('@click.stop="openCreateDialog(folder.path, entry.path)"');
});
});
describe("SqlFilePanel file filter", () => {
it("restores the previous filter when the backend rejects the saved pattern", () => {
expect(panelSource).toContain('message?.startsWith("Invalid file filter")');
expect(panelSource).toContain("saveSqlFileFilter(previousFilter)");
expect(panelSource).toContain('t("sqlFileTree.filterInvalid"');
});
it("renders the translated filter placeholder", () => {
expect(panelSource).toContain("t('sqlFileTree.fileFilterPlaceholder')");
expect(panelSource).not.toContain("const fileFilterPlaceholder =");
});
});