Skip to content

Commit 48d4a0d

Browse files
authored
Rearrange API methods in the sidebar. (#2098)
* Rearrange API methods in the sidebar. Signed-off-by: bgravenorst <[email protected]> * Address feedback. Signed-off-by: bgravenorst <[email protected]> --------- Signed-off-by: bgravenorst <[email protected]>
1 parent 9d6da06 commit 48d4a0d

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed

src/plugins/plugin-json-rpc.ts

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,49 @@ export interface ResponseItem {
77
error: Error | null | boolean;
88
}
99

10+
interface MethodItem {
11+
name: string;
12+
}
13+
14+
// Method sorting configuration
15+
const METHOD_SORT_PRIORITIES = {
16+
wallet_: 1, // Highest priority - appears first
17+
eth_: 2, // Second priority
18+
personal_: 3, // Third priority
19+
web3_: 4, // Fourth priority
20+
// Default priority for any other prefixes
21+
default: 999,
22+
};
23+
24+
const METHOD_SORT_CONFIG = {
25+
priorities: METHOD_SORT_PRIORITIES,
26+
};
27+
28+
// Helper function to extract method prefix
29+
const getMethodPrefix = (methodName: string): string => {
30+
const prefix = methodName.split("_")[0] + "_";
31+
return prefix;
32+
};
33+
34+
// Sort methods by priority group, then alphabetically within each group
35+
const sortMethods = (items: MethodItem[], sortConfig = METHOD_SORT_CONFIG) => {
36+
return [...items].sort((a, b) => {
37+
const aPrefix = getMethodPrefix(a.name);
38+
const bPrefix = getMethodPrefix(b.name);
39+
40+
const aPriority = sortConfig.priorities[aPrefix] || sortConfig.priorities.default;
41+
const bPriority = sortConfig.priorities[bPrefix] || sortConfig.priorities.default;
42+
43+
// First sort by priority group
44+
if (aPriority !== bPriority) {
45+
return aPriority - bPriority;
46+
}
47+
48+
// Then sort alphabetically within the same group
49+
return a.name.localeCompare(b.name);
50+
});
51+
};
52+
1053
async function fetchData(url: string, name: string): Promise<ResponseItem> {
1154
try {
1255
const response = await fetch(url, { method: "GET" });
@@ -18,7 +61,7 @@ async function fetchData(url: string, name: string): Promise<ResponseItem> {
1861
}
1962

2063
async function fetchMultipleData(
21-
requests: { url: string; name: string }[],
64+
requests: { url: string; name: string }[]
2265
): Promise<ResponseItem[]> {
2366
const promises = requests.map(({ url, name }) => fetchData(url, name));
2467
const responses = await Promise.all(promises);
@@ -45,28 +88,38 @@ const requests = [
4588
},
4689
];
4790

48-
export const prepareLinkItems = (items, refPath) => {
49-
return items.map((method) => ({
91+
export const prepareLinkItems = (
92+
items: MethodItem[],
93+
refPath: string,
94+
sortConfig = METHOD_SORT_CONFIG
95+
): { type: string; label: string; href: string }[] => {
96+
const sortedItems = sortMethods(items, sortConfig);
97+
98+
return sortedItems.map(method => ({
5099
type: "link",
51100
label: method.name,
52101
href: `/${refPath}/${method.name.toLowerCase()}`,
53-
}))
54-
}
102+
}));
103+
};
55104

56-
export const fetchAndGenerateDynamicSidebarItems = async (url: string, refPath: string, network: string) => {
105+
export const fetchAndGenerateDynamicSidebarItems = async (
106+
url: string,
107+
refPath: string,
108+
network: string
109+
) => {
57110
const dynamicRefItems = await fetchData(url, network);
58111
if (dynamicRefItems.data && !dynamicRefItems.error) {
59112
return prepareLinkItems(dynamicRefItems.data.methods, refPath);
60113
}
61-
return []
62-
}
114+
return [];
115+
};
63116

64117
export default function useNetworksMethodPlugin() {
65118
return {
66119
name: "plugin-json-rpc",
67120
async loadContent() {
68121
return await fetchMultipleData(requests)
69-
.then((responseArray) => {
122+
.then(responseArray => {
70123
return responseArray;
71124
})
72125
.catch(() => {
@@ -79,40 +132,38 @@ export default function useNetworksMethodPlugin() {
79132
const dynamicRoutes = content.find(item => item.name === NETWORK_NAMES.metamask);
80133
if (dynamicRoutes) {
81134
const methodsData = await createData(
82-
'methodsData.json',
135+
"methodsData.json",
83136
JSON.stringify(dynamicRoutes.data.methods)
84137
);
85-
dynamicRoutes.data.methods.forEach(async (method) => {
86-
return addRoute({
138+
for (const method of dynamicRoutes.data.methods) {
139+
await addRoute({
87140
path: `/${MM_REF_PATH}/${method.name.toLowerCase()}`,
88141
component: require.resolve("../components/CustomReferencePage/index.tsx"),
89142
modules: {
90143
methodsData: methodsData,
91144
},
92145
exact: true,
93-
customData: { ...method, networkName: NETWORK_NAMES.metamask }
146+
customData: { ...method, networkName: NETWORK_NAMES.metamask },
94147
});
95-
});
148+
}
96149
}
97150
},
98151
configureWebpack() {
99152
return {
100-
plugins: [
101-
new NodePolyfillPlugin()
102-
],
153+
plugins: [new NodePolyfillPlugin()],
103154
resolve: {
104155
alias: {
105-
"@site-global": path.resolve(__dirname, '../methodsData.json'),
156+
"@site-global": path.resolve(__dirname, "../methodsData.json"),
106157
fs: false,
107158
module: false,
108-
"child_process": false,
109-
"worker_threads": false,
159+
child_process: false,
160+
worker_threads: false,
110161
"uglify-js": false,
111162
"@swc/core": false,
112-
"esbuild": false,
163+
esbuild: false,
113164
},
114165
},
115-
}
166+
};
116167
},
117168
};
118169
}

0 commit comments

Comments
 (0)