Skip to content

Commit

Permalink
fix(docs): add global context for os toggle state (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamgambhir97 authored Dec 23, 2024
1 parent 9b0b0c1 commit 23e3637
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
52 changes: 26 additions & 26 deletions components/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CopyIcon, DropDownArrow } from "src/icons/shared-icons";
import { Windows, Mac, Ubuntu, OSProps } from "src/icons/os-icons";
import { useEffect } from "react";
import { motion } from "framer-motion";
import { useOSContext } from "theme/fetch-ai-docs/contexts/os-provider";

interface CodeGroupProps {
children: ReactNode;
Expand All @@ -14,6 +15,8 @@ interface CodeGroupProps {
codeBlocks: ReactNode;
dynamic?: boolean;
digest?: string;
selectedOs: string;
handleOSChange: (newOs: string) => void;
}

interface CodeBlockProps {
Expand All @@ -35,6 +38,8 @@ type CodeBoxProps = {
isLocalHostedFile?: boolean;
osBlocks: ReactNode;
codeBlocks: ReactNode;
selectedOs: string;
handleOSChange: (newOs: string) => void;
};

type CodeBlock = {
Expand Down Expand Up @@ -77,9 +82,6 @@ export const OsDropDown: React.FC<OsDropDownProps> = ({
const handleOptionSelect = (option: string) => {
onOptionSelect(option);
setIsOpen(false);
if (typeof window !== "undefined") {
localStorage.setItem("storedOsOption", JSON.stringify(option));
}
};

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

const safeParse = (key: string) => {
if (typeof window === "undefined") return null;

try {
const storedData = localStorage.getItem(key);
return storedData ? JSON.parse(storedData) : null;
} catch (error) {
console.error(`Error parsing value for key "${key}":`, error);
return null;
}
};

export const CustomPre: React.FC<CodeBoxProps> = ({
hasCopyCode,
isLocalHostedFile,
isOSFile,
codeBlocks,
osBlocks,
selectedOs,
handleOSChange,
}) => {
const [isCopied, setIsCopied] = useState(false);
const codeRef = useRef<HTMLDivElement>(null);
Expand All @@ -312,11 +304,6 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
: agentType[0].name;

const [selectedType, setSelectedType] = useState(localHostdType);
const [selectedOS, setSelectedOS] = useState<string>("windows");
useEffect(() => {
const osFromStorage = safeParse("storedOsOption");
setSelectedOS(osFromStorage || "windows");
}, []);

const filteredAgentType =
child?.length === 2
Expand All @@ -343,11 +330,11 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
const renderOsBlock = () => {
return React.Children.map(osBlocks, (child) => {
if (React.isValidElement<CodeBlockProps>(child)) {
if (selectedOS === "windows" && child?.props?.windows) {
if (selectedOs === "windows" && child?.props?.windows) {
return codeBlocks && child?.props?.children;
} else if (selectedOS === "mac" && child?.props?.mac) {
} else if (selectedOs === "mac" && child?.props?.mac) {
return codeBlocks && child?.props?.children;
} else if (selectedOS === "ubuntu" && child?.props?.ubuntu) {
} else if (selectedOs === "ubuntu" && child?.props?.ubuntu) {
return codeBlocks && child?.props?.children;
}
}
Expand All @@ -369,7 +356,7 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
}
}
}
}, [isLocalHostedFile, selectedType, codeBlocks, isOSFile, selectedOS]);
}, [isLocalHostedFile, selectedType, codeBlocks, isOSFile, selectedOs]);

const handleCopy = () => {
const codeElements = codeRef.current?.querySelectorAll("code");
Expand Down Expand Up @@ -408,8 +395,8 @@ export const CustomPre: React.FC<CodeBoxProps> = ({
)}
{isOSFile && (
<OsDropDown
selectedOption={selectedOS}
onOptionSelect={setSelectedOS}
selectedOption={selectedOs}
onOptionSelect={handleOSChange}
placeholder="Select Language"
options={osmenu as []}
/>
Expand Down Expand Up @@ -540,6 +527,13 @@ export const CodeGroup: React.FC<CodeGroupProps> = ({
},
);

const { selectedOS, setSelectedOS } = useOSContext();

const handleOSChange = (newOS: string) => {
setSelectedOS(newOS);
localStorage.setItem("storedOsOption", newOS);
};

const osBlocks = React.Children.toArray(children).filter(
(child): child is React.ReactElement<CodeBlockProps> => {
if (!React.isValidElement(child)) return false;
Expand All @@ -559,6 +553,8 @@ export const CodeGroup: React.FC<CodeGroupProps> = ({
hasCopy,
codeBlocks,
osBlocks,
selectedOs: selectedOS,
handleOSChange,
},
);

Expand All @@ -574,11 +570,15 @@ export const DocsCode: React.FC<CodeGroupProps> = ({
isOSFile,
hasCopy,
osBlocks,
selectedOs,
handleOSChange,
}) => {
return (
<div className="nx-mt-3">
<CustomPre
isLocalHostedFile={isLocalHostedFile}
handleOSChange={handleOSChange}
selectedOs={selectedOs}
isOSFile={isOSFile}
hasCopyCode={hasCopy}
codeBlocks={codeBlocks}
Expand Down
28 changes: 28 additions & 0 deletions theme/fetch-ai-docs/contexts/os-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { createContext, useContext, useEffect, useState } from "react";

const OSContext = createContext<{
selectedOS: string;
setSelectedOS: (os: string) => void;
}>({
selectedOS: "windows",
setSelectedOS: () => {},
});

export const OSProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [selectedOS, setSelectedOS] = useState<string>("windows");

useEffect(() => {
const osString = localStorage.getItem("storedOsOption") || "windows";
setSelectedOS(osString);
}, []);

return (
<OSContext.Provider value={{ selectedOS, setSelectedOS }}>
{children}
</OSContext.Provider>
);
};

export const useOSContext = () => useContext(OSContext);
13 changes: 8 additions & 5 deletions theme/fetch-ai-docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { setCookie } from "cookies-next";
import Error404 from "components/error-404";
import LastUpdatedTime from "components/last-updated";
import { ThemeDocProvider } from "./contexts/theme-provider";
import { OSProvider } from "./contexts/os-provider";

type MyItem = Item & {
// Add or modify properties as needed
Expand Down Expand Up @@ -339,11 +340,13 @@ export default function Layout({
}: NextraThemeLayoutProps): ReactElement {
return (
<ThemeDocProvider>
<ErrorBoundary FallbackComponent={Error404}>
<ConfigProvider value={context}>
<InnerLayout {...context.pageOpts}>{children}</InnerLayout>
</ConfigProvider>
</ErrorBoundary>
<OSProvider>
<ErrorBoundary FallbackComponent={Error404}>
<ConfigProvider value={context}>
<InnerLayout {...context.pageOpts}>{children}</InnerLayout>
</ConfigProvider>
</ErrorBoundary>
</OSProvider>
</ThemeDocProvider>
);
}
Expand Down
1 change: 0 additions & 1 deletion theme/fetch-ai-docs/utils/use-git-edit-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import gitUrlParse from "git-url-parse";
import { useConfig } from "../contexts";

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

Expand Down

0 comments on commit 23e3637

Please sign in to comment.