diff --git a/src/pages/index.tsx b/src/pages/index.tsx index fc84976..7e50af6 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -7,6 +7,7 @@ import { Bars3BottomLeftIcon, XMarkIcon } from '@heroicons/react/24/outline'; import { ChevronDownIcon, ChevronRightIcon } from '@heroicons/react/24/solid'; import axios from 'axios'; import { Fragment, useEffect, useState } from 'react'; +import { useRef } from 'react'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import { FiClipboard } from 'react-icons/fi'; import SyntaxHighlighter from 'react-syntax-highlighter'; @@ -110,7 +111,8 @@ export default function Example() { try { const response = await axios.get(`/specs/openrpc-${version}.json`); setSpec(response.data); - window.history.replaceState({}, '', `?version=${version}`); + const hash = window.location.hash; + window.history.replaceState({}, '', `?version=${version}${hash}`); } catch (error) { // eslint-disable-next-line no-console console.error('Error fetching JSON data:', error); @@ -136,6 +138,23 @@ export default function Example() { } }, []); + useEffect(() => { + if (spec) { + setTimeout(() => { + const hash = window.location.hash.substring(1); + const element = document.getElementById(hash); + + if (element) { + element.scrollIntoView({ + behavior: 'smooth', + block: 'start', + inline: 'nearest', + }); + } + }, 1000); + } + }, [spec]); + const activateSidebar = (param: any) => { setOpen(true); setCurrentParam(param); @@ -507,12 +526,17 @@ export default function Example() { {filteredMethods.map((method) => ( - + id={`${pkg}.${method.name}`} + > + + ))} ); @@ -613,43 +637,84 @@ const RPCMethod = ({ pkg, method, activateSidebar, + selectedVersion, }: { pkg: string; method: Method; activateSidebar: (param: Param) => void; + selectedVersion: string; }) => { const [showRequest, setShowRequest] = useState(false); const [showResponse, setShowResponse] = useState(false); + const methodRef = useRef(null); // create a ref + + // const handleCopyClick = () => { + // methodRef.current?.scrollIntoView({ + // behavior: 'auto', + // block: 'start', + // inline: 'nearest', + // }); + // const newUrl = `${window.location.origin}/?version=${selectedVersion}#${pkg}.${method.name}`; + // window.history.pushState({}, '', newUrl); + // const offset = window.innerWidth >= 768 ? -5 : -70; + // window.scrollBy(0, offset); + // }; + + const handleCopyClick = () => { + const hash = window.location.hash.substring(1); + const element = document.getElementById(hash); + element?.scrollIntoView(); + }; return ( -
-

- {method.name}( - {method.params.map((param, i, { length }) => ( - - {param.name}{' '} +

+
+

+ {method.name}( + {method.params.map((param, i, { length }) => ( + + {param.name}{' '} + activateSidebar(param)} + > + {param.description} + {length - 1 != i && ', '} + + + ))} + n ) + {method.result.description != 'Null' && ( activateSidebar(param)} + className='ml-2 text-sm text-blue-500 hover:cursor-pointer hover:font-bold' + onClick={() => activateSidebar(method.result)} > - {param.description} - {length - 1 != i && ', '} + {method.result.description} + )} + + perms: {method.auth} - ))} - ) - {method.result.description != 'Null' && ( +

+ activateSidebar(method.result)} + className='cursor-pointer text-gray-500 hover:text-blue-500' + onClick={() => { + window.location.hash = `${pkg}.${method.name}`; + }} > - {method.result.description} + # - )} - - perms: {method.auth} - -

+
+

{method.description}