Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix custom network on URI #25

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/pages/Network/Network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,19 @@ import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
import { ScrollArea } from "@/components/ui/scroll-area"
import {
isValidUri,
Network,
networkCategories,
onChangeChain,
SelectedChain,
selectedChain$,
} from "@/state/chains/chain.state"
import { addCustomNetwork, getCustomNetwork } from "@/state/chains/networks"
import { useStateObservable } from "@react-rxjs/core"
import { useCommandState } from "cmdk"
import { Check, ChevronDown } from "lucide-react"
import { FC, useEffect, useRef, useState } from "react"

const isValidUri = (input: string): boolean => {
try {
new URL(input)
} catch {
return false
}
return true
}

const EmptyOption: React.FC<{
enteredText: string
selectedNetwork: Network
Expand Down Expand Up @@ -114,6 +107,7 @@ export function NetworkSwitcher() {
</Button>
</DialogTrigger>
<NetworkSwitchDialogContent
key={selectedChain.endpoint}
selectedChain={selectedChain}
onClose={() => setOpen(false)}
/>
Expand Down Expand Up @@ -148,10 +142,16 @@ const NetworkSwitchDialogContent: FC<{
}

const handleConfirm = () => {
onChangeChain({
network: selectedNetwork,
endpoint: selectedRpc,
})
if (selectedNetwork.id === "custom-network") {
addCustomNetwork(selectedRpc)
onChangeChain({ network: getCustomNetwork(), endpoint: selectedRpc })
setEnteredText("")
} else {
onChangeChain({
network: selectedNetwork,
endpoint: selectedRpc,
})
}
onClose()
}

Expand Down
50 changes: 36 additions & 14 deletions src/state/chains/chain.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import {
switchMap,
tap,
} from "rxjs"
import { defaultNetwork, Network, networkCategories } from "./networks"
import {
addCustomNetwork,
defaultNetwork,
getCustomNetwork,
Network,
networkCategories,
} from "./networks"
import {
createSmoldotSource,
getSmoldotProvider,
Expand Down Expand Up @@ -64,23 +70,39 @@ selectedChainChanged$.subscribe(({ network, endpoint }) =>
endpoint,
}),
)
const getDefaultChain = (): SelectedChain => {
const findNetwork = (networkId: string) => {
for (const { networks } of networkCategories) {
for (const network of networks) {
if (network.id === networkId) {
return network
}
}
}
return null

const allNetworks = networkCategories.map((x) => x.networks).flat()
const findNetwork = (networkId: string): Network | undefined =>
allNetworks.find((x) => x.id == networkId)

export const isValidUri = (input: string): boolean => {
try {
new URL(input)
} catch {
return false
}
return true
}

const defaultSelectedChain: SelectedChain = {
network: defaultNetwork,
endpoint: "light-client",
}
const getDefaultChain = (): SelectedChain => {
const hashParams = getHashParams()
if (hashParams.has("networkId") && hashParams.has("endpoint")) {
const network = findNetwork(hashParams.get("networkId")!)
if (network) {
return { network, endpoint: hashParams.get("endpoint")! }
const networkId = hashParams.get("networkId")!
const endpoint = hashParams.get("endpoint")!
if (networkId === "custom") {
if (!isValidUri(endpoint)) return defaultSelectedChain
addCustomNetwork(endpoint)
return {
network: getCustomNetwork(),
endpoint,
}
}
const network = findNetwork(networkId)
if (network) return defaultSelectedChain
}

return {
Expand Down
13 changes: 9 additions & 4 deletions src/state/chains/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const networks = {
Kusama,
Paseo,
Westend,
Development: [
Custom: [
{
id: "localhost",
display: "Localhost",
id: "custom",
display: "Custom",
lightclient: false,
endpoints: {
"Local (ws://127.0.0.1:9944)": "ws://127.0.0.1:9944",
"ws://127.0.0.1:9944": "ws://127.0.0.1:9944",
},
} as Network,
],
Expand All @@ -54,4 +54,9 @@ export const networkCategories: NetworkCategory[] = Object.entries(
networks,
).map(([name, networks]) => ({ name, networks }))

export const getCustomNetwork = () => networks.Custom[0]
export const addCustomNetwork = (uri: string) => {
getCustomNetwork().endpoints[uri] = uri
}

export const defaultNetwork = Polkadot[0]