import React, { Fragment, useMemo } from "react" import CodeBlock, { Props as CodeBlockProps } from '@theme/CodeBlock'; import ErrorBoundary from "@docusaurus/ErrorBoundary" import { useTypedLocalStorage } from "./useLocalStorage"; import ButtonGroup from "./ButtonGroup"; import EditorFetch from '@site/src/components/Schema/SchemaEditorFetch'; import {useWindowSize} from '@docusaurus/theme-common'; export interface FileProps extends Omit { data: string | object filePathName?: string schemaName?: string interactive?: boolean defaultDisplay?: 'plain' | 'interactive' readOnlyAtWidth?: number } const FileExample = (props: FileProps) => { const { data, interactive = true, defaultDisplay = 'interactive', schemaName, filePathName, readOnlyAtWidth = 1, ...rest } = props; const [fileExampleShow, setFileExampleShow] = useTypedLocalStorage('docusaurus.tab.fileExampleShow', defaultDisplay, false); const windowSize = useWindowSize({desktopBreakpoint: readOnlyAtWidth}); let displaySwitch = null; const isInteractive = interactive && windowSize === 'desktop' if (isInteractive) { displaySwitch = ( setFileExampleShow(val as 'interactive' | 'plain')} /> ); } let fileNameContent = null; if(filePathName !== undefined && (isInteractive && fileExampleShow === 'interactive')) { fileNameContent = {filePathName} } let header = null; if(displaySwitch !== null || fileNameContent !== null) { header = (
{displaySwitch}{fileNameContent}
); } const codeBlockContent = useMemo(() =>{ if(isInteractive && fileExampleShow === 'interactive') { return null; } if(typeof data !== 'string') { return JSON.stringify(data, null, 2); } return data; }, [interactive, fileExampleShow, data]); return ( {header} {!isInteractive || fileExampleShow === 'plain' ? {codeBlockContent} : null} {isInteractive && fileExampleShow === 'interactive' ? : null} ); } const WrappedFileExample = (props: FileProps) => { return (

Example component crashed because of error: {error.message}.

)} >
} export default WrappedFileExample;