frontend: add ParamContext

This commit is contained in:
0x1eef 2024-01-15 07:58:19 -03:00
parent 40f86c69a5
commit dde21f2f22
4 changed files with 17 additions and 14 deletions

View file

@ -0,0 +1,2 @@
import { createContext } from "react";
export const ParamContext = createContext<Record<string, string>>({});

View file

@ -1,14 +1,21 @@
import React, { PropsWithChildren } from "react"; import React, { PropsWithChildren } from "react";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"; import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import { ParamContext } from "/Context";
export function App({children}: PropsWithChildren<{}>) { export function App({ children }: PropsWithChildren<{}>) {
const client = new ApolloClient({ const client = new ApolloClient({
uri: "/graphql", uri: "/graphql",
cache: new InMemoryCache(), cache: new InMemoryCache(),
}); });
const params = Object.fromEntries(
location.hash
.substring(1, location.hash.length)
.split(",")
.map(e => e.split("=")),
);
return ( return (
<ApolloProvider client={client}> <ParamContext.Provider value={params}>
{children} <ApolloProvider client={client}>{children}</ApolloProvider>
</ApolloProvider> </ParamContext.Provider>
) );
} }

View file

@ -9,8 +9,6 @@ import { rendermd } from "/lib/markdown-utils";
import { NavBar } from "/components/NavBar"; import { NavBar } from "/components/NavBar";
import { Tabs, Tab } from "/components/Tabs"; import { Tabs, Tab } from "/components/Tabs";
import classnames from "classnames";
const DEFAULT_TASK_CONTENT = [ const DEFAULT_TASK_CONTENT = [
"## Subtasks", "## Subtasks",
"", "",

View file

@ -1,16 +1,12 @@
import React from "react"; import React, { useContext } from "react";
import { ParamContext } from "/Context";
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import { App } from "/components/App"; import { App } from "/components/App";
import { Task } from "/components/Task"; import { Task } from "/components/Task";
(function () { (function () {
const params = Object.fromEntries(
location.hash
.substring(1, location.hash.length)
.split(",")
.map(e => e.split("=")),
);
const root = document.querySelector(".react-root")!; const root = document.querySelector(".react-root")!;
const params = useContext(ParamContext);
ReactDOM.createRoot(root).render( ReactDOM.createRoot(root).render(
<App> <App>
<Task taskId={Number(params.id)} /> <Task taskId={Number(params.id)} />