From aa1d081fd9e254d4c6e3a8c2f2065deaf8064f44 Mon Sep 17 00:00:00 2001 From: rare-magma Date: Mon, 18 Sep 2023 19:58:20 +0200 Subject: [PATCH] fix: prevent spamming keyboard shortcuts Signed-off-by: rare-magma --- src/components/Budget/BudgetPage.tsx | 20 +++++++++++++----- src/components/ChartsPage/ChartsPage.tsx | 2 +- src/components/NavBar/NavBar.tsx | 27 ++++++++++++++++++------ src/components/StatCard/StatCard.tsx | 8 +++++-- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/components/Budget/BudgetPage.tsx b/src/components/Budget/BudgetPage.tsx index 8967c7e..861407f 100644 --- a/src/components/Budget/BudgetPage.tsx +++ b/src/components/Budget/BudgetPage.tsx @@ -48,15 +48,25 @@ function BudgetPage() { const { setIntlConfig, handleCurrency } = useConfig(); - useHotkeys("s", () => handleExportJSON(), { + useHotkeys("s", (e) => !e.repeat && handleExportJSON(), { preventDefault: true, }); - useHotkeys("d", () => handleExportCSV(), { preventDefault: true }); - useHotkeys("a", () => !showGraphs && handleNew(), { preventDefault: true }); - useHotkeys("c", () => !showGraphs && handleClone(), { preventDefault: true }); - useHotkeys("i", () => !showGraphs && budget && setShowGraphs(true), { + useHotkeys("d", (e) => !e.repeat && handleExportCSV(), { preventDefault: true, }); + useHotkeys("a", (e) => !e.repeat && !showGraphs && handleNew(), { + preventDefault: true, + }); + useHotkeys("c", (e) => !e.repeat && !showGraphs && handleClone(), { + preventDefault: true, + }); + useHotkeys( + "i", + (e) => !e.repeat && !showGraphs && budget && setShowGraphs(true), + { + preventDefault: true, + }, + ); function handleError(e: unknown) { if (e instanceof Error) setError(e.message); diff --git a/src/components/ChartsPage/ChartsPage.tsx b/src/components/ChartsPage/ChartsPage.tsx index 29eb4c8..b19a46d 100644 --- a/src/components/ChartsPage/ChartsPage.tsx +++ b/src/components/ChartsPage/ChartsPage.tsx @@ -19,7 +19,7 @@ interface GraphProps { function ChartsPage({ onShowGraphs }: GraphProps) { const { budgetList } = useBudget(); - useHotkeys("i", () => onShowGraphs(), { + useHotkeys("i", (e) => !e.repeat && onShowGraphs(), { preventDefault: true, }); diff --git a/src/components/NavBar/NavBar.tsx b/src/components/NavBar/NavBar.tsx index b3aecc4..08aa67d 100644 --- a/src/components/NavBar/NavBar.tsx +++ b/src/components/NavBar/NavBar.tsx @@ -67,19 +67,34 @@ function NavBar({ const { currency, handleCurrency } = useConfig(); const { budget, budgetNameList } = useBudget(); - useHotkeys("pageup", () => handleGoForward(), { preventDefault: true }); - useHotkeys("pagedown", () => handleGoBack(), { preventDefault: true }); - useHotkeys("Home", () => handleGoHome(), { preventDefault: true }); + useHotkeys("pageup", (e) => !e.repeat && handleGoForward(), { + preventDefault: true, + }); + + useHotkeys("pagedown", (e) => !e.repeat && handleGoBack(), { + preventDefault: true, + }); + + useHotkeys("Home", (e) => !e.repeat && handleGoHome(), { + preventDefault: true, + }); + useHotkeys( ["/", "f"], - () => + (e) => + !e.repeat && focusRef(typeRef as unknown as React.MutableRefObject), { preventDefault: true }, ); - useHotkeys("r", () => focusRef(nameRef), { preventDefault: true }); + + useHotkeys("r", (e) => !e.repeat && focusRef(nameRef), { + preventDefault: true, + }); + useHotkeys( "t", - () => + (e) => + !e.repeat && focusRef( currencyRef as unknown as React.MutableRefObject, ), diff --git a/src/components/StatCard/StatCard.tsx b/src/components/StatCard/StatCard.tsx index 2b0e886..47744c2 100644 --- a/src/components/StatCard/StatCard.tsx +++ b/src/components/StatCard/StatCard.tsx @@ -36,8 +36,12 @@ function StatCard({ onChange, onAutoGoal, onShowGraphs }: StatCardProps) { const reservesRef = useRef() as React.MutableRefObject; - useHotkeys("g", () => focusRef(goalRef), { preventDefault: true }); - useHotkeys("e", () => focusRef(reservesRef), { preventDefault: true }); + useHotkeys("g", (e) => !e.repeat && focusRef(goalRef), { + preventDefault: true, + }); + useHotkeys("e", (e) => !e.repeat && focusRef(reservesRef), { + preventDefault: true, + }); function handleInputChange(item: React.ChangeEvent) { let updatedStat: Stat;