query param reader

This commit is contained in:
2026-04-11 14:37:31 -05:00
parent 144a81f8a4
commit 0513ff5585
3 changed files with 34 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
import {createContext, PropsWithChildren, useContext, useState} from "react";
import {createContext, PropsWithChildren, useContext, useEffect, useState} from "react";
import {Reader} from "./Reader";
import {Article} from "./articles";
import {Article, ARTICLES} from "./articles";
import {useRouter} from "../router/RouterContext";
import {Path} from "../router/router";
const articleContext =
createContext<ReturnType<typeof useState<Article | undefined>>>([undefined, function(){}]);
@@ -9,9 +11,33 @@ export function useArticle() {
return useContext(articleContext);
}
const ARTICLE_QUERY = "a";
export default function ArticleProvider({ children }: PropsWithChildren) {
const state = useState<Article | undefined>(undefined);
return <articleContext.Provider value={state}>
const [article, setArticle] = useState<Article | undefined>(undefined);
const router = useRouter();
useEffect(() => {
const params = new URLSearchParams(location.search);
if(params.has(ARTICLE_QUERY)) {
const id = Number.parseInt(params.get(ARTICLE_QUERY)!);
setArticle(ARTICLES[id]);
}
}, [setArticle]);
useEffect(() => {
if(!article) {
// query param
const params = new URLSearchParams(location.search);
if(params.has(ARTICLE_QUERY)) {
router.replacePage(location.pathname as Path);
}
} else {
const id = ARTICLES.indexOf(article);
router.replacePage(`${location.pathname}?${ARTICLE_QUERY}=${id}` as Path);
}
}, [article]);
return <articleContext.Provider value={[article, setArticle]}>
<Reader/>
{children}
</articleContext.Provider>;

View File

@@ -6,7 +6,7 @@ import {ReactComponent as K8s} from "../../img/Kubernetes Logo Vector.svg";
# My high school experience
<img className="thumbnail" src={allBlack} alt="turtleneck thumbnail"/>
<img className="thumbnail" src={allBlack} width={400} alt="turtleneck thumbnail"/>
My first coding experience was in middle school playing Code Combat.
You could say Python is the first language I learned, not particularly uncommon.

View File

@@ -13,8 +13,10 @@ export const ROUTES: Routes = [
function Route(props: PropsWithChildren & {route: Route}) {
const {route: [path], children} = props;
const router = useRouter();
// remove search params
const [cleanPath] = /\/?\w*[^?]/.exec(router.current) ?? [path];
return <>{router.current === path && children}</>
return <>{cleanPath === path && children}</>
}