From e1fb2875ae1833a12c82ba2816673b46efef384f Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Tue, 12 Mar 2024 05:48:57 -0300 Subject: [PATCH] Persist 'projectId' between page reloads --- twenty-client/src/js/Context.ts | 1 + twenty-client/src/js/components/App.tsx | 13 +++-- twenty-client/src/js/components/NavBar.tsx | 9 ++- twenty-client/src/js/components/Select.tsx | 22 +++---- twenty-client/src/js/components/Tabs.tsx | 2 +- twenty-client/src/js/components/Task.tsx | 57 ++++++++----------- twenty-client/src/js/components/Tasks.tsx | 10 ++-- .../src/js/hooks/mutations/useCreateTask.ts | 8 +-- 8 files changed, 59 insertions(+), 63 deletions(-) diff --git a/twenty-client/src/js/Context.ts b/twenty-client/src/js/Context.ts index c8c3354..36eca17 100644 --- a/twenty-client/src/js/Context.ts +++ b/twenty-client/src/js/Context.ts @@ -1,2 +1,3 @@ import { createContext } from "react"; export const ParamContext = createContext>({}); +export const CookieContext = createContext>({}); diff --git a/twenty-client/src/js/components/App.tsx b/twenty-client/src/js/components/App.tsx index 51c83fb..b1b1121 100644 --- a/twenty-client/src/js/components/App.tsx +++ b/twenty-client/src/js/components/App.tsx @@ -1,6 +1,6 @@ import { PropsWithChildren } from "react"; import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client"; -import { ParamContext } from "~/Context"; +import { ParamContext, CookieContext } from "~/Context"; export function App({ children }: PropsWithChildren<{}>) { const client = new ApolloClient({ @@ -13,9 +13,14 @@ export function App({ children }: PropsWithChildren<{}>) { .split(",") .map(e => e.split("=")), ); + const cookies = Object.fromEntries( + document.cookie.split(";").map(e => e.split("=")), + ); return ( - - {children} - + + + {children} + + ); } diff --git a/twenty-client/src/js/components/NavBar.tsx b/twenty-client/src/js/components/NavBar.tsx index f395380..a4d9093 100644 --- a/twenty-client/src/js/components/NavBar.tsx +++ b/twenty-client/src/js/components/NavBar.tsx @@ -1,5 +1,5 @@ import { useContext } from "react"; -import { ParamContext } from "~/Context"; +import { ParamContext, CookieContext } 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"]; @@ -32,6 +32,7 @@ const find = (path: string, bar: Bar): Maybe => { export function NavBar() { const params = useContext(ParamContext); + const cookies = useContext(CookieContext); const bar: Bar = { Tasks: [ { text: "All tasks", href: "/tasks/" }, @@ -69,13 +70,15 @@ export function NavBar() { ); })} -

Filters

+

Scope

{ if (project) { + document.cookie = `projectId=${project.id}; path=/`; location.hash = `projectId=${project.id}`; } else { + document.cookie = `projectId=; path=/; max-age=0`; location.hash = ""; } location.reload(); diff --git a/twenty-client/src/js/components/Select.tsx b/twenty-client/src/js/components/Select.tsx index 6d08b9a..c5a50a9 100644 --- a/twenty-client/src/js/components/Select.tsx +++ b/twenty-client/src/js/components/Select.tsx @@ -17,7 +17,7 @@ const ACTIVE_CLASSNAME = [ "border-solid", "border-secondary", "text-primary", - "bg-secondary" + "bg-secondary", ].join(" "); type Props = { @@ -36,7 +36,7 @@ export const Select = ({ onChange, options, selected, placeholder }: Props) => { const selectOptions = [{ label: placeholder, value: "" }, ...options]; const [isOpen, setIsOpen] = useState(false); const [option, setOption] = useState