diff --git a/src/src/isomorphic/generic/components/route-modal/index.ts b/src/src/isomorphic/generic/components/route-modal/index.ts new file mode 100644 index 0000000..94a56ce --- /dev/null +++ b/src/src/isomorphic/generic/components/route-modal/index.ts @@ -0,0 +1,2 @@ +export { RouteModal } from "./main"; +export type { RouteModalInput } from "./types"; diff --git a/src/src/isomorphic/generic/components/route-modal/main.tsx b/src/src/isomorphic/generic/components/route-modal/main.tsx new file mode 100644 index 0000000..8ebb86c --- /dev/null +++ b/src/src/isomorphic/generic/components/route-modal/main.tsx @@ -0,0 +1,35 @@ +"use client"; + +import { Modal } from "@mantine/core"; +import { useRouter } from "next/navigation"; +import { useCallback } from "react"; + +import type { RouteModalInput } from "./types"; + +import { useLocalization } from "../../../localization/hooks/use-localization"; +import { useHistory } from "../../hooks/use-history"; + +export function RouteModal({ fallback, title, ...props }: RouteModalInput) { + const router = useRouter(); + + const { history } = useHistory(); + const { localization } = useLocalization(); + + const handleClose = useCallback(() => { + if (history.entries.length > 1) router.back(); + else router.push(fallback); + }, [fallback, history.entries.length, router]); + + return ( + + ); +} diff --git a/src/src/isomorphic/generic/components/route-modal/types.ts b/src/src/isomorphic/generic/components/route-modal/types.ts new file mode 100644 index 0000000..33fb7ee --- /dev/null +++ b/src/src/isomorphic/generic/components/route-modal/types.ts @@ -0,0 +1,10 @@ +import type { MessageDescriptor } from "@lingui/core"; +import type { ModalProps } from "@mantine/core"; + +export type RouteModalInput = Omit< + ModalProps, + "onClose" | "opened" | "title" +> & { + fallback: string; + title?: MessageDescriptor | string; +};