Skip to content

Commit

Permalink
fix(docs) : UX issues in the iota api ref page (#4015)
Browse files Browse the repository at this point in the history
* Fix UX for iota api ref page

* Fix sidenav and codeblocks ui

* Add browserify

* Add browserify

* Remove BrowserOnly component

* Add ExecutionEnvironment import statement
  • Loading branch information
vivekjain23 authored Nov 15, 2024
1 parent ec67eef commit edd0332
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 116 deletions.
53 changes: 30 additions & 23 deletions docs/site/src/components/API/api-ref/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import React, { useState, useEffect } from "react";
import Markdown from "markdown-to-jsx";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import js from "react-syntax-highlighter/dist/esm/languages/hljs/json";
import docco from "react-syntax-highlighter/dist/esm/styles/hljs/docco";
import dark from "react-syntax-highlighter/dist/esm/styles/hljs/dracula";

import { a11yLight, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs";
SyntaxHighlighter.registerLanguage("json", js);

const Examples = (props) => {
const [light, setLight] = useState(true);

const [light, setLight] = useState(false);

useEffect(() => {
const theme = localStorage.getItem("theme");
setLight(theme === "light" ? true : false);
},[])


useEffect(() => {
const checkTheme = () => {
const theme = localStorage.getItem("theme");

if (theme !== "light") {
setLight(false);
}
setLight(theme === "light" ? true : false);
};

window.addEventListener("storage", checkTheme);
Expand Down Expand Up @@ -66,31 +68,36 @@ const Examples = (props) => {
<p className="font-bold mt-4 text-iota-gray-80 dark:text-iota-gray-50">
Request
</p>
<pre className="p-2 pb-0 max-h-96 dark:bg-iota-ghost-dark bg-iota-ghost-white rounded-lg mt-4 overflow-x-auto border dark:border-iota-gray-75">
<code className="text-base">
<SyntaxHighlighter language={js} style={light ? docco : dark}>
{stringRequest}
</SyntaxHighlighter>
</code>
</pre>
<SyntaxHighlighter
language="js"
style={light ? a11yLight : vs2015}
customStyle={{
...(light ? { boxShadow: '0 1px 2px 0 rgba(0, 0, 0, 0.1)' } : {}),
padding: '15px'
}}
>
{stringRequest}
</SyntaxHighlighter>

</div>
)}
{examples[0].result.value && (
<div>
<p className="font-bold mt-6 text-iota-gray-80 dark:text-iota-gray-50">
Response
</p>
<pre className="p-2 pb-0 max-h-96 dark:bg-iota-ghost-dark bg-iota-ghost-white rounded-lg mt-4 overflow-x-auto border dark:border-iota-gray-75">
<code className="text-base">
<SyntaxHighlighter language={js} style={light ? docco : dark}>
{JSON.stringify(response, null, 2)}
</SyntaxHighlighter>
</code>
</pre>
<SyntaxHighlighter
language={js}
style={light ? a11yLight : vs2015} customStyle={{
...(light ? { boxShadow: '0 1px 2px 0 rgba(0, 0, 0, 0.1)' } : {}),
padding: '15px'
}}>
{JSON.stringify(response, null, 2)}
</SyntaxHighlighter>
</div>
)}
</div>
);
};

export default Examples;
export default Examples;
4 changes: 2 additions & 2 deletions docs/site/src/components/API/api-ref/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Method = (props) => {
<Heading
as="h2"
id={`${api.replaceAll(/\s/g, "-").toLowerCase()}`}
className="border-0 border-b border-solid border-iota-blue-dark dark:border-iota-blue scroll-mt-32 text-3xl text-iota-blue-dark dark:text-iota-blue font-bold mt-12 after:content-['_#'] after:hidden after:hover:inline after:opacity-20 cursor-pointer"
className="dark:text-white mt-12 after:hidden after:hover:inline after:opacity-20 cursor-pointer"
onClick={handleClick}
key={api.replaceAll(/\s/g, "-").toLowerCase()}
>
Expand Down Expand Up @@ -72,7 +72,7 @@ const Method = (props) => {
>
<Heading
as="h3"
className="text-2xl font-bold after:content-['_#'] after:hidden after:hover:inline after:opacity-20 cursor-pointer"
className="after:hidden after:hover:inline after:opacity-20 cursor-pointer"
key={`link-${method.name.toLowerCase()}`}
id={`${method.name.toLowerCase()}`}
onClick={null}
Expand Down
11 changes: 7 additions & 4 deletions docs/site/src/components/API/api-ref/networkselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// SPDX-License-Identifier: Apache-2.0

import React, { useState, useEffect } from "react";
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
import { Select, MenuItem, FormControl, InputLabel } from "@mui/material";
import { StyledEngineProvider } from "@mui/material/styles";
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";

const NETWORKS = ["Devnet", "Testnet"];

const NetworkSelect = () => {
const [selection, setSelection] = useState(() => {

if (ExecutionEnvironment.canUseDOM) {
const network = localStorage.getItem("RPC");
if (network === null) {
Expand All @@ -23,8 +24,10 @@ const NetworkSelect = () => {
});

useEffect(() => {
localStorage.setItem("RPC", selection);
window.dispatchEvent(new Event("storage"));
if (typeof window !== "undefined") {
localStorage.setItem("RPC", selection);
window.dispatchEvent(new Event("storage"));
}
}, [selection]);

const handleChange = (e) => {
Expand All @@ -33,7 +36,7 @@ const NetworkSelect = () => {

return (
<StyledEngineProvider injectFirst>
<div className="w-11/12">
<div className="w-11/12 pb-3">
<FormControl fullWidth>
<InputLabel
id="network"
Expand Down
8 changes: 4 additions & 4 deletions docs/site/src/components/API/api-ref/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ const Parameters = (props) => {
return (
<div className="border-b mb-8">
{hasParams && (
<div className="grid grid-cols-6 ml-4">
<div className="rounded-tl-lg rounded-bl-lg col-span-2 p-2 bg-iota-blue dark:bg-iota-blue-dark text-iota-gray-95 dark:text-iota-gray-50 font-bold">
<div className="grid grid-cols-6 ml-4 bg-iota-hero-dark rounded-lg">
<div className="col-span-2 p-2 text-white font-bold">
Name&lt;Type&gt;
</div>
<div className="p-2 bg-iota-blue dark:bg-iota-blue-dark text-iota-gray-95 dark:text-iota-gray-50 text-iota-gray-35 font-bold">
<div className="p-2 text-white font-bold">
Required
</div>
<div className="rounded-tr-lg rounded-br-lg col-span-3 p-2 bg-iota-blue dark:bg-iota-blue-dark text-iota-gray-95 dark:text-iota-gray-50 text-iota-gray-35 font-bold">
<div className="col-span-3 p-2 text-white font-bold">
Description
</div>
</div>
Expand Down
76 changes: 43 additions & 33 deletions docs/site/src/components/API/api-ref/refnav.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import React from "react";
import React, { useState } from "react";
import Link from "@docusaurus/Link";
import NetworkSelect from "./networkselect";

const RefNav = (props) => {
const { json, apis } = props;

const { json, apis, openDropdown, setOpenDropdown } = props;
const [page, setPage] = useState(null)
const toggleDropdown = (index) => {
setOpenDropdown(openDropdown === index ? null : index);
};
return (
<div className="mb-24">
<div className="sticky -top-12 -mt-8 pt-8 pb-2 bg-white dark:bg-ifm-background-color-dark">
<div className="sticky -top-12 -mt-8 pt-8 pb-2 bg-white dark:bg-ifm-background-color-dark flex justify-center">
<NetworkSelect />
</div>

{apis.map((api) => {
{apis.map((api, index) => {
return (
<div key={`${api.replaceAll(/\s/g, "-").toLowerCase()}`}>
<Link
href={`#${api.replaceAll(/\s/g, "-").toLowerCase()}`}
data-to-scrollspy-id={`${api
.replaceAll(/\s/g, "-")
.toLowerCase()}`}
className="hover:no-underline pt-4 block text-black dark:text-white hover:text-iota-blue dark:hover:text-iota-blue"
>
{api}
</Link>
{json["methods"]
.filter((method) => method.tags[0].name == api)
.map((method) => {
return (
<Link
className="my-1 pl-4 block text-iota-gray-95 dark:text-iota-grey-35 hover:no-underline dark:hover:text-iota-blue"
key={`link-${method.name.toLowerCase()}`}
href={`#${method.name.toLowerCase()}`}
data-to-scrollspy-id={`${method.name.toLowerCase()}`}
>
{method.name}
</Link>
);
})}
<div onClick={() => toggleDropdown(index)}>
<Link
href={`#${api.replaceAll(/\s/g, "-").toLowerCase()}`}
data-to-scrollspy-id={`${api
.replaceAll(/\s/g, "-")
.toLowerCase()}`}
className="hover:no-underline text-[#525860] dark:text-[#C0C0C0] hover:text-[#525860] dark:hover:text-white"
>
<div className="flex justify-between p-[5.625px] dark:hover:bg-[#212121] hover:bg-[#f2f2f2]" style={{ alignItems: "center" }}>
<div className={`font-medium ${openDropdown === index && 'menu__link--active'}`}>{api}</div>
<div className="ml-4 pr-4 text-center">
<p className={`transition-transform duration-500 scale-y-150 ${openDropdown === index ? 'rotate-90' : ''}`} style={{ marginBottom: "0px", fontSize: "17px", fontWeight: "600" }}>&gt;</p>
</div>
</div>
</Link>
</div>
<div className={`ml-4 transition-all duration-300 overflow-hidden ${openDropdown === index ? "max-h-screen" : "max-h-0"
}`}>
{json["methods"]
.filter((method) => method.tags[0].name == api)
.map((method) => {
return (
<Link
className={`menu__link font-medium block hover:no-underline text-base ${page === method.name && 'menu__link--active'}`}
key={`link-${method.name.toLowerCase()}`}
href={`#${method.name.toLowerCase()}`}
data-theme="dark"
onClick={() => setPage(method.name)}
>
{method.name}
</Link>
);
})}
</div>
</div>
);
})}
</div>
);
};

export default RefNav;
export default RefNav;
70 changes: 45 additions & 25 deletions docs/site/src/components/API/api-ref/typedef.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import React from "react";

import React, { useState, useEffect } from "react";
import _ from "lodash";
import { getRef } from "../index";

import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import json from "react-syntax-highlighter/dist/esm/languages/hljs/json";
import dark from "react-syntax-highlighter/dist/esm/styles/hljs/dracula";
import { a11yLight, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs";

const TypeDef = (props) => {
const [light, setLight] = useState(() => {
if (typeof window !== "undefined") {
const theme = localStorage.getItem("theme");
return theme === "light";
}
return false;
});

useEffect(() => {
const checkTheme = () => {
if (typeof window !== "undefined") {
const theme = localStorage.getItem("theme");
setLight(theme === "light");
}
};

window.addEventListener("storage", checkTheme);
return () => {
window.removeEventListener("storage", checkTheme);
};
}, []);

const { schema, schemas } = props;
const schemaObj = schemas[schema];
let refs = [{ title: schema, ...schemaObj }];
Expand All @@ -23,36 +44,35 @@ const TypeDef = (props) => {
} else if (value && typeof value === "object") {
collectRefs(value);
}
if (key == "$ref")
if (key === "$ref") {
refs.push({ title: getRef(value), ...schemas[getRef(value)] });
}
}
};

collectRefs(schemaObj);

refs.map((ref) => {
collectRefs(schemas[ref.title]);
});

refs.map((ref) => {
collectRefs(schemas[ref.title]);
});
refs.map((ref) => collectRefs(schemas[ref.title]));
refs.map((ref) => collectRefs(schemas[ref.title]));

return (
<div>
{_.uniqWith(refs, (a, b) => {
return a.title === b.title;
}).map((curObj, idx) => {
return (
<div key={idx}>
<p className="text-lg font-bold mb-0 mt-8">{curObj.title}</p>
<hr className="mt-0" />
<SyntaxHighlighter language={json} style={dark}>
{JSON.stringify(_.omit(curObj, "title"), null, 4)}
</SyntaxHighlighter>
</div>
);
})}
{_.uniqWith(refs, (a, b) => a.title === b.title).map((curObj, idx) => (
<div key={idx}>
<p className="text-lg font-bold mb-0 mt-8">{curObj.title}</p>
<hr className="mt-0" />
<SyntaxHighlighter
language="json"
style={light ? a11yLight : vs2015}
customStyle={{
...(light ? { boxShadow: '0 1px 2px 0 rgba(0, 0, 0, 0.1)' } : {}),
padding: '15px'
}}
>
{JSON.stringify(_.omit(curObj, "title"), null, 4)}
</SyntaxHighlighter>
</div>
))}
</div>
);
};
Expand Down
Loading

0 comments on commit edd0332

Please sign in to comment.