From 112d3dfdd7760bc206237a509394b6b9f9825c32 Mon Sep 17 00:00:00 2001 From: Luthfi Bustillos-Francis Date: Tue, 4 Oct 2022 12:56:22 -0400 Subject: [PATCH] move context out of provider file to avoid refresh bug (#95) Moving the CTW Context and provider into separate files to avoid issues when changing certain files it errors out and says context is not defined. This is caused by a bug in vite which there is a current open pr on.. https://github.com/vitejs/vite/pull/10239, which should solve this. Being that this bug is annoying enough in dev i am splitting context and provider into separate files to avoid this bug. --- .changeset/purple-parents-jog.md | 5 +++++ src/components/core/ctw-context.tsx | 24 +++++++++++++++++++++ src/components/core/ctw-provider.tsx | 31 ++-------------------------- 3 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 .changeset/purple-parents-jog.md create mode 100644 src/components/core/ctw-context.tsx diff --git a/.changeset/purple-parents-jog.md b/.changeset/purple-parents-jog.md new file mode 100644 index 000000000..daeaf88f5 --- /dev/null +++ b/.changeset/purple-parents-jog.md @@ -0,0 +1,5 @@ +--- +"@zus-health/ctw-component-library": patch +--- + +Move ctw provider and context into separate files to avoid bug outlined in https://github.com/vitejs/vite/pull/10239 diff --git a/src/components/core/ctw-context.tsx b/src/components/core/ctw-context.tsx new file mode 100644 index 000000000..5fc9c73fe --- /dev/null +++ b/src/components/core/ctw-context.tsx @@ -0,0 +1,24 @@ +import { Theme } from "@/styles/tailwind.theme"; +import { createContext } from "react"; +import type { Env } from "./ctw-provider"; + +export type CTWToken = { + accessToken: string; + issuedTokenType: string; + tokenType: string; + expiresAt: number; +}; + +export type CTWState = { + env: Env; + authToken?: string; + headers?: HeadersInit; + authTokenURL?: string; + theme?: Theme; + token?: CTWToken; + actions: { + handleAuth: () => Promise; + }; +}; + +export const CTWStateContext = createContext(undefined); diff --git a/src/components/core/ctw-provider.tsx b/src/components/core/ctw-provider.tsx index 115e6f28b..2f79ce743 100644 --- a/src/components/core/ctw-provider.tsx +++ b/src/components/core/ctw-provider.tsx @@ -3,39 +3,14 @@ import { DefaultTheme, mapToCSSVar, Theme } from "@/styles/tailwind.theme"; import { queryClient } from "@/utils/request"; import { QueryClientProvider } from "@tanstack/react-query"; import { merge } from "lodash"; -import { - createContext, - ReactNode, - useCallback, - useContext, - useMemo, - useState, -} from "react"; +import { ReactNode, useCallback, useContext, useMemo, useState } from "react"; +import { CTWState, CTWStateContext, CTWToken } from "./ctw-context"; import "./main.scss"; export type Env = "dev" | "sandbox" | "production"; const EXPIRY_PADDING_MS = 60000; -type CTWToken = { - accessToken: string; - issuedTokenType: string; - tokenType: string; - expiresAt: number; -}; - -type CTWState = { - env: Env; - authToken?: string; - headers?: HeadersInit; - authTokenURL?: string; - theme?: Theme; - token?: CTWToken; - actions: { - handleAuth: () => Promise; - }; -}; - type AuthTokenSpecified = { authToken: string; authTokenURL?: never }; type AuthTokenURLSpecified = { authToken?: never; authTokenURL: string }; @@ -46,8 +21,6 @@ type CTWProviderProps = { headers?: HeadersInit; } & (AuthTokenSpecified | AuthTokenURLSpecified); -const CTWStateContext = createContext(undefined); - function CTWProvider({ theme, children, ...ctwState }: CTWProviderProps) { const [token, setToken] = useState();