Skip to content

Commit 3e71233

Browse files
committed
Rearrange API methods in the sidebar.
Signed-off-by: bgravenorst <[email protected]>
1 parent 15cf919 commit 3e71233

File tree

1 file changed

+73
-20
lines changed

1 file changed

+73
-20
lines changed

src/plugins/plugin-json-rpc.ts

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

10+
// Method sorting configuration
11+
const METHOD_SORT_PRIORITIES = {
12+
wallet_: 1, // Highest priority - appears first
13+
eth_: 2, // Second priority
14+
personal_: 3, // Third priority
15+
web3_: 4, // Fourth priority
16+
// Default priority for any other prefixes
17+
default: 999,
18+
};
19+
20+
const METHOD_SORT_CONFIG = {
21+
priorities: METHOD_SORT_PRIORITIES,
22+
// Optional: Custom sort function for special cases
23+
customSort: null,
24+
// Optional: Group labels for future use
25+
groupLabels: {
26+
wallet_: "Wallet Methods",
27+
eth_: "Ethereum Methods",
28+
personal_: "Personal Methods",
29+
web3_: "Web3 Methods",
30+
default: "Other Methods",
31+
},
32+
};
33+
34+
// Helper function to extract method prefix
35+
const getMethodPrefix = (methodName: string): string => {
36+
const prefix = methodName.split("_")[0] + "_";
37+
return prefix;
38+
};
39+
40+
// Sort methods by priority group, then alphabetically within each group
41+
const sortMethods = (items: any[], sortConfig = METHOD_SORT_CONFIG) => {
42+
return [...items].sort((a, b) => {
43+
const aPrefix = getMethodPrefix(a.name);
44+
const bPrefix = getMethodPrefix(b.name);
45+
46+
const aPriority = sortConfig.priorities[aPrefix] || sortConfig.priorities.default;
47+
const bPriority = sortConfig.priorities[bPrefix] || sortConfig.priorities.default;
48+
49+
// First sort by priority group
50+
if (aPriority !== bPriority) {
51+
return aPriority - bPriority;
52+
}
53+
54+
// Then sort alphabetically within the same group
55+
return a.name.localeCompare(b.name);
56+
});
57+
};
58+
1059
async function fetchData(url: string, name: string): Promise<ResponseItem> {
1160
try {
1261
const response = await fetch(url, { method: "GET" });
@@ -18,7 +67,7 @@ async function fetchData(url: string, name: string): Promise<ResponseItem> {
1867
}
1968

2069
async function fetchMultipleData(
21-
requests: { url: string; name: string }[],
70+
requests: { url: string; name: string }[]
2271
): Promise<ResponseItem[]> {
2372
const promises = requests.map(({ url, name }) => fetchData(url, name));
2473
const responses = await Promise.all(promises);
@@ -45,28 +94,34 @@ const requests = [
4594
},
4695
];
4796

48-
export const prepareLinkItems = (items, refPath) => {
49-
return items.map((method) => ({
97+
export const prepareLinkItems = (items, refPath, sortConfig = METHOD_SORT_CONFIG) => {
98+
const sortedItems = sortMethods(items, sortConfig);
99+
100+
return sortedItems.map(method => ({
50101
type: "link",
51102
label: method.name,
52103
href: `/${refPath}/${method.name.toLowerCase()}`,
53-
}))
54-
}
104+
}));
105+
};
55106

56-
export const fetchAndGenerateDynamicSidebarItems = async (url: string, refPath: string, network: string) => {
107+
export const fetchAndGenerateDynamicSidebarItems = async (
108+
url: string,
109+
refPath: string,
110+
network: string
111+
) => {
57112
const dynamicRefItems = await fetchData(url, network);
58113
if (dynamicRefItems.data && !dynamicRefItems.error) {
59114
return prepareLinkItems(dynamicRefItems.data.methods, refPath);
60115
}
61-
return []
62-
}
116+
return [];
117+
};
63118

64119
export default function useNetworksMethodPlugin() {
65120
return {
66121
name: "plugin-json-rpc",
67122
async loadContent() {
68123
return await fetchMultipleData(requests)
69-
.then((responseArray) => {
124+
.then(responseArray => {
70125
return responseArray;
71126
})
72127
.catch(() => {
@@ -79,40 +134,38 @@ export default function useNetworksMethodPlugin() {
79134
const dynamicRoutes = content.find(item => item.name === NETWORK_NAMES.metamask);
80135
if (dynamicRoutes) {
81136
const methodsData = await createData(
82-
'methodsData.json',
137+
"methodsData.json",
83138
JSON.stringify(dynamicRoutes.data.methods)
84139
);
85-
dynamicRoutes.data.methods.forEach(async (method) => {
140+
dynamicRoutes.data.methods.forEach(async method => {
86141
return addRoute({
87142
path: `/${MM_REF_PATH}/${method.name.toLowerCase()}`,
88143
component: require.resolve("../components/CustomReferencePage/index.tsx"),
89144
modules: {
90145
methodsData: methodsData,
91146
},
92147
exact: true,
93-
customData: { ...method, networkName: NETWORK_NAMES.metamask }
148+
customData: { ...method, networkName: NETWORK_NAMES.metamask },
94149
});
95150
});
96151
}
97152
},
98153
configureWebpack() {
99154
return {
100-
plugins: [
101-
new NodePolyfillPlugin()
102-
],
155+
plugins: [new NodePolyfillPlugin()],
103156
resolve: {
104157
alias: {
105-
"@site-global": path.resolve(__dirname, '../methodsData.json'),
158+
"@site-global": path.resolve(__dirname, "../methodsData.json"),
106159
fs: false,
107160
module: false,
108-
"child_process": false,
109-
"worker_threads": false,
161+
child_process: false,
162+
worker_threads: false,
110163
"uglify-js": false,
111164
"@swc/core": false,
112-
"esbuild": false,
165+
esbuild: false,
113166
},
114167
},
115-
}
168+
};
116169
},
117170
};
118171
}

0 commit comments

Comments
 (0)