Skip to content

Commit

Permalink
feat: allow a single router for now
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Oct 23, 2024
1 parent cf4192c commit 2a57da9
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/pages/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ const urlValidationFn = (value: string): Error | null => {
}
}

/**
* Converts newline delimited URLs to an array of URLs, and validates each URL.
*/
const singleUrlValidationFn = (value: string): Error | null => {
try {
const urls: string[] = convertUrlInputToArray(value)
let i = 0
if (urls.length === 0) {
throw new Error('At least one URL is required. Reset the config to use defaults.')
}
if (urls.length > 1) {
throw new Error('Only one URL is allowed. Reset the config to use defaults.')
}
try {
urls.map((url, index) => {
i = index
return new URL(url)
})
} catch (e) {
throw new Error(`URL "${urls[i]}" on line ${i} is not valid`)
}
return null
} catch (err) {
return err as Error
}
}

/**
* Converts newline delimited patterns of space delimited key+value pairs to a JSON object, and validates each URL.
*
Expand Down Expand Up @@ -188,13 +215,13 @@ function ConfigPage (): React.JSX.Element | null {
localStorageKey={LOCAL_STORAGE_KEYS.config.enableWebTransport}
resetKey={resetKey}
/>
{/* TODO: we should only accept a single router URL */}
{/* For now we only accept a single router URL */}
<LocalStorageInput
className="e2e-config-page-input e2e-config-page-input-routers"
description="A newline delimited list of delegated IPFS router URLs."
localStorageKey={LOCAL_STORAGE_KEYS.config.routers}
label='Routers'
validationFn={urlValidationFn}
validationFn={singleUrlValidationFn}
defaultValue={convertUrlArrayToInput(defaultRouters)}
preSaveFormat={newlineToJsonArray}
postLoadFormat={jsonToNewlineString}
Expand Down

0 comments on commit 2a57da9

Please sign in to comment.