Skip to content

Commit 0796470

Browse files
committed
chore: coderabbit suggestions
1 parent 9f2d832 commit 0796470

4 files changed

Lines changed: 57 additions & 75 deletions

File tree

resources/js/components/postgres/postgres-master-sidebar.tsx

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip
1010
import { usePostgresStore } from "@/contexts/postgres-context";
1111
import { useAppearance } from "@/hooks/use-appearance";
1212
import { DatabaseConnection } from "@/types/database";
13+
import { QueryHistoryItem } from "@/types/postgres";
1314
import Editor from "@monaco-editor/react";
1415
import dayjs from "dayjs";
1516
import relativeTime from "dayjs/plugin/relativeTime";
16-
import {
17-
CodeIcon,
18-
DatabaseIcon,
19-
PanelLeftIcon,
20-
PlayIcon,
21-
RotateCcwIcon,
22-
SearchIcon,
23-
XIcon
24-
} from "lucide-react";
25-
import { useCallback, useEffect, useRef, useState } from "react";
17+
import { CodeIcon, DatabaseIcon, PanelLeftIcon, PlayIcon, RotateCcwIcon, SearchIcon, XIcon } from "lucide-react";
18+
import { useCallback, useEffect, useState } from "react";
2619
import { Separator } from "../ui/separator";
2720

2821
dayjs.extend(relativeTime);
@@ -36,18 +29,6 @@ interface TableData {
3629
table_name: string;
3730
}
3831

