Skip to content

Commit

Permalink
#11 added query history
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Jul 28, 2024
1 parent 6c3b8e0 commit c1d893d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 6 deletions.
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-hover-card": "^1.1.1",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-scroll-area": "^1.1.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
Expand Down
23 changes: 19 additions & 4 deletions src/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import {
} from "./ui/drawer";
import { Input } from "./ui/input";
import { Button } from "./ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";

import { Settings2Icon } from "lucide-react";

const ROWS_PER_PAGE_KEY = "rowsPerPage";

export default function Settings() {
const { setRowPerPageOrAuto, setIsCustomQuery } = useSQLiteStore();
const { setRowPerPageOrAuto, setIsCustomQuery, queryHestory } =
useSQLiteStore();
const [selectedRowsPerPage, setSelectedRowsPerPage] = useState<number | null>(
null
);
Expand Down Expand Up @@ -124,11 +127,23 @@ export default function Settings() {
<span>Save</span>
</Button>
</div>
{/* <div>
<div>
<p className="mb-1 text-sm text-muted-foreground">
Soon
Query History
</p>
</div> */}
<ScrollArea className="h-72 rounded-md border">
<div className="p-4">
{queryHestory.map((query, index) => (
<>
<div key={index} className="text-sm">
{query}
</div>
<Separator className="my-2" />
</>
))}
</div>
</ScrollArea>
</div>
</div>
<DrawerFooter>
<DrawerClose asChild>
Expand Down
46 changes: 46 additions & 0 deletions src/components/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from "react";
import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";

import { cn } from "@/lib/utils";

const ScrollArea = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn("relative overflow-hidden", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
));
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;

const ScrollBar = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
>(({ className, orientation = "vertical", ...props }, ref) => (
<ScrollAreaPrimitive.ScrollAreaScrollbar
ref={ref}
orientation={orientation}
className={cn(
"flex touch-none select-none transition-colors",
orientation === "vertical" &&
"h-full w-2.5 border-l border-l-transparent p-[1px]",
orientation === "horizontal" &&
"h-2.5 flex-col border-t border-t-transparent p-[1px]",
className
)}
{...props}
>
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" />
</ScrollAreaPrimitive.ScrollAreaScrollbar>
));
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;

export { ScrollArea, ScrollBar };
4 changes: 3 additions & 1 deletion src/hooks/useQueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function useQueryData(
page: number,
isCustomQuery: boolean
) {
const { db, setQueryError, setIsCustomQuery, query } = useSQLiteStore();
const { db, setQueryError, setIsCustomQuery, query, appendToQueryHestory } =
useSQLiteStore();

const [data, setData] = useState<TableRow[]>([]);
const [columns, setColumns] = useState<string[]>([]);
Expand All @@ -34,6 +35,7 @@ export function useQueryData(
if (error instanceof Error) setQueryError(error.message);
} finally {
setIsQueryLoading(false);
appendToQueryHestory(queryString);
}
})();
}
Expand Down
10 changes: 9 additions & 1 deletion src/store/useSQLiteStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ interface SQLiteState {
setRowPerPageOrAuto: (value: number | "auto") => void;
isCustomQuery: boolean;
setIsCustomQuery: (value: boolean) => void;
queryHestory: string[];
setQueryHestory: (value: string[]) => void;
appendToQueryHestory: (value: string) => void;
}

const initializeStore = create<SQLiteState>((set, get) => ({
Expand Down Expand Up @@ -84,7 +87,12 @@ const initializeStore = create<SQLiteState>((set, get) => ({
setRowPerPageOrAuto: (value: number | "auto") =>
set({ rowPerPageOrAuto: value }),

setIsCustomQuery: (value: boolean) => set({ isCustomQuery: value })
setIsCustomQuery: (value: boolean) => set({ isCustomQuery: value }),

queryHestory: [],
setQueryHestory: (value: string[]) => set({ queryHestory: value }),
appendToQueryHestory: (value: string) =>
set((state) => ({ queryHestory: [...state.queryHestory, value] }))
}));

export default initializeStore;

0 comments on commit c1d893d

Please sign in to comment.