Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
move context out of provider file to avoid refresh bug (#95)
Browse files Browse the repository at this point in the history
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.. vitejs/vite#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.
  • Loading branch information
luthfib committed Oct 4, 2022
1 parent aa2d4cd commit 112d3df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .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
24 changes: 24 additions & 0 deletions 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<string>;
};
};

export const CTWStateContext = createContext<CTWState | undefined>(undefined);
31 changes: 2 additions & 29 deletions src/components/core/ctw-provider.tsx
Expand Up @@ -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<string>;
};
};

type AuthTokenSpecified = { authToken: string; authTokenURL?: never };
type AuthTokenURLSpecified = { authToken?: never; authTokenURL: string };

Expand All @@ -46,8 +21,6 @@ type CTWProviderProps = {
headers?: HeadersInit;
} & (AuthTokenSpecified | AuthTokenURLSpecified);

const CTWStateContext = createContext<CTWState | undefined>(undefined);

function CTWProvider({ theme, children, ...ctwState }: CTWProviderProps) {
const [token, setToken] = useState<CTWToken>();

Expand Down

0 comments on commit 112d3df

Please sign in to comment.