Skip to content

Commit

Permalink
fix: prevent spamming keyboard shortcuts
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Sep 18, 2023
1 parent 1a56b74 commit aa1d081
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
20 changes: 15 additions & 5 deletions src/components/Budget/BudgetPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChartsPage/ChartsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface GraphProps {
function ChartsPage({ onShowGraphs }: GraphProps) {
const { budgetList } = useBudget();

useHotkeys("i", () => onShowGraphs(), {
useHotkeys("i", (e) => !e.repeat && onShowGraphs(), {
preventDefault: true,
});

Expand Down
27 changes: 21 additions & 6 deletions src/components/NavBar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>),
{ 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<HTMLInputElement>,
),
Expand Down
8 changes: 6 additions & 2 deletions src/components/StatCard/StatCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ function StatCard({ onChange, onAutoGoal, onShowGraphs }: StatCardProps) {
const reservesRef =
useRef<HTMLInputElement>() as React.MutableRefObject<HTMLInputElement>;

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<HTMLInputElement>) {
let updatedStat: Stat;
Expand Down

0 comments on commit aa1d081

Please sign in to comment.