Skip to content

Commit

Permalink
refactor: optimized key in form & anonymous functions Refactoring (#835)
Browse files Browse the repository at this point in the history
* refactor: remove unused key and avoid use index as key

* refactor: useMemo instead of anonymous functions

* refactor: useMemo instead of anonymous functions

---------

Co-authored-by: Brent C <[email protected]>
  • Loading branch information
momodaka and Brent C authored Feb 9, 2024
1 parent a5cc47b commit a08416f
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 56 deletions.
4 changes: 1 addition & 3 deletions website/src/components/InputForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const createFormField = (field, index) => (
<Form.Item
label={nameOrIndex(field, index)}
name={nameOrIndex(field, index)}
key={index}
>
<Input placeholder={field.type} />
</Form.Item>
Expand All @@ -20,10 +19,9 @@ const createFormField = (field, index) => (
export const FormGenerator = ({ formData }) => {
const renderFormFields = (fields) => {
return fields.map((field, index) => {
console.log("field", field);
if (field.members) {
return (
<div key={index}>
<div key={nameOrIndex(field, index)}>
<Title level={4}>{nameOrIndex(field, index)}</Title>
{renderFormFields(field.members)}
</div>
Expand Down
20 changes: 14 additions & 6 deletions website/src/tabs/account/SignMessage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Card, Divider, Form, Input } from "antd";
import { CopyButton } from "../../components/CopyButton";
import { useAleoWASM } from "../../aleo-wasm-hook";
Expand Down Expand Up @@ -37,9 +37,17 @@ export const SignMessage = () => {

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };

const signatureString = useMemo(() => {
return signingKey !== null ? signingKey : ""
}, [signingKey]);

const messageString = useMemo(() => {
return message !== null ? message : ""
}, [signingKey]);


if (aleo !== null) {
const signatureString = () => (signingKey !== null ? signingKey : "");
const messageString = () => (message !== null ? message : "");


return (
<Card
Expand All @@ -61,7 +69,7 @@ export const SignMessage = () => {
name="Message"
size="large"
placeholder="Message"
value={messageString()}
value={messageString}
allowClear={true}
onChange={onMessageChange}
/>
Expand All @@ -74,9 +82,9 @@ export const SignMessage = () => {
<Input
size="large"
placeholder="Signature"
value={signatureString()}
value={signatureString}
addonAfter={
<CopyButton data={signatureString()} />
<CopyButton data={signatureString} />
}
disabled
/>
Expand Down
11 changes: 6 additions & 5 deletions website/src/tabs/rest/GetBlockByHash.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Card, Divider, Form, Input, Row, Col } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand Down Expand Up @@ -41,8 +41,9 @@ export const GetBlockByHash = () => {

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };

const blockString = () =>
blockByHash !== null ? blockByHash.toString() : "";
const blockString = useMemo(() => {
return blockByHash !== null ? blockByHash.toString() : ""
}, [blockByHash]);

return (
<Card
Expand Down Expand Up @@ -74,13 +75,13 @@ export const GetBlockByHash = () => {
size="large"
rows={15}
placeholder="Block"
value={blockString()}
value={blockString}
disabled
/>
</Form.Item>
</Col>
<Col span={1} align="middle">
<CopyButton data={blockString()} />
<CopyButton data={blockString} />
</Col>
</Row>
</Form>
Expand Down
11 changes: 6 additions & 5 deletions website/src/tabs/rest/GetBlockByHeight.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Card, Divider, Form, Input, Row, Col } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand Down Expand Up @@ -43,8 +43,9 @@ export const GetBlockByHeight = () => {

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };

const blockString = () =>
blockByHeight !== null ? blockByHeight.toString() : "";
const blockString = useMemo(() => {
return blockByHeight !== null ? blockByHeight.toString() : ""
}, [blockByHeight]);

return (
<Card
Expand Down Expand Up @@ -76,13 +77,13 @@ export const GetBlockByHeight = () => {
size="large"
rows={15}
placeholder="Block"
value={blockString()}
value={blockByHeight}
disabled
/>
</Form.Item>
</Col>
<Col span={1} align="middle">
<CopyButton data={blockString()} />
<CopyButton data={blockString} />
</Col>
</Row>
</Form>
Expand Down
13 changes: 7 additions & 6 deletions website/src/tabs/rest/GetLatestBlock.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Button, Card, Col, Divider, Form, Input, Row } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand All @@ -21,8 +21,9 @@ export const GetLatestBlock = () => {

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };

const latestBlockString = () =>
latestBlock !== null ? latestBlock.toString() : "";
const latestBlockString = useMemo(() => {
return latestBlock !== null ? latestBlock.toString() : ""
}, [latestBlock]);

return (
<Card
Expand All @@ -33,7 +34,7 @@ export const GetLatestBlock = () => {
<Col>
<Button
type="primary"

size="middle"
onClick={tryRequest}
>
Expand All @@ -51,13 +52,13 @@ export const GetLatestBlock = () => {
size="large"
rows={15}
placeholder="Block"
value={latestBlockString()}
value={latestBlockString}
disabled
/>
</Form.Item>
</Col>
<Col span={1} align="middle">
<CopyButton data={latestBlockString()} />
<CopyButton data={latestBlockString} />
</Col>
</Row>
</Form>
Expand Down
13 changes: 7 additions & 6 deletions website/src/tabs/rest/GetLatestBlockHeight.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Button, Card, Col, Divider, Form, Input, Row } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand All @@ -21,8 +21,9 @@ export const GetLatestBlockHeight = () => {

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };

const latestHeightString = () =>
latestHeight !== null ? latestHeight.toString() : "";
const latestHeightString = useMemo(() => {
return latestHeight !== null ? latestHeight.toString() : ""
}, [latestHeight]);

return (
<Card
Expand All @@ -33,7 +34,7 @@ export const GetLatestBlockHeight = () => {
<Col>
<Button
type="primary"

size="middle"
onClick={tryRequest}
>
Expand All @@ -49,10 +50,10 @@ export const GetLatestBlockHeight = () => {
size="large"
rows={1}
placeholder="Block"
value={latestHeightString()}
value={latestHeightString}
addonAfter={
<CopyButton
data={latestHeightString()}
data={latestHeightString}
/>
}
disabled
Expand Down
20 changes: 13 additions & 7 deletions website/src/tabs/rest/GetMappingNames.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Button, Card, Col, Divider, Form, Input, Row } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand Down Expand Up @@ -56,8 +56,14 @@ export const GetMappingNames = () => {
};

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };
const mappingString = () => (mapping !== null ? mapping : "");
const programIDString = () => (programID !== null ? programID : "");

const mappingString = useMemo(() => {
return mapping !== null ? mapping : ""
}, [mapping]);

const programIDString = useMemo(() => {
return programID !== null ? programID : ""
}, [programID]);

return (
<Card
Expand All @@ -66,7 +72,7 @@ export const GetMappingNames = () => {
extra={
<Button
type="primary"

size="middle"
onClick={() => {
tryRequest("credits.aleo");
Expand All @@ -89,7 +95,7 @@ export const GetMappingNames = () => {
allowClear
onSearch={onSearch}
onChange={onChange}
value={programIDString()}
value={programIDString}
/>
</Form.Item>
</Form>
Expand All @@ -107,13 +113,13 @@ export const GetMappingNames = () => {
whiteSpace: "pre-wrap",
overflowWrap: "break-word",
}}
value={mappingString()}
value={mappingString}
disabled
/>
</Form.Item>
</Col>
<Col span={1} align="middle">
<CopyButton data={mappingString()} />
<CopyButton data={mappingString} />
</Col>
</Row>
</Form>
Expand Down
13 changes: 7 additions & 6 deletions website/src/tabs/rest/GetMappingValue.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Button, Card, Col, Divider, Form, Input, Result, Row } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand Down Expand Up @@ -43,8 +43,9 @@ export const GetMappingValue = () => {
setMappingKey(key);
};

const mappingErrorString = () =>
mappingError !== null ? mappingError : "";
const mappingErrorString = useMemo(() => {
return mappingError !== null ? mappingError : ""
}, [mappingError]);

// Attempts to request the program bytecode with the given program id.
const tryRequest = () => {
Expand Down Expand Up @@ -92,7 +93,7 @@ export const GetMappingValue = () => {
extra={
<Button
type="primary"

size="middle"
onClick={() => {
setDefaultRequest(
Expand Down Expand Up @@ -142,7 +143,7 @@ export const GetMappingValue = () => {
<Col>
<Button
type="primary"

size="middle"
onClick={tryRequest}
>
Expand Down Expand Up @@ -179,7 +180,7 @@ export const GetMappingValue = () => {
<Result
status="error"
title="Mapping Error"
subTitle={"Error: " + mappingErrorString()}
subTitle={"Error: " + mappingErrorString}
/>
)}
</Card>
Expand Down
20 changes: 13 additions & 7 deletions website/src/tabs/rest/GetProgram.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import {useMemo, useState} from "react";
import { Button, Card, Col, Divider, Form, Input, Row } from "antd";
import axios from "axios";
import { CopyButton } from "../../components/CopyButton";
Expand Down Expand Up @@ -54,8 +54,14 @@ export const GetProgram = () => {
};

const layout = { labelCol: { span: 3 }, wrapperCol: { span: 21 } };
const programString = () => (program !== null ? program : "");
const programIDString = () => (programID !== null ? programID : "");

const programString = useMemo(() => {
return program !== null ? program : ""
}, [program]);

const programIDString = useMemo(() => {
return programID !== null ? programID : ""
}, [programID])

return (
<Card
Expand All @@ -64,7 +70,7 @@ export const GetProgram = () => {
extra={
<Button
type="primary"

size="middle"
onClick={() => {
tryRequest("credits.aleo");
Expand All @@ -87,7 +93,7 @@ export const GetProgram = () => {
allowClear
onSearch={onSearch}
onChange={onChange}
value={programIDString()}
value={programIDString}
/>
</Form.Item>
</Form>
Expand All @@ -105,13 +111,13 @@ export const GetProgram = () => {
whiteSpace: "pre-wrap",
overflowWrap: "break-word",
}}
value={programString()}
value={programString}
disabled
/>
</Form.Item>
</Col>
<Col span={1} align="middle">
<CopyButton data={programString()} />
<CopyButton data={programString} />
</Col>
</Row>
</Form>
Expand Down
Loading

0 comments on commit a08416f

Please sign in to comment.