Skip to content

Commit 8df40ca

Browse files
committed
Use our useShortcutKeys component instead
1 parent 9fafcb5 commit 8df40ca

1 file changed

Lines changed: 10 additions & 26 deletions

File tree

apps/webapp/app/components/metrics/QueryWidget.tsx

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import { DialogClose } from "@radix-ui/react-dialog";
66
import { IconBraces, IconChartHistogram, IconFileTypeCsv } from "@tabler/icons-react";
77
import { assertNever } from "assert-never";
88
import { Maximize2 } from "lucide-react";
9-
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
9+
import { useCallback, useRef, useState, type ReactNode } from "react";
1010
import { z } from "zod";
1111
import { Card } from "~/components/primitives/charts/Card";
1212
import { ShortcutKey } from "~/components/primitives/ShortcutKey";
1313
import { SimpleTooltip } from "~/components/primitives/Tooltip";
14+
import { useShortcutKeys } from "~/hooks/useShortcutKeys";
1415
import { cn } from "~/utils/cn";
1516
import { rowsToCSV, rowsToJSON } from "~/utils/dataExport";
1617
import { QueryResultsChart } from "../code/QueryResultsChart";
@@ -180,32 +181,15 @@ export function QueryWidget({
180181
const hasEditActions = onEdit || onRename || onDelete || onDuplicate;
181182
const hasData = props.data.rows.length > 0;
182183

183-
// "v" to fullscreen the hovered widget
184-
useEffect(() => {
185-
const handleKeyDown = (e: KeyboardEvent) => {
186-
if (isRenameDialogOpen || isMenuOpen) return;
187-
if (e.key !== "v" || e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) return;
188-
189-
// Ignore when typing in inputs/textareas/contenteditable
190-
const target = e.target as HTMLElement;
191-
if (
192-
target.tagName === "INPUT" ||
193-
target.tagName === "TEXTAREA" ||
194-
target.tagName === "SELECT" ||
195-
target.isContentEditable
196-
) {
197-
return;
198-
}
199-
200-
// When not fullscreen, require hover to activate
201-
if (!isFullscreen && !containerRef.current?.matches(":hover")) return;
202-
203-
e.preventDefault();
184+
// "v" to toggle fullscreen on hovered widget
185+
useShortcutKeys({
186+
shortcut: { key: "v" },
187+
action: useCallback(() => {
188+
const isHovered = containerRef.current?.matches(":hover");
189+
if (!isFullscreen && !isHovered) return;
204190
setIsFullscreen((prev) => !prev);
205-
};
206-
document.addEventListener("keydown", handleKeyDown);
207-
return () => document.removeEventListener("keydown", handleKeyDown);
208-
}, [isFullscreen, isRenameDialogOpen, isMenuOpen]);
191+
}, [isFullscreen]),
192+
});
209193

210194
const copyToClipboard = useCallback((text: string) => {
211195
navigator.clipboard.writeText(text);

0 commit comments

Comments
 (0)