Skip to content

Commit d639c99

Browse files
authored
Merge pull request #1022 from supertokens/fix/github-issues
2 parents a5ea601 + 4811723 commit d639c99

File tree

8 files changed

+47
-23
lines changed

8 files changed

+47
-23
lines changed

src/components/Tabs/FrontendTabs.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Tabs, { Props as TabsProps } from "@theme/Tabs";
22
import TabItem from "@theme/TabItem";
33
import { useContext, useMemo } from "react";
44
import { DocItemContext } from "@site/src/context";
5+
import { useUIType } from "@site/src/hooks";
56

67
// TODO: Rename values to items
78
const PrebuiltUITabValues = [
@@ -25,7 +26,7 @@ type FrontendTabsProps = Omit<TabsProps, "values" | "groupId"> & {
2526

2627
function FrontendTabsRoot(props: FrontendTabsProps) {
2728
const { children, exclude, uiType: propsUiType, ...rest } = props;
28-
const { uiType: contextUiType } = useContext(DocItemContext);
29+
const { uiType: contextUiType } = useUIType();
2930
const uiType = propsUiType || contextUiType;
3031
const groupId = useMemo(() => {
3132
return `${FrontendTabsGroupIdPrefix}-${uiType}`;

src/components/UITypeSwitch.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { Card, Flex, Heading, RadioCards, Text } from "@radix-ui/themes";
55
import BrowserOnly from "@docusaurus/BrowserOnly";
66
import { AnimatePresence, motion } from "motion/react";
77
import { TOC_UI_TYPE_SWITCH_ID } from "../lib";
8-
import { useIsVisible } from "../hooks";
8+
import { useIsVisible, useUIType } from "../hooks";
99

1010
import "./RadioCard.scss";
1111

1212
function UITypeSwitch({}) {
13-
const { uiType, onChangeUIType } = useContext(DocItemContext);
13+
const { uiType, onChangeUIType } = useUIType();
1414
const { visibilityRef, isVisible } = useIsVisible();
1515

1616
return (
@@ -42,7 +42,7 @@ function UITypeSwitch({}) {
4242
}
4343

4444
function TOCSwitch({ isParentVisible }: { isParentVisible: boolean }) {
45-
const { uiType, onChangeUIType } = useContext(DocItemContext);
45+
const { uiType, onChangeUIType } = useUIType();
4646
const root = document.getElementById(TOC_UI_TYPE_SWITCH_ID);
4747
const elementRef = useRef<HTMLDivElement>(null);
4848
if (!root) return null;
@@ -90,15 +90,15 @@ function HeadingFilter({ children, name }: React.PropsWithChildren<{ name: strin
9090
}
9191

9292
function PrebuiltUIContent({ children }: React.PropsWithChildren<{}>) {
93-
const state = useContext(DocItemContext);
93+
const { uiType } = useUIType();
9494

95-
return <HeadingFilter name="prebuilt">{state.uiType === "prebuilt" ? children : null}</HeadingFilter>;
95+
return <HeadingFilter name="prebuilt">{uiType === "prebuilt" ? children : null}</HeadingFilter>;
9696
}
9797

9898
function CustomUIContent({ children }: React.PropsWithChildren<{}>) {
99-
const state = useContext(DocItemContext);
99+
const { uiType } = useUIType();
100100

101-
return <HeadingFilter name="custom">{state.uiType === "custom" ? children : null}</HeadingFilter>;
101+
return <HeadingFilter name="custom">{uiType === "custom" ? children : null}</HeadingFilter>;
102102
}
103103

104104
export const UIType = {

src/context/DocItemContext.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type DocItemContextType = DocsItemStateType & {
66
derived: Record<string, unknown>;
77
webJsVersions: Record<string, string>;
88
prebuiltUIVersion: string;
9-
onChangeUIType: (type: DocsItemStateType["uiType"]) => void;
109
onChangeTenantType: (type: DocsItemStateType["tenantType"]) => void;
1110
onChangeAppType: (type: DocsItemStateType["tenantType"]) => void;
1211
onChangeRecipeProperty: (
@@ -39,16 +38,6 @@ export function DocItemContextProvider({ children }: { children: React.ReactNode
3938
onLoadPrebuiltUIVersion();
4039
}, []);
4140

42-
const onChangeUIType = useCallback(
43-
(type: DocsItemStateType["uiType"]) => {
44-
setState({
45-
...state,
46-
uiType: type,
47-
});
48-
},
49-
[state],
50-
);
51-
5241
const onChangeTenantType = useCallback(
5342
(type: DocsItemStateType["tenantType"]) => {
5443
setState({
@@ -135,7 +124,6 @@ export function DocItemContextProvider({ children }: { children: React.ReactNode
135124
},
136125
derived: derivedState,
137126
onChangeAppType,
138-
onChangeUIType,
139127
onChangeTenantType,
140128
onChangeRecipeProperty,
141129
onChangeAppInfoField,

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from "./useSelectionStore";
44
export * from "./useDocItemStore";
55
export * from "./useDocPageData";
66
export * from "./useLoadOpenApiSchema";
7+
export * from "./useUIType";

src/hooks/useDocItemStore.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type DocsItemStateType = {
99
};
1010
thirdparty: {};
1111
};
12-
uiType: "prebuilt" | "custom";
1312
appInfo: {
1413
appName: string;
1514
apiDomain: string;
@@ -35,7 +34,6 @@ const DefaultState: DocsItemStateType = {
3534
},
3635
thirdparty: {},
3736
},
38-
uiType: "prebuilt",
3937
tenantType: "single",
4038
appType: "single",
4139
appInfo: {

src/hooks/useFilteredTocItems.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useContext, useState, useLayoutEffect } from "react";
22
import { DocItemContext } from "@site/src/context";
33
import type { Props } from "@theme/TOC";
4+
import { useUIType } from "./useUIType";
45

56
/**
67
* The TOC component does not support dynamic headings.
@@ -11,7 +12,8 @@ import type { Props } from "@theme/TOC";
1112
* to allow dynamic headings based on tab selection.
1213
*/
1314
export default function useFilteredTocItems(toc: Props["toc"]) {
14-
const { uiType, tenantType } = useContext(DocItemContext);
15+
const { tenantType } = useContext(DocItemContext);
16+
const { uiType } = useUIType();
1517

1618
const [filteredTocItems, setFilteredTocItems] = useState<Props["toc"]>([]);
1719

src/hooks/useUIType.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useCallback, useMemo } from "react";
2+
import { useDocPageData } from "./useDocPageData";
3+
import { useLocation, useHistory } from "@docusaurus/router";
4+
import { docPageStore } from "../lib";
5+
6+
export function useUIType() {
7+
const { search } = useLocation();
8+
const history = useHistory();
9+
const storeUIType = useDocPageData("uiType");
10+
const urlUIType = useMemo(() => {
11+
const searchParams = new URLSearchParams(search);
12+
return searchParams.get("uiType") as "prebuilt" | "custom" | null;
13+
}, [search]);
14+
15+
const uiType: "prebuilt" | "custom" = urlUIType || storeUIType || "prebuilt";
16+
17+
const onChangeUIType = useCallback(
18+
(type: "prebuilt" | "custom") => {
19+
const searchParams = new URLSearchParams(search);
20+
searchParams.set("uiType", type);
21+
history.push({
22+
pathname: history.location.pathname,
23+
search: searchParams.toString(),
24+
hash: history.location.hash,
25+
});
26+
docPageStore.updateValue("uiType", type);
27+
},
28+
[urlUIType, history],
29+
);
30+
31+
return { uiType, onChangeUIType };
32+
}

src/lib/docPageStore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface DocPageState {
77
selfHostedDeploymentType: "with-docker" | "without-docker";
88
tenantType: "single-tenant" | "multi-tenant";
99
apiRequestExampleLanguage: "shell" | "nodejs" | "python" | "go";
10+
uiType: "prebuilt" | "custom";
1011
apiDomain: string;
1112
apiBasePath: string;
1213
websiteDomain: string;
@@ -45,6 +46,7 @@ class DocPageStore {
4546
tenantType: "single-tenant",
4647
nextjsRouterType: "app-router",
4748
accountType: "managed",
49+
uiType: "prebuilt",
4850
selfHostedDeploymentType: "with-docker",
4951
apiRequestExampleLanguage: "shell",
5052
apiDomain: DOC_PAGE_STORE_DEFAULT_VALUES.apiDomain,

0 commit comments

Comments
 (0)