Skip to content

Commit 1b33556

Browse files
authored
fix(instructions): auto-switch to Aliases tab on alias-only searches (#1216)
* Update InstructionGroups.tsx * fix eslint error * Update InstructionGroups.tsx
1 parent b40349b commit 1b33556

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

src/components/Instructions/InstructionGroups.tsx

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const InstructionGroups = React.memo(({ instructions, aliases, search }:
4949
return acc;
5050
}, {});
5151
return aliases.map((alias) => ({ ...alias, instruction: instructionsByMnemonic[alias.alias_of] }));
52-
}, [instructions]);
52+
}, [instructions, aliases]);
5353

5454
const searchValue = search.toLowerCase();
5555

@@ -58,43 +58,61 @@ export const InstructionGroups = React.memo(({ instructions, aliases, search }:
5858
item.doc?.opcode?.toLowerCase()?.includes(searchValue) ||
5959
item.doc?.fift?.toLowerCase()?.includes(searchValue) ||
6060
item?.doc?.description?.toLowerCase()?.includes(searchValue),
61-
), [searchValue]);
61+
), [instructions, searchValue]);
6262

6363
const filteredAliases = useMemo(() => aliasesWithInstructions.filter(
6464
(item) =>
6565
item.mnemonic?.toLowerCase()?.includes(searchValue) ||
6666
item.description?.toLowerCase()?.includes(searchValue) ||
6767
item.doc_fift?.toLowerCase()?.includes(searchValue),
68-
), [searchValue]);
68+
), [aliasesWithInstructions, searchValue]);
6969

70-
return <Tabs>
71-
{
72-
sections.map(({ label, types, value }) => {
70+
const activeTab = useMemo(() => {
71+
if (filteredAliases.length > 0 && filteredInstructions.length === 0) {
72+
return 'alias';
73+
}
74+
return 'all';
75+
}, [filteredAliases, filteredInstructions]);
76+
77+
return (
78+
<Tabs key={activeTab} defaultValue={activeTab}>
79+
{sections.map(({ label, types, value }) => {
7380
if (value === 'alias') {
74-
return filteredAliases?.length ? <TabItem label={label} value={value} key={value}>
75-
<InstructionTable instructions={filteredAliases.map((alias) => ({
76-
opcode: alias.instruction?.doc?.opcode,
77-
fift: alias.doc_fift,
78-
gas: alias.instruction?.doc?.gas,
79-
description: alias.description,
80-
stack: alias.doc_stack,
81-
}))}/>
82-
</TabItem> : null;
81+
if (!filteredAliases.length) return null;
82+
return (
83+
<TabItem label={label} value={value} key={value}>
84+
<InstructionTable
85+
instructions={filteredAliases.map((alias) => ({
86+
opcode: alias.instruction?.doc.opcode,
87+
fift: alias.doc_fift,
88+
gas: alias.instruction?.doc.gas,
89+
description: alias.description,
90+
stack: alias.doc_stack,
91+
}))}
92+
/>
93+
</TabItem>
94+
);
8395
}
8496

85-
const tabInstructions = types ? filteredInstructions.filter(instruction => types.includes(instruction.doc.category)) : filteredInstructions;
86-
return (tabInstructions?.length ?
97+
const tabInstructions = types
98+
? filteredInstructions.filter((inst) => types.includes(inst.doc.category))
99+
: filteredInstructions;
100+
101+
if (!tabInstructions.length) return null;
102+
return (
87103
<TabItem label={label} value={value} key={value}>
88104
<InstructionTable
89-
instructions={tabInstructions.map(instruction => ({
90-
opcode: instruction.doc?.opcode,
91-
fift: instruction.doc?.fift,
92-
gas: instruction.doc?.gas,
93-
description: instruction?.doc.description,
94-
stack: instruction.doc?.stack,
95-
}))}/>
96-
</TabItem> : null);
97-
})
98-
}
99-
</Tabs>;
105+
instructions={tabInstructions.map((inst) => ({
106+
opcode: inst.doc.opcode,
107+
fift: inst.doc.fift,
108+
gas: inst.doc.gas,
109+
description: inst.doc.description,
110+
stack: inst.doc.stack,
111+
}))}
112+
/>
113+
</TabItem>
114+
);
115+
})}
116+
</Tabs>
117+
);
100118
});

0 commit comments

Comments
 (0)