Skip to content

Commit

Permalink
Highlight selected extension or tool from hash
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
UnsignedArduino committed Jan 13, 2024
1 parent 7ee25de commit 7447d9d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
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

0 comments on commit 7447d9d

Please sign in to comment.