Skip to content

Commit 23e3637

Browse files
fix(docs): add global context for os toggle state (#1116)
1 parent 9b0b0c1 commit 23e3637

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

components/code.tsx

+26-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CopyIcon, DropDownArrow } from "src/icons/shared-icons";
44
import { Windows, Mac, Ubuntu, OSProps } from "src/icons/os-icons";
55
import { useEffect } from "react";
66
import { motion } from "framer-motion";
7+
import { useOSContext } from "theme/fetch-ai-docs/contexts/os-provider";
78

89
interface CodeGroupProps {
910
children: ReactNode;
@@ -14,6 +15,8 @@ interface CodeGroupProps {
1415
codeBlocks: ReactNode;
1516
dynamic?: boolean;
1617
digest?: string;
18+
selectedOs: string;
19+
handleOSChange: (newOs: string) => void;
1720
}
1821

1922
interface CodeBlockProps {
@@ -35,6 +38,8 @@ type CodeBoxProps = {
3538
isLocalHostedFile?: boolean;
3639
osBlocks: ReactNode;
3740
codeBlocks: ReactNode;
41+
selectedOs: string;
42+
handleOSChange: (newOs: string) => void;
3843
};
3944

4045
type CodeBlock = {
@@ -77,9 +82,6 @@ export const OsDropDown: React.FC<OsDropDownProps> = ({
7782
const handleOptionSelect = (option: string) => {
7883
onOptionSelect(option);
7984
setIsOpen(false);
80-
if (typeof window !== "undefined") {
81-
localStorage.setItem("storedOsOption", JSON.stringify(option));
82-
}
8385
};
8486

8587
const selectedOptionData = options.find((opt) => opt.name === selectedOption);
@@ -274,24 +276,14 @@ const agentType = [
274276
{ name: "Agentverse", label: "hosted" },
275277
];
276278

277-
const safeParse = (key: string) => {
278-
if (typeof window === "undefined") return null;
279-
280-
try {
281-
const storedData = localStorage.getItem(key);
282-
return storedData ? JSON.parse(storedData) : null;
283-
} catch (error) {
284-
console.error(`Error parsing value for key "${key}":`, error);
285-
return null;
286-
}
287-
};
288-
289279
export const CustomPre: React.FC<CodeBoxProps> = ({
290280
hasCopyCode,
291281
isLocalHostedFile,
292282
isOSFile,
293283
codeBlocks,
294284
osBlocks,
285+
selectedOs,
286+
handleOSChange,
295287
}) => {
296288
const [isCopied, setIsCopied] = useState(false);
297289
const codeRef = useRef<HTMLDivElement>(null);
@@ -312,11 +304,6 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
312304
: agentType[0].name;
313305

314306
const [selectedType, setSelectedType] = useState(localHostdType);
315-
const [selectedOS, setSelectedOS] = useState<string>("windows");
316-
useEffect(() => {
317-
const osFromStorage = safeParse("storedOsOption");
318-
setSelectedOS(osFromStorage || "windows");
319-
}, []);
320307

321308
const filteredAgentType =
322309
child?.length === 2
@@ -343,11 +330,11 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
343330
const renderOsBlock = () => {
344331
return React.Children.map(osBlocks, (child) => {
345332
if (React.isValidElement<CodeBlockProps>(child)) {
346-
if (selectedOS === "windows" && child?.props?.windows) {
333+
if (selectedOs === "windows" && child?.props?.windows) {
347334
return codeBlocks && child?.props?.children;
348-
} else if (selectedOS === "mac" && child?.props?.mac) {
335+
} else if (selectedOs === "mac" && child?.props?.mac) {
349336
return codeBlocks && child?.props?.children;
350-
} else if (selectedOS === "ubuntu" && child?.props?.ubuntu) {
337+
} else if (selectedOs === "ubuntu" && child?.props?.ubuntu) {
351338
return codeBlocks && child?.props?.children;
352339
}
353340
}
@@ -369,7 +356,7 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
369356
}
370357
}
371358
}
372-
}, [isLocalHostedFile, selectedType, codeBlocks, isOSFile, selectedOS]);
359+
}, [isLocalHostedFile, selectedType, codeBlocks, isOSFile, selectedOs]);
373360

374361
const handleCopy = () => {
375362
const codeElements = codeRef.current?.querySelectorAll("code");
@@ -408,8 +395,8 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
408395
)}
409396
{isOSFile && (
410397
<OsDropDown
411-
selectedOption={selectedOS}
412-
onOptionSelect={setSelectedOS}
398+
selectedOption={selectedOs}
399+
onOptionSelect={handleOSChange}
413400
placeholder="Select Language"
414401
options={osmenu as []}
415402
/>
@@ -540,6 +527,13 @@ export const CodeGroup: React.FC<CodeGroupProps> = ({
540527
},
541528
);
542529

530+
const { selectedOS, setSelectedOS } = useOSContext();
531+
532+
const handleOSChange = (newOS: string) => {
533+
setSelectedOS(newOS);
534+
localStorage.setItem("storedOsOption", newOS);
535+
};
536+
543537
const osBlocks = React.Children.toArray(children).filter(
544538
(child): child is React.ReactElement<CodeBlockProps> => {
545539
if (!React.isValidElement(child)) return false;
@@ -559,6 +553,8 @@ export const CodeGroup: React.FC<CodeGroupProps> = ({
559553
hasCopy,
560554
codeBlocks,
561555
osBlocks,
556+
selectedOs: selectedOS,
557+
handleOSChange,
562558
},
563559
);
564560

@@ -574,11 +570,15 @@ export const DocsCode: React.FC<CodeGroupProps> = ({
574570
isOSFile,
575571
hasCopy,
576572
osBlocks,
573+
selectedOs,
574+
handleOSChange,
577575
}) => {
578576
return (
579577
<div className="nx-mt-3">
580578
<CustomPre
581579
isLocalHostedFile={isLocalHostedFile}
580+
handleOSChange={handleOSChange}
581+
selectedOs={selectedOs}
582582
isOSFile={isOSFile}
583583
hasCopyCode={hasCopy}
584584
codeBlocks={codeBlocks}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { createContext, useContext, useEffect, useState } from "react";
2+
3+
const OSContext = createContext<{
4+
selectedOS: string;
5+
setSelectedOS: (os: string) => void;
6+
}>({
7+
selectedOS: "windows",
8+
setSelectedOS: () => {},
9+
});
10+
11+
export const OSProvider: React.FC<{ children: React.ReactNode }> = ({
12+
children,
13+
}) => {
14+
const [selectedOS, setSelectedOS] = useState<string>("windows");
15+
16+
useEffect(() => {
17+
const osString = localStorage.getItem("storedOsOption") || "windows";
18+
setSelectedOS(osString);
19+
}, []);
20+
21+
return (
22+
<OSContext.Provider value={{ selectedOS, setSelectedOS }}>
23+
{children}
24+
</OSContext.Provider>
25+
);
26+
};
27+
28+
export const useOSContext = () => useContext(OSContext);

theme/fetch-ai-docs/index.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { setCookie } from "cookies-next";
3131
import Error404 from "components/error-404";
3232
import LastUpdatedTime from "components/last-updated";
3333
import { ThemeDocProvider } from "./contexts/theme-provider";
34+
import { OSProvider } from "./contexts/os-provider";
3435

3536
type MyItem = Item & {
3637
// Add or modify properties as needed
@@ -339,11 +340,13 @@ export default function Layout({
339340
}: NextraThemeLayoutProps): ReactElement {
340341
return (
341342
<ThemeDocProvider>
342-
<ErrorBoundary FallbackComponent={Error404}>
343-
<ConfigProvider value={context}>
344-
<InnerLayout {...context.pageOpts}>{children}</InnerLayout>
345-
</ConfigProvider>
346-
</ErrorBoundary>
343+
<OSProvider>
344+
<ErrorBoundary FallbackComponent={Error404}>
345+
<ConfigProvider value={context}>
346+
<InnerLayout {...context.pageOpts}>{children}</InnerLayout>
347+
</ConfigProvider>
348+
</ErrorBoundary>
349+
</OSProvider>
347350
</ThemeDocProvider>
348351
);
349352
}

theme/fetch-ai-docs/utils/use-git-edit-url.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import gitUrlParse from "git-url-parse";
22
import { useConfig } from "../contexts";
33

44
export function useGitEditUrl(filePath = ""): string {
5-
console.log(filePath, "s");
65
const config = useConfig();
76
const repo = gitUrlParse(config.docsRepositoryBase || "");
87

0 commit comments

Comments
 (0)