Skip to content

Commit

Permalink
fix bug click inside the task builder unselects the task and switch t…
Browse files Browse the repository at this point in the history
…o create mode
  • Loading branch information
vuvincent committed Sep 23, 2023
1 parent 0e0c31a commit d479f54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
32 changes: 22 additions & 10 deletions apps/desktop-v2/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { ISupernovaTask } from "@supernova/types";
import { GearIcon } from "@radix-ui/react-icons";
import Link from "next/link";
import { settingsRoute } from "./settings/meta";
import Image from "next/image";
import { SupernovaCommandCenter } from "../components/command-center";
import { SupernovaCommand } from "../types/command";
import { AlertDialog } from "../components/alert-dialog";
Expand Down Expand Up @@ -48,6 +47,10 @@ function Home() {
useState<boolean>(false);
const { makeToast } = useSupernovaToast();

const anyModalOpen = useMemo(() => {
return taskBuilderIsOpen || showAreYouSureDialog;
}, [showAreYouSureDialog, taskBuilderIsOpen]);

const handleCheckTask = useCallback(
(taskId: string) => async (value: boolean) => {
const foundTask = tasks.find((task) => task.id === taskId);
Expand Down Expand Up @@ -76,7 +79,7 @@ function Home() {
// TODO: show error toast
}
},
[tasks]
[makeToast, tasks]
);

const handleClickTask = (taskIndex: number) => () => {
Expand Down Expand Up @@ -162,15 +165,18 @@ function Home() {
}
}
},
[chosenTaskIndex, tasks]
[chosenTaskIndex, makeToast, tasks]
);

const openTaskBuilder = useCallback((e?: Mousetrap.ExtendedKeyboardEvent) => {
e?.preventDefault(); // prevent typing into the task builder
setTaskBuilderIsOpen(true);
// deselect task
setChosenTaskIndex(-1);
}, []);
const openTaskBuilder = useCallback(
(e?: Mousetrap.ExtendedKeyboardEvent) => {
e?.preventDefault(); // prevent typing into the task builder
setTaskBuilderIsOpen(true);
// deselect task
setChosenTaskIndex(-1);
},
[setChosenTaskIndex]
);

const commands: SupernovaCommand[] = useMemo(
() => [
Expand All @@ -195,7 +201,7 @@ function Home() {
cb: (e) => {
e?.preventDefault(); // prevent typing into the task builder
// if modal is open, then don't open another one
if (taskBuilderIsOpen) {
if (anyModalOpen) {
return;
}

Expand Down Expand Up @@ -279,6 +285,8 @@ function Home() {
commands,
handleCheckTask,
handleDeleteTask,
setChosenTaskIndex,
showAreYouSureDialog,
taskBuilderIsOpen,
tasks,
]);
Expand Down Expand Up @@ -324,7 +332,11 @@ function Home() {
}, [refetchTasks]);

// if the user press outside of the task list (i.e the task builder), then unselect the task
// only unselect if no modal is open since we don't want to unselect when the user clicks into any modal
useOutsideClick(taskListRef, () => {
if (anyModalOpen) {
return;
}
setChosenTaskIndex(-1);
});

Expand Down
17 changes: 10 additions & 7 deletions apps/desktop-v2/hooks/useOutsideClick.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { useEffect, useRef } from "react";
import { useCallback, useEffect, useRef } from "react";

const useOutsideClick = <T extends HTMLElement>(
ref: React.RefObject<T>,
callback: () => void
) => {
const handleClick = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback();
}
};
const handleClick = useCallback(
(event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback();
}
},
[callback, ref]
);

useEffect(() => {
document.addEventListener("click", handleClick);

return () => {
document.removeEventListener("click", handleClick);
};
}, []);
}, [handleClick]);
};

export default useOutsideClick;

1 comment on commit d479f54

@vercel
Copy link

@vercel vercel bot commented on d479f54 Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.