diff --git a/apps/client/src/App.tsx b/apps/client/src/App.tsx index 4399062f..6fab378c 100644 --- a/apps/client/src/App.tsx +++ b/apps/client/src/App.tsx @@ -30,10 +30,12 @@ import SharedPage from "@/pages/share/shared-page.tsx"; import Shares from "@/pages/settings/shares/shares.tsx"; import ShareLayout from "@/features/share/components/share-layout.tsx"; import ShareRedirect from '@/pages/share/share-redirect.tsx'; +import { useTrackOrigin } from "@/hooks/use-track-origin"; export default function App() { const { t } = useTranslation(); useRedirectToCloudSelect(); + useTrackOrigin(); return ( <> @@ -59,7 +61,7 @@ export default function App() { } /> } /> - + } /> } /> diff --git a/apps/client/src/components/settings/atoms/settings-origin-atom.ts b/apps/client/src/components/settings/atoms/settings-origin-atom.ts new file mode 100644 index 00000000..36ea889b --- /dev/null +++ b/apps/client/src/components/settings/atoms/settings-origin-atom.ts @@ -0,0 +1,10 @@ +import { atom, WritableAtom } from "jotai"; + +export const settingsOriginAtom: WritableAtom = atom( + null, + (get, set, newValue) => { + if (get(settingsOriginAtom) !== newValue) { + set(settingsOriginAtom, newValue); + } + } +); diff --git a/apps/client/src/components/settings/settings-sidebar.tsx b/apps/client/src/components/settings/settings-sidebar.tsx index 483c0026..79ee511d 100644 --- a/apps/client/src/components/settings/settings-sidebar.tsx +++ b/apps/client/src/components/settings/settings-sidebar.tsx @@ -13,7 +13,7 @@ import { IconKey, IconWorld, } from "@tabler/icons-react"; -import { Link, useLocation, useNavigate } from "react-router-dom"; +import { Link, useLocation } from "react-router-dom"; import classes from "./settings.module.css"; import { useTranslation } from "react-i18next"; import { isCloud } from "@/lib/config.ts"; @@ -32,6 +32,7 @@ import { import AppVersion from "@/components/settings/app-version.tsx"; import { mobileSidebarAtom } from "@/components/layouts/global/hooks/atoms/sidebar-atom.ts"; import { useToggleSidebar } from "@/components/layouts/global/hooks/hooks/use-toggle-sidebar.ts"; +import { useSettingsNavigation } from "@/hooks/use-settings-navigation"; interface DataItem { label: string; @@ -105,7 +106,7 @@ export default function SettingsSidebar() { const { t } = useTranslation(); const location = useLocation(); const [active, setActive] = useState(location.pathname); - const navigate = useNavigate(); + const { goBack } = useSettingsNavigation(); const { isAdmin } = useUserRole(); const [workspace] = useAtom(workspaceAtom); const [mobileSidebarOpened] = useAtom(mobileSidebarAtom); @@ -210,7 +211,12 @@ export default function SettingsSidebar() {
navigate(-1)} + onClick={() => { + goBack(); + if (mobileSidebarOpened) { + toggleMobileSidebar(); + } + }} variant="transparent" c="gray" aria-label="Back" diff --git a/apps/client/src/hooks/use-settings-navigation.ts b/apps/client/src/hooks/use-settings-navigation.ts new file mode 100644 index 00000000..389445c5 --- /dev/null +++ b/apps/client/src/hooks/use-settings-navigation.ts @@ -0,0 +1,14 @@ +import { settingsOriginAtom } from "@/components/settings/atoms/settings-origin-atom"; +import { useAtomValue } from "jotai"; +import { useNavigate } from "react-router-dom"; + +export function useSettingsNavigation() { + const navigate = useNavigate(); + const origin = useAtomValue(settingsOriginAtom); + + const goBack = () => { + navigate(origin ?? "/home", { replace: true }); + }; + + return { goBack }; +} \ No newline at end of file diff --git a/apps/client/src/hooks/use-track-origin.ts b/apps/client/src/hooks/use-track-origin.ts new file mode 100644 index 00000000..089dffbe --- /dev/null +++ b/apps/client/src/hooks/use-track-origin.ts @@ -0,0 +1,16 @@ +import { settingsOriginAtom } from "@/components/settings/atoms/settings-origin-atom"; +import { useAtomValue, useSetAtom } from "jotai"; +import { useEffect } from "react"; +import { useLocation } from "react-router-dom"; + +export function useTrackOrigin() { + const location = useLocation(); + const setOrigin = useSetAtom(settingsOriginAtom); + + useEffect(() => { + const isInSettings = location.pathname.startsWith("/settings"); + if (!isInSettings) { + setOrigin(location.pathname); + } + }, [location.pathname, setOrigin]); +} \ No newline at end of file