Skip to content

Commit

Permalink
chore: simplify variant & composite scraped object
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat committed Jun 17, 2024
1 parent e16ab29 commit 555ba09
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
18 changes: 11 additions & 7 deletions src/model/Scraper/Types/Composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ export class Composite extends Base implements MetadataType {

// Scrape composite fields. Overwrites `fields` with scraped fields.
scrape(scraper: MetadataScraper, { trailId }: TypeParams) {
return [...this.fields].map((field, index) => {
return [...this.fields].map(({ type, name, typeName }, index) => {
const inputKey = `${this.inputKey}_${index}`;

const result = {
...field,
let result = {
name,
typeName,
};

// If this field is a known custom type, do not scrape child types. Otherwise, continue.
if (this.input() === 'indent') {
result.type = scraper.start(field.type, {
parentTrailId: trailId,
inputKey,
});
result = {
...result,
...scraper.start(type, {
parentTrailId: trailId,
inputKey,
}),
};
}

return result;
Expand Down
15 changes: 8 additions & 7 deletions src/model/Scraper/Types/Variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ export class Variant extends Base implements MetadataType {
// Scrape variant fields. Overwrites `fields` with scraped fields.
scrape(scraper: MetadataScraper, { trailId }: TypeParams) {
// NOTE: This input key logic may need to be revised.
return [...this.items].map((item) => ({
...item,
fields: item.fields.map((field, index) => {
const inputKey = `${this.inputKey}_${index}`;

return [...this.items].map(({ index, name, fields }) => ({
index,
name,
fields: fields.map(({ type, name: fieldName, typeName }, i) => {
const inputKey = `${this.inputKey}_${i}`;
return {
...field,
type: scraper.start(field.type, { parentTrailId: trailId, inputKey }),
name: fieldName,
typeName,
...scraper.start(type, { parentTrailId: trailId, inputKey }),
};
}),
}));
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Chain/Extrinsics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Extrinsics = () => {
if (!activePallet || !activeItem) {
return null;
}
// NOTE: Currently limiting scraper to 7 recursive levels to avoid app freezing.
// NOTE: Currently limiting scraper to 7 recursive levels to improve performance.
const scraper = new PalletScraper(Metadata, { maxDepth: 7 });
return scraper.getCallItem(activePallet, activeItem);
}, [items, activeItem, activePallet]);
Expand Down
17 changes: 10 additions & 7 deletions src/routes/Chain/Inputs/useInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const useInput = () => {
<Section indent={true}>
{arg.composite.map((field: AnyJson, index: number) => (
<Fragment key={`input_arg_${inputKey}_${index}`}>
{readInput(field.type, config, { prependLabel: field?.name })}
{readInput(field, config, { prependLabel: field?.name })}
</Fragment>
))}
</Section>
Expand Down Expand Up @@ -230,12 +230,15 @@ export const useInput = () => {
{/* Render selected variant item's fields if they exist. */}
{selectedItemFields && (
<Section indent={true}>
{selectedItemFields.map((field: AnyJson, index: number) => (
<Fragment key={`input_arg_${inputKey}_${index}`}>
<h4 className="standalone">{field.typeName}</h4>
{readInput(field.type, config)}
</Fragment>
))}
{selectedItemFields.map((field: AnyJson, index: number) => {
const { typeName, ...rest } = field;
return (
<Fragment key={`input_arg_${inputKey}_${index}`}>
<h4 className="standalone">{typeName}</h4>
{readInput(rest, config)}
</Fragment>
);
})}
</Section>
)}
</>
Expand Down

0 comments on commit 555ba09

Please sign in to comment.