39-
interface QueryHistoryItem {
40-
id: number;
41-
database_connection_id: number;
42-
query: string;
43-
executor: string;
44-
executor_id: number | null;
45-
type: string;
46-
description: string;
47-
created_at: string;
48-
updated_at: string;
49-
}
50-
5132
interface PostgresMasterSidebarProps {
5233
connection: DatabaseConnection;
5334
loading: boolean;
@@ -59,19 +40,19 @@ interface PostgresMasterSidebarProps {
5940
onTableSelect: (tableName: string) => void;
6041
}
6142

62-
const PostgresMasterSidebar = ({
43+
const PostgresMasterSidebar = ({
6344
connection,
64-
loading,
65-
selectedSchema,
66-
schemas,
67-
tables,
68-
selectedTable,
69-
onSchemaSelect,
70-
onTableSelect
45+
loading,
46+
selectedSchema,
47+
schemas,
48+
tables,
49+
selectedTable,
50+
onSchemaSelect,
51+
onTableSelect,
7152
}: PostgresMasterSidebarProps) => {
7253
const isQueryMode = usePostgresStore((state) => state.isQueryMode);
7354
const queryString = usePostgresStore((state) => state.queryString);
74-
const queryHistory = usePostgresStore((state) => state.queryHistory) as QueryHistoryItem[];
55+
const queryHistory = usePostgresStore((state) => state.queryHistory);
7556
const executingQuery = usePostgresStore((state) => state.executingQuery);
7657
const loadingHistory = usePostgresStore((state) => state.loadingHistory);
7758
const setQueryString = usePostgresStore((state) => state.setQueryString);
@@ -81,7 +62,7 @@ const PostgresMasterSidebar = ({
8162
const [open, setOpen] = useState(false);
8263
const [search, setSearch] = useState("");
8364
const [activeTab, setActiveTab] = useState("explorer");
84-
65+
8566
const { appearance } = useAppearance();
8667

8768
// Auto-switch to query tab when query mode is enabled
@@ -92,15 +73,19 @@ const PostgresMasterSidebar = ({
9273
}, [isQueryMode]);
9374

9475
// Handle tab changes and auto-enable query mode
95-
const handleTabChange = useCallback((tab: string) => {
96-
setActiveTab(tab);
97-
if (tab === "query" && !isQueryMode) {
98-
setQueryMode(true);
99-
}
100-
}, [isQueryMode, setQueryMode]);
101-
76+
const handleTabChange = useCallback(
77+
(tab: string) => {
78+
setActiveTab(tab);
79+
if (tab === "query" && !isQueryMode) {
80+
setQueryMode(true);
81+
}
82+
},
83+
[isQueryMode, setQueryMode],
84+
);
85+
10286
const filteredTables = tables.filter((table) => {
103-
const regex = new RegExp(search, "i");
87+
const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
88+
const regex = new RegExp(escapedSearch, "i");
10489
return regex.test(table.table_name);
10590
});
10691

@@ -169,7 +154,7 @@ const PostgresMasterSidebar = ({
169154
<div
170155
className="rounded-md border bg-background p-2 font-mono text-sm flex-1 min-h-0"
171156
onKeyDown={(e) => {
172-
if (e.ctrlKey && e.code === "Enter" && queryString?.trim()) {
157+
if ((e.ctrlKey || e.metaKey) && e.code === "Enter" && queryString?.trim()) {
173158
executeQuery(connection);
174159
}
175160
}}
@@ -298,21 +283,17 @@ const PostgresMasterSidebar = ({
298283
<TooltipContent>Open sidebar</TooltipContent>
299284
</Tooltip>
300285
<SheetContent side="left" className="w-80 p-4">
301-
<div className="flex flex-col gap-4 overflow-y-auto pt-10 h-full">
302-
{sidebarContent}
303-
</div>
286+
<div className="flex flex-col gap-4 overflow-y-auto pt-10 h-full">{sidebarContent}</div>
304287
</SheetContent>
305288
</Sheet>
306289
</div>
307290

308291
{/* Desktop view */}
309292
<div className="hidden h-full md:block">
310-
<div className="flex h-full w-76 flex-col gap-4 overflow-y-auto border-r p-4">
311-
{sidebarContent}
312-
</div>
293+
<div className="flex h-full w-76 flex-col gap-4 overflow-y-auto border-r p-4">{sidebarContent}</div>
313294
</div>
314295
</>
315296
);
316297
};
317298

318-
export default PostgresMasterSidebar;
299+
export default PostgresMasterSidebar;

resources/js/pages/postgres/explorer.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,20 @@ const ExplorerContent = ({ connection, breadcrumbs }: ExplorerContentProps) => {
128128
storeRefreshTableData(connection);
129129
}, [connection, storeRefreshTableData]);
130130

131-
const handlePaginationChange = useCallback((newPagination: { page: number; perPage: number; total: number }) => {
132-
const currentPagination = pagination;
133-
setPagination(newPagination);
134-
135-
// If page changed, refresh table data to log the query and update history
136-
if (newPagination.page !== currentPagination.page || newPagination.perPage !== currentPagination.perPage) {
137-
setTimeout(() => {
138-
storeRefreshTableData(connection);
139-
}, 0);
140-
}
141-
}, [pagination, setPagination, connection, storeRefreshTableData]);
131+
const handlePaginationChange = useCallback(
132+
(newPagination: { page: number; perPage: number; total: number }) => {
133+
const currentPagination = pagination;
134+
setPagination(newPagination);
135+
136+
// If page changed, refresh table data to log the query and update history
137+
if (newPagination.page !== currentPagination.page || newPagination.perPage !== currentPagination.perPage) {
138+
setTimeout(() => {
139+
storeRefreshTableData(connection);
140+
}, 0);
141+
}
142+
},
143+
[pagination, setPagination, connection, storeRefreshTableData],
144+
);
142145

143146
return (
144147
<AppLayout breadcrumbs={breadcrumbs}>

resources/js/stores/postgres-store.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
import { DatabaseConnection } from "@/types/database";
2-
import { Column, Filter, Pagination, Schema, Table, TableRecord } from "@/types/postgres";
2+
import { Column, Filter, Pagination, QueryHistoryItem, Schema, Table, TableRecord } from "@/types/postgres";
33
import axios, { AxiosError } from "axios";
44
import { create } from "zustand";
55

6-
// Define the sorting option interface
76
interface SortOption {
87
id: string;
98
column: string;
109
direction: "asc" | "desc";
1110
}
1211

13-
// Define query history item interface
14-
interface QueryHistoryItem {
15-
id: number;
16-
database_connection_id: number;
17-
query: string;
18-
executor: string;
19-
executor_id: number | null;
20-
type: string;
21-
description: string;
22-
created_at: string;
23-
updated_at: string;
24-
}
25-
2612
export interface PostgresStoreState {
2713
selectedSchema: string | null;
2814
schemas: Schema[];
@@ -457,7 +443,7 @@ export const postgresStore = create<PostgresStoreState>((set, get) => ({
457443
const { selectedTable } = get();
458444
if (selectedTable) {
459445
await get().fetchTableData(connection, selectedTable);
460-
446+
461447
// If we're in query mode, refresh the query history to show any new filter/sort queries
462448
const { isQueryMode } = get();
463449
if (isQueryMode) {

resources/js/types/postgres.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@ export interface ConnectionStatus {
5959
connection_error: string | null;
6060
last_tested: string;
6161
}
62+
63+
export interface QueryHistoryItem {
64+
id: string;
65+
database_connection_id: string;
66+
query: string;
67+
executor: string;
68+
executor_id: string | null;
69+
type: string;
70+
description: string;
71+
created_at: string;
72+
updated_at: string;
73+
}

0 commit comments

Comments
 (0)