Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight selected extension or tool from hash #68

Merged
merged 2 commits into from
Jan 13, 2024
Merged
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
47 changes: 46 additions & 1 deletion src/components/AwesomeArcadeList/extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { copyTextToClipboard } from "@/scripts/Utils/Clipboard";
import Link from "next/link";
import { smoothScrollHash } from "@/components/AwesomeArcadeList/linkableHeader";
import { AnalyticEvents } from "@/components/Analytics";
import { useRouter } from "next/router";

export function AwesomeArcadeExtension({
ext,
highlight,
showImportURL,
pad,
}: {
ext: Extension;
highlight?: boolean | undefined;
showImportURL?: boolean | undefined;
pad?: boolean | undefined;
}): JSX.Element {
Expand Down Expand Up @@ -62,7 +65,12 @@ export function AwesomeArcadeExtension({
}, [tooltip]);

return (
<div className={`card ${pad ? "mb-2" : ""} h-100`} id={ext.repo}>
<div
className={`card ${pad ? "mb-2" : ""} ${
highlight ? "border-primary border-3" : ""
} h-100`}
id={ext.repo}
>
<div className="card-body">
<h5
className="card-title"
Expand Down Expand Up @@ -216,6 +224,42 @@ export function AwesomeArcadeExtensionGroup({
showImportURL?: boolean | undefined;
pad?: boolean | undefined;
}): JSX.Element {
const router = useRouter();

const [extToHighlight, setExtToHighlight] = React.useState<
string | undefined
>(undefined);

const onHashChange = (e?: HashChangeEvent | undefined) => {
const repo = e
? e.newURL.split("#")[1]
: window.location.hash.replaceAll("#", "");
console.log(`Changing extension to highlight ${repo}`);
setExtToHighlight(repo);
};

React.useEffect(() => {
setTimeout(() => {
onHashChange();
});

window.addEventListener("hashchange", onHashChange);
return () => {
window.removeEventListener("hashchange", onHashChange);
};
}, []);

React.useEffect(() => {
router.events.on("routeChangeComplete", () => {
onHashChange();
});

return () =>
router.events.off("routeChangeComplete", () => {
onHashChange();
});
}, [router.events]);

return (
<div className={pad == undefined || pad ? "mb-3" : ""}>
{title}
Expand All @@ -227,6 +271,7 @@ export function AwesomeArcadeExtensionGroup({
<div className="col py-3" key={ext.repo}>
<AwesomeArcadeExtension
ext={ext}
highlight={ext.repo === extToHighlight}
showImportURL={showImportURL}
pad={i < exts.length - 1}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/components/AwesomeArcadeList/linkableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ export function smoothScrollHash(
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>
) {
e.preventDefault();
const hash = e.currentTarget.href.split("#")[1];
const href = e.currentTarget.href;
setTimeout(() => {
const hash = href.split("#")[1];
console.log(`Smooth scrolling to ${hash}`);
smoothScrollToID(hash);
window.dispatchEvent(new HashChangeEvent("hashchange", { newURL: href }));
});
}

export function smoothScrollToID(id: string) {
const e = document.getElementById(id);
e?.scrollIntoView({
behavior: "smooth",
block: "center",
});
const u = new URL(window.location.toString());
u.hash = id;
Expand Down
52 changes: 50 additions & 2 deletions src/components/AwesomeArcadeList/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import React from "react";
import Link from "next/link";
import { smoothScrollHash } from "@/components/AwesomeArcadeList/linkableHeader";
import { AnalyticEvents } from "@/components/Analytics";
import { useRouter } from "next/router";

export function AwesomeArcadeTool({
tool,
highlight,
pad,
}: {
tool: Tool;
highlight?: boolean | undefined;
pad?: boolean | undefined;
}): JSX.Element {
const [showCardLink, setShowCardLink] = React.useState(false);

return (
<div className={`card ${pad ? "mb-2" : ""} h-100`} id={tool.repo}>
<div
className={`card ${pad ? "mb-2" : ""} ${
highlight ? "border-primary border-3" : ""
} h-100`}
id={tool.repo}
>
<div className="card-body">
<h5
className="card-title"
Expand Down Expand Up @@ -145,6 +153,42 @@ export function AwesomeArcadeToolGroup({
tools: Tool[];
pad?: boolean | undefined;
}): JSX.Element {
const router = useRouter();

const [toolToHighlight, setToolToHighlight] = React.useState<
string | undefined
>(undefined);

const onHashChange = (e?: HashChangeEvent | undefined) => {
const repo = e
? e.newURL.split("#")[1]
: window.location.hash.replaceAll("#", "");
console.log(`Changing tool to highlight ${repo}`);
setToolToHighlight(repo);
};

React.useEffect(() => {
setTimeout(() => {
onHashChange();
});

window.addEventListener("hashchange", onHashChange);
return () => {
window.removeEventListener("hashchange", onHashChange);
};
}, []);

React.useEffect(() => {
router.events.on("routeChangeComplete", () => {
onHashChange();
});

return () =>
router.events.off("routeChangeComplete", () => {
onHashChange();
});
}, [router.events]);

return (
<div className={pad == undefined || pad ? "mb-3" : ""}>
{title}
Expand All @@ -154,7 +198,11 @@ export function AwesomeArcadeToolGroup({
{tools.map((tool, i) => {
return (
<div className="col py-3" key={tool.repo}>
<AwesomeArcadeTool tool={tool} pad={i < tools.length - 1} />
<AwesomeArcadeTool
tool={tool}
highlight={tool.repo === toolToHighlight}
pad={i < tools.length - 1}
/>
</div>
);
})}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ LayoutProps): JSX.Element {
const baseURL = (() => {
switch (appProps.environment) {
case "production": {
return "https://awesome-arcade-extensions.vercel.app";
return "https://awesome-arcade.vercel.app";
}
case "preview": {
return "https://awesome-arcade-extensions-beta.vercel.app";
return "https://awesome-arcade-beta.vercel.app";
}
case "development": {
return "http://localhost:3000";
Expand Down
9 changes: 9 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Adsense from "../components/Adsense";
import Analytics from "../components/Analytics";
import ErrorBoundary from "../components/ErrorBoundary";
import { SessionProvider } from "next-auth/react";
import { useRouter } from "next/router";

export default function AwesomeArcadeExtensions({
Component,
Expand All @@ -17,6 +18,14 @@ export default function AwesomeArcadeExtensions({
import("bootstrap");
}, []);

const router = useRouter();

React.useEffect(() => {
window.addEventListener("popstate", () => {
router.push(window.location.href);
});
}, [router]);

return (
<ErrorBoundary>
<NextNProgress color="#FFF603" options={{ showSpinner: false }} />
Expand Down
Loading