eno/lib/iso.tsx

100 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-04-05 20:05:21 -04:00
import React from "react";
2023-04-05 23:34:20 -04:00
type Meta = {title:string, description:string, keywords:string, image:string, canonical:string }
type MetaKeys = keyof Meta;
export const Meta:Meta = {
title:"",
description:"",
keywords:"",
image:"",
canonical:""
2023-04-05 20:05:21 -04:00
};
2023-04-05 23:34:20 -04:00
type MetasInputs = { [Property in MetaKeys]?: string };
export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasInputs):null=>
{
const additive = props.concatListed ? (key:MetaKeys, value:string)=> Meta[key] += value : (key:MetaKeys, value:string)=> Meta[key] = value;
const subtractive = props.dropUnlisted ? (key:MetaKeys)=> Meta[key] = "" : (key:MetaKeys)=> {};
Object.keys(Meta).forEach((key)=>{
const metaKey = key as MetaKeys;
const propValue = props[metaKey]||"";
propValue ? additive(metaKey, propValue) : subtractive(metaKey);
})
if(window.innerWidth)
{
document.title = Meta.title;
}
return null;
2023-04-13 22:23:04 -04:00
}
type RoutePath = Array<string>;
2023-04-14 06:58:12 -04:00
type RouteParams = Record<string, string|number|boolean>;
type RouteState = {URL:URL, Path:RoutePath, Params:RouteParams, Anchor:string};
type RouteContext = [Route:RouteState, Update:(inPath?:RoutePath, inParams?:RouteParams, inAnchor?:string)=>void];
2023-04-13 22:23:04 -04:00
type RouteProps = {children:typeof React.Children, url?:URL };
export const Router = {
2023-04-14 06:58:12 -04:00
Parse(url:URL):RouteState
2023-04-13 22:23:04 -04:00
{
2023-04-14 06:58:12 -04:00
const Path = url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/");
const Params:RouteParams = {};
new URLSearchParams(url.search).forEach((k, v)=> Params[k] = v);
const Anchor = url.hash.substring(1);
return {URL:url, Path, Params, Anchor} as RouteState;
2023-04-13 22:23:04 -04:00
},
2023-04-14 06:58:12 -04:00
Context:React.createContext([{URL:new URL("https://original.route/"), Path:[], Params:{}, Anchor:""}, ()=>{}] as RouteContext),
2023-04-13 22:23:04 -04:00
Provider(props:RouteProps)
{
2023-04-14 06:58:12 -04:00
const [routeGet, routeSet] = React.useState(Router.Parse(props.url || new URL(document.location.href)));
2023-04-14 21:41:01 -04:00
const [dirtyGet, dirtySet] = React.useState(true);
2023-04-13 22:23:04 -04:00
2023-04-14 06:58:12 -04:00
const routeUpdate:RouteContext[1] =(inPath, inParams, inAnchor)=>
2023-04-13 22:23:04 -04:00
{
2023-04-14 06:58:12 -04:00
const clone = new URL(routeGet.URL);
inPath && (clone.pathname = inPath.join("/"));
inParams && (clone.search = new URLSearchParams(inParams as Record<string, string>).toString());
2023-04-13 22:23:04 -04:00
routeSet({
URL:clone,
2023-04-14 06:58:12 -04:00
Path: inPath || routeGet.Path,
Params: inParams || routeGet.Params,
Anchor: inAnchor || routeGet.Anchor
2023-04-13 22:23:04 -04:00
});
};
2023-04-14 06:58:12 -04:00
// when the state changes, update the page url
2023-04-14 21:41:01 -04:00
React.useEffect(()=> dirtyGet ? dirtySet(false) : history.pushState({...routeGet, URL:undefined}, "", routeGet.URL), [routeGet.URL.href]);
2023-04-14 06:58:12 -04:00
React.useEffect(()=>{
2023-04-14 21:41:01 -04:00
history.replaceState({...routeGet, URL:undefined}, "", routeGet.URL);
window.addEventListener("popstate", ({state})=>
{
dirtySet(true);
routeUpdate(state.Path, state.Params, state.Anchor);
});
document.addEventListener("click", e=>
{
const t = e.target as HTMLAnchorElement;
if(t.href)
{
const u = new URL(t.href);
if(u.origin == document.location.origin)
{
e.preventDefault();
const parts = Router.Parse(u);
routeUpdate(parts.Path, parts.Params, parts.Anchor);
}
}
})
2023-04-13 22:23:04 -04:00
}, []);
return <Router.Context.Provider value={[routeGet, routeUpdate]}>
{props.children}
</Router.Context.Provider>;
},
Consumer()
{
return React.useContext(Router.Context);
}
};