Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(pam): simplify query tab hook, remove dead code in sql editor and query panel

Bernie Gandin670481e0b1d5
diff --git a/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts b/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
index e7c2643e3c..f91cba7f68 100644
--- a/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
+++ b/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
@@ -197,14 +197,16 @@ export const handlePostgresSession = async (
// multiple statements (simple query protocol). We take the last result so the
// caller always sees a single consistent response — same behaviour as psql.
const startTime = performance.now();
- const rawResult = await pgClient.query(message.sql);
+ const rawResult = (await pgClient.query(message.sql)) as
+ | pg.QueryResult<Record<string, unknown>>
+ | pg.QueryResult<Record<string, unknown>>[];
const executionTimeMs = Math.round(performance.now() - startTime);
const MAX_ROWS = 1000;
const result = Array.isArray(rawResult) ? rawResult[rawResult.length - 1] : rawResult;
sendResponse({
type: PostgresServerMessageType.QueryResult,
id: message.id,
- rows: (result.rows ?? []).slice(0, MAX_ROWS) as Record<string, unknown>[],
+ rows: (result.rows ?? []).slice(0, MAX_ROWS),
fields: (result.fields ?? []).map((f) => ({ name: f.name })),
rowCount: result.rowCount,
command: result.command ?? "",
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/PamDataExplorerPage.tsx b/frontend/src/pages/pam/PamDataExplorerPage/PamDataExplorerPage.tsx
index bdabb39f52..2a4354107d 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/PamDataExplorerPage.tsx
+++ b/frontend/src/pages/pam/PamDataExplorerPage/PamDataExplorerPage.tsx
@@ -239,10 +239,6 @@ export const PamDataExplorerPage = () => {
setActiveTab(BROWSE_TAB_ID);
}, [pendingTableSwitch, selectedSchema, loadTableDetail, setActiveTab]);
- const handleChangeCountUpdate = useCallback((count: number) => {
- unsavedChangeCountRef.current = count;
- }, []);
-
const handleFullRefresh = useCallback(async () => {
// 1. Re-fetch tables for current schema (picks up new/dropped tables)
setIsLoadingTables(true);
@@ -468,7 +464,9 @@ export const PamDataExplorerPage = () => {
</button>
</div>
- <div className={cn("flex flex-1 overflow-hidden", activeTabId !== BROWSE_TAB_ID && "hidden")}>
+ <div
+ className={cn("flex flex-1 overflow-hidden", activeTabId !== BROWSE_TAB_ID && "hidden")}
+ >
{selectedTable ? (
<DataExplorerGrid
key={`${selectedSchema}.${selectedTable}`}
@@ -478,7 +476,9 @@ export const PamDataExplorerPage = () => {
table={selectedTable}
executeQuery={executeQuery}
isLoading={isLoadingDetail}
- onChangeCountUpdate={handleChangeCountUpdate}
+ onChangeCountUpdate={(count) => {
+ unsavedChangeCountRef.current = count;
+ }}
onFullRefresh={handleFullRefresh}
/>
) : (
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx b/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
index b1bdfee872..485e73bd75 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
+++ b/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { cn } from "@app/components/v3/utils";
@@ -39,7 +39,7 @@ export function QueryPanel({ tab, executeQuery, tableDetail, onSqlChange }: Prop
}
}, []);
- const handleRun = useCallback(async () => {
+ const handleRun = async () => {
if (!tab.sql.trim() || isRunning) return;
setIsRunning(true);
setError(null);
@@ -52,15 +52,10 @@ export function QueryPanel({ tab, executeQuery, tableDetail, onSqlChange }: Prop
} finally {
setIsRunning(false);
}
- }, [tab.sql, isRunning, executeQuery]);
-
- const onMouseDown = useCallback((e: React.MouseEvent) => {
- e.preventDefault();
- setIsDragging(true);
- }, []);
+ };
useEffect(() => {
- if (!isDragging) return;
+ if (!isDragging) return undefined;
const onMouseMove = (e: MouseEvent) => {
const container = containerRef.current;
@@ -99,11 +94,19 @@ export function QueryPanel({ tab, executeQuery, tableDetail, onSqlChange }: Prop
"h-1 shrink-0 cursor-row-resize bg-mineshaft-600 transition-colors hover:bg-primary/50",
isDragging && "bg-primary/50"
)}
- onMouseDown={onMouseDown}
+ onMouseDown={(e) => {
+ e.preventDefault();
+ setIsDragging(true);
+ }}
/>
<div className="min-h-0 flex-1 overflow-hidden bg-bunker-800">
- <QueryResultsTable result={result} error={error} isRunning={isRunning} tableDetail={tableDetail} />
+ <QueryResultsTable
+ result={result}
+ error={error}
+ isRunning={isRunning}
+ tableDetail={tableDetail}
+ />
</div>
</div>
</div>
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/components/SqlEditor.tsx b/frontend/src/pages/pam/PamDataExplorerPage/components/SqlEditor.tsx
index ad645bd246..51b22b41d8 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/components/SqlEditor.tsx
+++ b/frontend/src/pages/pam/PamDataExplorerPage/components/SqlEditor.tsx
@@ -66,16 +66,5 @@ export function SqlEditor({ value, onChange, onExecute }: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
- // Sync external value changes into the editor
- useEffect(() => {
- const view = viewRef.current;
- if (view) {
- const current = view.state.doc.toString();
- if (current !== value) {
- view.dispatch({ changes: { from: 0, to: current.length, insert: value } });
- }
- }
- }, [value]);
-
return <div ref={containerRef} className="h-full w-full" />;
}
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/use-query-tabs.ts b/frontend/src/pages/pam/PamDataExplorerPage/use-query-tabs.ts
index 865a021310..33d9d67dbf 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/use-query-tabs.ts
+++ b/frontend/src/pages/pam/PamDataExplorerPage/use-query-tabs.ts
@@ -1,4 +1,4 @@
-import { useCallback, useState } from "react";
+import { useState } from "react";
export type QueryTab = {
id: string;
@@ -19,50 +19,48 @@ const DEFAULT_STATE: TabState = { tabs: [], activeTabId: BROWSE_TAB_ID, nextTabN
export function useQueryTabs() {
const [state, setState] = useState<TabState>(DEFAULT_STATE);
- const update = useCallback((updater: (prev: TabState) => TabState) => {
- setState((prev) => updater(prev));
- }, []);
-
- const addTab = useCallback(() => {
- update((prev) => {
+ const addTab = () => {
+ setState((prev) => {
const newTab: QueryTab = {
id: crypto.randomUUID(),
title: `Query ${prev.nextTabNumber}`,
sql: ""
};
- return { tabs: [...prev.tabs, newTab], activeTabId: newTab.id, nextTabNumber: prev.nextTabNumber + 1 };
+ return {
+ tabs: [...prev.tabs, newTab],
+ activeTabId: newTab.id,
+ nextTabNumber: prev.nextTabNumber + 1
+ };
});
- }, [update]);
+ };
- const closeTab = useCallback(
- (id: string) => {
- update((prev) => {
- const idx = prev.tabs.findIndex((t) => t.id === id);
- const newTabs = prev.tabs.filter((t) => t.id !== id);
- const newActiveId =
- prev.activeTabId === id
- ? (newTabs[idx - 1]?.id ?? newTabs[0]?.id ?? BROWSE_TAB_ID)
- : prev.activeTabId;
- return { ...prev, tabs: newTabs, activeTabId: newActiveId };
- });
- },
- [update]
- );
+ const closeTab = (id: string) => {
+ setState((prev) => {
+ const idx = prev.tabs.findIndex((t) => t.id === id);
+ const newTabs = prev.tabs.filter((t) => t.id !== id);
+ const newActiveId =
+ prev.activeTabId === id
+ ? (newTabs[idx - 1]?.id ?? newTabs[0]?.id ?? BROWSE_TAB_ID)
+ : prev.activeTabId;
+ return { ...prev, tabs: newTabs, activeTabId: newActiveId };
+ });
+ };
- const setActiveTab = useCallback(
- (id: string) => update((prev) => ({ ...prev, activeTabId: id })),
- [update]
- );
+ const setActiveTab = (id: string) => setState((prev) => ({ ...prev, activeTabId: id }));
- const updateTabSql = useCallback(
- (id: string, sql: string) => {
- update((prev) => ({
- ...prev,
- tabs: prev.tabs.map((t) => (t.id === id ? { ...t, sql } : t))
- }));
- },
- [update]
- );
+ const updateTabSql = (id: string, sql: string) => {
+ setState((prev) => ({
+ ...prev,
+ tabs: prev.tabs.map((t) => (t.id === id ? { ...t, sql } : t))
+ }));
+ };
- return { tabs: state.tabs, activeTabId: state.activeTabId, addTab, closeTab, setActiveTab, updateTabSql };
+ return {
+ tabs: state.tabs,
+ activeTabId: state.activeTabId,
+ addTab,
+ closeTab,
+ setActiveTab,
+ updateTabSql
+ };
}