Replace ParamContext & CookieContext with AppContext

This commit is contained in:
0x1eef 2024-03-13 06:57:53 -03:00
parent e1fb2875ae
commit b28a723d19
6 changed files with 14 additions and 18 deletions

View file

@ -1,3 +1,4 @@
-stage/
-node_modules/
-.gems/
-/twenty-client/tmp/

View file

@ -1,3 +1,3 @@
import { createContext } from "react";
export const ParamContext = createContext<Record<string, string>>({});
export const CookieContext = createContext<Record<string, string>>({});
type TContext = Record<"params" | "cookies", Record<string, string>>;
export const AppContext = createContext<TContext>({});

View file

@ -1,6 +1,6 @@
import { PropsWithChildren } from "react";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import { ParamContext, CookieContext } from "~/Context";
import { AppContext } from "~/Context";
export function App({ children }: PropsWithChildren<{}>) {
const client = new ApolloClient({
@ -17,10 +17,8 @@ export function App({ children }: PropsWithChildren<{}>) {
document.cookie.split(";").map(e => e.split("=")),
);
return (
<CookieContext.Provider value={cookies}>
<ParamContext.Provider value={params}>
<AppContext.Provider value={{ params, cookies }}>
<ApolloProvider client={client}>{children}</ApolloProvider>
</ParamContext.Provider>
</CookieContext.Provider>
</AppContext.Provider>
);
}

View file

@ -1,5 +1,5 @@
import { useContext } from "react";
import { ParamContext, CookieContext } from "~/Context";
import { AppContext } from "~/Context";
import { Maybe } from "~/types/schema";
import { ProjectSelect } from "~/components/ProjectSelect";
const BASE_CLASSNAMES = ["block", "w-3/4", "no-underline", "p-3", "mt-2"];
@ -31,8 +31,7 @@ const find = (path: string, bar: Bar): Maybe<Item> => {
};
export function NavBar() {
const params = useContext(ParamContext);
const cookies = useContext(CookieContext);
const { params, cookies } = useContext(AppContext);
const bar: Bar = {
Tasks: [
{ text: "All tasks", href: "/tasks/" },
@ -70,7 +69,7 @@ export function NavBar() {
</>
);
})}
<h3>Scope</h3>
<h3>Settings</h3>
<ProjectSelect
selected={params.projectId || cookies.projectId}
onChange={project => {

View file

@ -1,5 +1,5 @@
import { useEffect, useState, useContext } from "react";
import { ParamContext, CookieContext } from "~/Context";
import { AppContext } from "~/Context";
import { useForm } from "react-hook-form";
import { useCreateTask } from "~/hooks/mutations/useCreateTask";
import { useUpdateTask } from "~/hooks/mutations/useUpdateTask";
@ -23,8 +23,7 @@ const DEFAULT_TASK_CONTENT = [
].join("\n");
export function Task() {
const params = useContext(ParamContext);
const cookies = useContext(CookieContext);
const { params, cookies } = useContext(AppContext);
const projectId: Maybe<number> = Number(
params.projectId || cookies.projectId,
);

View file

@ -1,13 +1,12 @@
import { useEffect, useContext } from "react";
import { ParamContext, CookieContext } from "~/Context";
import { AppContext } from "~/Context";
import { NavBar } from "~/components/NavBar";
import { Group } from "~/components/Group";
import { TaskStatus, Maybe } from "~/types/schema";
import { useTasks } from "~/hooks/queries/useTasks";
export function Tasks() {
const params = useContext(ParamContext);
const cookies = useContext(CookieContext);
const { params, cookies } = useContext(AppContext);
const projectId: Maybe<number> =
params.projectId || cookies.projectId
? Number(params.projectId || cookies.projectId)