diff --git a/src/lib/components/ConfirmDialog.stories.tsx b/src/lib/components/ConfirmDialog.stories.tsx new file mode 100644 index 0000000..33a5063 --- /dev/null +++ b/src/lib/components/ConfirmDialog.stories.tsx @@ -0,0 +1,39 @@ +import type { Meta, StoryObj } from "@storybook/react"; +import useConfirm, { ConfirmationDialog } from "./ConfirmDialog"; + +const meta = { + title: "Components/ConfirmDialog", + component: ConfirmationDialog, +} satisfies Meta; + +export default meta; + +type Story = StoryObj; + +export const Default: Story = { + args: { + message: "Are you sure you want to proceed?", + open: false, + handleConfirm: () => {}, + handleCancel: () => {}, + }, + render: (args) => { + const { ConfirmationDialog, confirm } = useConfirm({ + message: args.message, + }); + + const handleClick = async () => { + const result = await confirm(); + alert(`Confirmed: ${result}`); + }; + + return ( +
+ + +
+ ); + }, +}; diff --git a/src/lib/components/ConfirmDialog.tsx b/src/lib/components/ConfirmDialog.tsx new file mode 100644 index 0000000..baf1bd3 --- /dev/null +++ b/src/lib/components/ConfirmDialog.tsx @@ -0,0 +1,97 @@ +import { useState } from "react"; + +export const ConfirmationDialog = ({ + title, + message, + open, + handleConfirm, + handleCancel, + cancelText = "Cancel", + okText = "OK", +}: { + title?: string; + message: string; + open: boolean; + cancelText?: string; + okText?: string; + handleConfirm: () => void; + handleCancel: () => void; +}) => ( + +
+ {title &&

{title}

} +

{message}

+
+
+
+ + +
+
+
+
+
+); + +const useConfirm = ({ + title = "Confirm", + message = "Are you sure you want to proceed?", + cancelText = "Cancel", + okText = "OK", +}: { + title?: string; + message?: string; + cancelText?: string; + okText?: string; +}) => { + const [promise, setPromise] = useState(null); + + const confirm = () => { + return new Promise((resolve, reject) => { + setPromise({ resolve }); + }); + }; + + const handleClose = () => { + setPromise(null); + }; + + const handleConfirm = () => { + promise?.resolve(true); + handleClose(); + }; + + const handleCancel = () => { + promise?.resolve(false); + handleClose(); + }; + + return { + ConfirmationDialog: () => ( + + ), + confirm, + }; +}; + +export default useConfirm; diff --git a/src/options.tsx b/src/options.tsx index 66403ce..f0e188c 100644 --- a/src/options.tsx +++ b/src/options.tsx @@ -1,6 +1,7 @@ import UserCard from "~lib/components/UserCard"; import { useBskyUserManager } from "~lib/hooks/useBskyUserManager"; import "./style.css"; +import useConfirm from "~lib/components/ConfirmDialog"; import Sidebar from "~lib/components/Sidebar"; const Option = () => { @@ -15,17 +16,22 @@ const Option = () => { matchTypeStats, } = useBskyUserManager(); + const { confirm, ConfirmationDialog } = useConfirm({ + title: "Proceed with Execution?", + message: + "User detection is not perfect and may include false positives. Do you still want to proceed?", + cancelText: "Cancel", + okText: "OK", + }); + const handleActionAll = async () => { - if ( - !window.confirm( - "User detection is not perfect and may include false positives. Do you still want to proceed?", - ) - ) { + if (!(await confirm())) { return; } await actionAll(); }; + return ( <>
@@ -55,6 +61,7 @@ const Option = () => {
+ );