Skip to content

Commit

Permalink
feat: add url hash and scroll to method
Browse files Browse the repository at this point in the history
  • Loading branch information
jcstein committed Oct 7, 2023
1 parent 13eeb91 commit a97323b
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,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);
Expand All @@ -137,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);
Expand Down Expand Up @@ -508,13 +526,17 @@ export default function Example() {
</h3>
</a>
{filteredMethods.map((method) => (
<RPCMethod
<div
key={`${pkg}.${method.name}`}
pkg={pkg}
method={method}
activateSidebar={activateSidebar}
selectedVersion={selectedVersion} // pass selectedVersion here
/>
id={`${pkg}.${method.name}`}
>
<RPCMethod
pkg={pkg}
method={method}
activateSidebar={activateSidebar}
selectedVersion={selectedVersion}
/>
</div>
))}
</div>
);
Expand Down Expand Up @@ -626,18 +648,31 @@ const RPCMethod = ({
const [showResponse, setShowResponse] = useState(false);
const methodRef = useRef<HTMLDivElement>(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 = () => {
methodRef.current?.scrollIntoView({
behavior: 'auto',
block: 'start',
inline: 'nearest',
});
const offset = window.innerWidth >= 768 ? -5 : -70;
window.scrollBy(0, offset);
const hash = window.location.hash.substring(1);
const element = document.getElementById(hash);
element?.scrollIntoView();
};

return (
<div key={method.name} className='py-2' ref={methodRef}>
<div
key={`${pkg}.${method.name}`}
id={`${pkg}.${method.name}`}
className='py-2'
ref={methodRef}
>
<div className='flex items-center justify-between'>
<p className='text-md'>
{method.name}(
Expand All @@ -653,7 +688,7 @@ const RPCMethod = ({
</span>
</span>
))}
)
n )
{method.result.description != 'Null' && (
<span
className='ml-2 text-sm text-blue-500 hover:cursor-pointer hover:font-bold'
Expand All @@ -670,7 +705,12 @@ const RPCMethod = ({
text={`${window.location.origin}/?version=${selectedVersion}#${pkg}.${method.name}`}
onCopy={handleCopyClick}
>
<span className='cursor-pointer text-gray-500 hover:text-blue-500'>
<span
className='cursor-pointer text-gray-500 hover:text-blue-500'
onClick={() => {
window.location.hash = `${pkg}.${method.name}`;
}}
>
#
</span>
</CopyToClipboard>
Expand Down

0 comments on commit a97323b

Please sign in to comment.