-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the selection of a custom IPFS gateway (#557)
- Loading branch information
1 parent
3d0cc3a
commit fcbb8b9
Showing
5 changed files
with
262 additions
and
85 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,120 @@ | ||
import { Form, Input, Modal, Typography } from "antd"; | ||
import { useState } from "react"; | ||
import { z } from "zod"; | ||
|
||
import IconClose from "src/assets/icons/x.svg?react"; | ||
import { useEnvContext } from "src/contexts/Env"; | ||
import { getStorageByKey, setStorageByKey } from "src/utils/browser"; | ||
import { | ||
CLOSE, | ||
IPFS_CUSTOM_GATEWAY_KEY, | ||
IPFS_PUBLIC_GATEWAY_CHECKER_URL, | ||
SAVE, | ||
URL_FIELD_ERROR_MESSAGE, | ||
} from "src/utils/constants"; | ||
|
||
export function SettingsModal({ | ||
afterOpenChange, | ||
isOpen, | ||
onClose, | ||
onSave, | ||
}: { | ||
afterOpenChange: (isOpen: boolean) => void; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
onSave: () => void; | ||
}) { | ||
const env = useEnvContext(); | ||
const [form] = Form.useForm(); | ||
|
||
const defaultIpfsGatewayUrl = getStorageByKey({ | ||
defaultValue: env.ipfsGatewayUrl, | ||
key: IPFS_CUSTOM_GATEWAY_KEY, | ||
parser: z.string().url(), | ||
}); | ||
|
||
const [areSettingsValid, setAreSettingsValid] = useState<boolean>(false); | ||
|
||
const onResetToDefault = () => { | ||
form.setFieldValue("ipfsGatewayUrl", env.ipfsGatewayUrl); | ||
void form.validateFields(["ipfsGatewayUrl"]); | ||
}; | ||
|
||
const onSaveClick = () => { | ||
const parsedValue = z.string().url().safeParse(form.getFieldValue("ipfsGatewayUrl")); | ||
if (parsedValue.success) { | ||
const value = parsedValue.data.trim(); | ||
const sanitizedValue = value.endsWith("/") ? value.slice(0, -1) : value; | ||
setStorageByKey({ | ||
key: IPFS_CUSTOM_GATEWAY_KEY, | ||
value: sanitizedValue, | ||
}); | ||
onSave(); | ||
} | ||
}; | ||
|
||
const onFormChange = () => { | ||
setAreSettingsValid(!form.getFieldsError().some(({ errors }) => errors.length)); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
afterOpenChange={afterOpenChange} | ||
cancelText={CLOSE} | ||
centered | ||
closable | ||
closeIcon={<IconClose />} | ||
maskClosable | ||
okButtonProps={{ disabled: !areSettingsValid }} | ||
okText={SAVE} | ||
onCancel={onClose} | ||
onOk={onSaveClick} | ||
open={isOpen} | ||
style={{ maxWidth: 400 }} | ||
title="Change IPFS gateway" | ||
> | ||
<Form | ||
form={form} | ||
initialValues={{ | ||
ipfsGatewayUrl: defaultIpfsGatewayUrl, | ||
}} | ||
layout="vertical" | ||
onFieldsChange={onFormChange} | ||
> | ||
<Typography.Paragraph style={{ whiteSpace: "pre-line" }} type="secondary"> | ||
The IPFS gateway makes it possible to access files hosted on IPFS. You can customize the | ||
IPFS gateway or continue using the default.{"\n"} | ||
{"\n"}You can use sites like the{" "} | ||
<Typography.Link href={IPFS_PUBLIC_GATEWAY_CHECKER_URL} target="_blank"> | ||
IPFS Public Gateway Checker | ||
</Typography.Link>{" "} | ||
to choose a custom gateway. | ||
</Typography.Paragraph> | ||
<Form.Item | ||
extra={ | ||
<Typography.Link | ||
onClick={onResetToDefault} | ||
style={{ display: "block", textAlign: "right" }} | ||
> | ||
Reset to default | ||
</Typography.Link> | ||
} | ||
label="IPFS gateway URL" | ||
name="ipfsGatewayUrl" | ||
required | ||
rules={[ | ||
{ | ||
required: true, | ||
}, | ||
{ | ||
message: URL_FIELD_ERROR_MESSAGE, | ||
validator: (_, value) => z.string().url().parseAsync(value), | ||
}, | ||
]} | ||
> | ||
<Input placeholder={env.ipfsGatewayUrl} /> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
); | ||
} |
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
Oops, something went wrong.