Skip to content

Commit 53ba877

Browse files
(v2.1) adds API count and Fuse search
1 parent f304ba7 commit 53ba877

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

ui/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@fontsource/roboto": "^4.5.8",
1818
"@heroicons/react": "^2.0.15",
1919
"astro": "^2.0.6",
20+
"fuse.js": "^6.6.2",
2021
"react-ace": "^10.1.0",
2122
"react-anchor-link-smooth-scroll": "^1.0.12",
2223
"react-markdown": "^8.0.5",

ui/src/components/App.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ApiInfo from './ApiInfo';
55
import ApiAction from './ApiAction';
66
import useLocalStorage from 'react-use-localstorage';
77
import shortid from 'shortid';
8+
import Fuse from 'fuse.js';
89

910
interface IAPIRule {
1011
[key: string]: string[];
@@ -24,6 +25,7 @@ interface IAPIInfo {
2425
export default function App() {
2526

2627
const [lrdDocsJson, setLrdDocsJson] = useState<IAPIInfo[]>([]);
28+
const [lrdDocsJsonCopy, setLrdDocsJsonCopy] = useState<IAPIInfo[]>([]);
2729
const [allParamsRegistry, setAllParamsRegistery] = useLocalStorage('allParamsRegistry', "{}");
2830
const [apiURL, setApiURL] = useState<string>('');
2931
const [host, setHost] = useState<string>('');
@@ -39,7 +41,10 @@ export default function App() {
3941
const [showPatch] = useLocalStorage('showPatch', 'true');
4042
const [showHead] = useLocalStorage('showHead', 'true');
4143

42-
44+
const searchOptions = {
45+
keys: ['uri', 'docBlock'],
46+
threshold: 0.3
47+
};
4348

4449
useEffect(() => {
4550
// get query param named api
@@ -72,13 +77,31 @@ export default function App() {
7277
.then((lrdDocsJson) => {
7378
setError(null)
7479
setLrdDocsJson(lrdDocsJson)
80+
setLrdDocsJsonCopy(lrdDocsJson)
7581
setSendingRequest(false)
7682
}).catch((error) => {
7783
setError(error.message)
7884
setSendingRequest(false)
7985
})
8086
}
8187

88+
const handleSearch = (search: string) => {
89+
search = search.trim()
90+
if (!search) {
91+
setLrdDocsJson(lrdDocsJsonCopy)
92+
return
93+
}
94+
const fuse = new Fuse(lrdDocsJson, searchOptions);
95+
96+
const filteredData = fuse.search(search);
97+
const filteredLrdJson: IAPIInfo[] = []
98+
for (let i = 0; i < filteredData.length; i++) {
99+
filteredLrdJson.push(filteredData[i].item)
100+
}
101+
102+
setLrdDocsJson(filteredLrdJson)
103+
}
104+
82105
const handleChangeSettings = (showGet: string,
83106
showPost: string,
84107
showDelete: string,
@@ -92,7 +115,7 @@ export default function App() {
92115
}
93116
return (
94117
<>
95-
<TopNav handleChangeSettings={handleChangeSettings} />
118+
<TopNav handleChangeSettings={handleChangeSettings} handleSearch={handleSearch} />
96119
{sendingRequest && (
97120
<progress className="progress progress-success w-full"></progress>
98121
)}

ui/src/components/Sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function Sidebar(props: Props) {
2929
<>
3030
<aside>
3131
<h2 className="title pl-5 pt-2 mb-5">
32-
API List
32+
API List <span className='text-slate-500 capitalize'>Total {lrdDocsJson.length}</span>
3333
</h2>
3434
<ul>
3535
{lrdDocsJson.map((lrdDocsItem) => (

ui/src/components/TopNav.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ interface Props {
1313
showHead: string,
1414
sort: string,
1515
groupby: string) => void
16+
handleSearch: (search: string) => void
1617
}
1718
export default function TopNav(props: Props) {
1819

19-
const { handleChangeSettings } = props
20+
const { handleChangeSettings, handleSearch } = props
2021
const [theme, setTheme] = useLocalStorage('theme', '');
2122
const [sort, setSort] = useLocalStorage('sort', 'default');
2223
const [groupby, setGroupby] = useLocalStorage('groupby', 'default');
@@ -97,6 +98,9 @@ export default function TopNav(props: Props) {
9798
</a>
9899
</div>
99100
<div className="flex-none">
101+
<div className="form-control">
102+
<input type="text" placeholder="Search" className="input input-sm input-bordered" onChange={ (e) => handleSearch(e.target.value) } />
103+
</div>
100104
<div className="menu menu-horizontal px-6 ">
101105
<label className="swap swap-rotate">
102106
<input type="checkbox" onChange={toggleDarkMode} />

0 commit comments

Comments
 (0)