Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/components/BackButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import { Button } from "@/components/ui/button";
import type { ReactNode } from "react";
import { MdArrowBack } from "react-icons/md";
import { useEffect } from "react";

const VISITED_PATHS_KEY = "visited-paths";
const MAX_VISITED_PATHS = 10;

export const BackButton = (props: {
href: string;
buttonLabel?: ReactNode;
contextLabel?: ReactNode;
}) => {
useEffect(() => {
const currentPath = window.location.pathname;
const storedPaths = sessionStorage.getItem(VISITED_PATHS_KEY);
let visitedPaths = storedPaths ? JSON.parse(storedPaths) : [];

if (!visitedPaths.includes(currentPath)) {
visitedPaths.push(currentPath);

if (visitedPaths.length > MAX_VISITED_PATHS) {
visitedPaths = visitedPaths.slice(-MAX_VISITED_PATHS);
}

sessionStorage.setItem(VISITED_PATHS_KEY, JSON.stringify(visitedPaths));
}
}, []);

const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
const referrer = document.referrer;
const hasReferrer = !!referrer && referrer.includes(window.location.host);
const hasHistory = window.history.length > 1;

const storedPaths = sessionStorage.getItem(VISITED_PATHS_KEY);
const visitedPaths = storedPaths ? JSON.parse(storedPaths) : [];
const hasVisitedPaths = visitedPaths.length > 1;

if (hasHistory && (hasReferrer || hasVisitedPaths)) {
e.preventDefault();
window.history.back();
}
};

return (
<div className="flex items-center gap-4">
<Button asChild variant="secondary-dark" size="xs">
<a
href={props.href}
onClick={(e) => {
if (
window.history.length > 1 &&
document.referrer.indexOf(window.location.host) !== -1
) {
e.preventDefault();
window.history.back();
}
}}
>
<a href={props.href} onClick={handleClick}>
<MdArrowBack className="mr-2" /> {props.buttonLabel ?? "Back"}
</a>
</Button>
Expand Down
16 changes: 16 additions & 0 deletions src/layouts/RootLayout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import { ClientRouter } from "astro:transitions";
import Analytics from "@vercel/analytics/astro";
import "@fontsource/tomorrow/400.css";
import "@fontsource/tomorrow/500.css";
Expand Down Expand Up @@ -30,8 +31,23 @@ import { Toaster } from "@/components/ui/sonner";
/>
<meta name="generator" content={Astro.generator} />
<meta name="theme-color" content="#171717" />
<ClientRouter />
<slot name="seo" />
<slot name="ld+json" />

<script>
document.addEventListener(
"astro:page-load",
() => {
document.documentElement.dataset.previousUrl = window.location.href;

window.addEventListener("popstate", () => {
document.documentElement.dataset.previousUrl = window.location.href;
});
},
{ once: true },
);
</script>
</head>
<body
class="flex min-h-svh flex-col overflow-x-hidden overflow-y-scroll bg-black text-foreground"
Expand Down