-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sim
committed
Jan 10, 2022
1 parent
d789246
commit e3519f7
Showing
8 changed files
with
205 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useTranslation } from "react-i18next" | ||
import { useNavigate } from "react-router-dom" | ||
import { useForm } from "react-hook-form" | ||
import { useNetworkState } from "data/wallet" | ||
import { useCustomNetworks } from "data/settings/CustomNetworks" | ||
import { Form, FormItem, Submit, Input } from "components/form" | ||
import { useNetworks } from "app/NetworksProvider" | ||
|
||
const AddNetwork = () => { | ||
const { t } = useTranslation() | ||
const navigate = useNavigate() | ||
const { mainnet } = useNetworks() | ||
const [, setNetwork] = useNetworkState() | ||
const { add, validateName } = useCustomNetworks() | ||
|
||
/* form */ | ||
const form = useForm<TerraNetwork>({ mode: "onChange" }) | ||
const { register, handleSubmit, formState } = form | ||
const { errors } = formState | ||
|
||
const submit = (values: TerraNetwork) => { | ||
add(values) | ||
setNetwork(values.name) | ||
navigate("/") | ||
} | ||
|
||
return ( | ||
<Form onSubmit={handleSubmit(submit)}> | ||
<FormItem label={t("Name")} error={errors.name?.message}> | ||
<Input | ||
{...register("name", { | ||
required: true, | ||
validate: { | ||
exists: (value) => | ||
!validateName(value) ? `${value} already exists` : undefined, | ||
}, | ||
})} | ||
placeholder={mainnet.name} | ||
autoFocus | ||
/> | ||
</FormItem> | ||
|
||
<FormItem label={t("Chain ID")} error={errors.chainID?.message}> | ||
<Input | ||
{...register("chainID", { required: true })} | ||
placeholder={mainnet.chainID} | ||
/> | ||
</FormItem> | ||
|
||
<FormItem label={t("LCD")} error={errors.lcd?.message}> | ||
<Input | ||
{...register("lcd", { required: true })} | ||
placeholder={mainnet.lcd} | ||
/> | ||
</FormItem> | ||
|
||
<Submit /> | ||
</Form> | ||
) | ||
} | ||
|
||
export default AddNetwork |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Card, Page } from "components/layout" | ||
import { useTranslation } from "react-i18next" | ||
import AddNetworkForm from "./AddNetworkForm" | ||
|
||
const AddNetworkPage = () => { | ||
const { t } = useTranslation() | ||
return ( | ||
<Page title={t("Add a network")} small> | ||
<Card> | ||
<AddNetworkForm /> | ||
</Card> | ||
</Page> | ||
) | ||
} | ||
|
||
export default AddNetworkPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect } from "react" | ||
import { useTranslation } from "react-i18next" | ||
import { useNavigate } from "react-router-dom" | ||
import { useNetworkState } from "data/wallet" | ||
import { useCustomNetworks } from "data/settings/CustomNetworks" | ||
import { Grid } from "components/layout" | ||
import CustomItem from "txs/AddressBook/CustomItem" | ||
|
||
const ManageNetworksForm = () => { | ||
const { t } = useTranslation() | ||
const navigate = useNavigate() | ||
const [, setNetwork] = useNetworkState() | ||
const { list, remove } = useCustomNetworks() | ||
const empty = !list.length | ||
|
||
useEffect(() => { | ||
if (empty) navigate("/") | ||
}, [empty, navigate]) | ||
|
||
return ( | ||
<Grid gap={12}> | ||
{list.map((item) => { | ||
const { name, chainID, lcd } = item | ||
return ( | ||
<CustomItem | ||
name={name} | ||
contents={[ | ||
{ title: t("Chain ID"), desc: chainID }, | ||
{ title: t("LCD"), desc: lcd }, | ||
]} | ||
onClick={() => setNetwork(name)} | ||
onDelete={() => remove(name)} | ||
key={name} | ||
/> | ||
) | ||
})} | ||
</Grid> | ||
) | ||
} | ||
|
||
export default ManageNetworksForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useTranslation } from "react-i18next" | ||
import { Link } from "react-router-dom" | ||
import AddIcon from "@mui/icons-material/Add" | ||
import { Card, Page } from "components/layout" | ||
import ManageNetworksForm from "./ManageNetworksForm" | ||
|
||
const ManageNetworksPage = () => { | ||
const { t } = useTranslation() | ||
|
||
const add = ( | ||
<Link to="/network/new"> | ||
<AddIcon /> | ||
</Link> | ||
) | ||
|
||
return ( | ||
<Page title={t("Manage networks")} small> | ||
<Card title={t("Custom networks")} extra={add}> | ||
<ManageNetworksForm /> | ||
</Card> | ||
</Page> | ||
) | ||
} | ||
|
||
export default ManageNetworksPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { atom, useRecoilState } from "recoil" | ||
import { fromPairs } from "ramda" | ||
import { getLocalSetting, setLocalSetting } from "utils/localStorage" | ||
import { SettingKey } from "utils/localStorage" | ||
import { useNetworks } from "app/NetworksProvider" | ||
|
||
const customNetworksState = atom({ | ||
key: "customNetworks", | ||
default: getLocalSetting<TerraNetwork[]>(SettingKey.CustomNetworks), | ||
}) | ||
|
||
export const useCustomNetworks = () => { | ||
const [customNetworks, setCustomNetworks] = | ||
useRecoilState(customNetworksState) | ||
|
||
const validateName = (name: string) => | ||
!customNetworks.some((network) => network.name === name) | ||
|
||
const updateList = (list: TerraNetwork[]) => { | ||
setCustomNetworks(list) | ||
setLocalSetting(SettingKey.CustomNetworks, list) | ||
} | ||
|
||
const add = (newNetwork: TerraNetwork) => { | ||
if (!validateName(newNetwork.name)) | ||
throw new Error(`Already exists: ${newNetwork.name}`) | ||
|
||
updateList([...customNetworks, newNetwork]) | ||
} | ||
|
||
const remove = (name: string) => | ||
updateList(customNetworks.filter((item) => item.name !== name)) | ||
|
||
return { list: customNetworks, add, remove, validateName } | ||
} | ||
|
||
export const useMergeNetworks = () => { | ||
const networks = useNetworks() | ||
const { list } = useCustomNetworks() | ||
const custom = fromPairs(list.map((item) => [item.name, item])) | ||
return { ...networks, ...custom } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters