|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Button from '@/components/button/button'; |
| 4 | +import Input from '@/components/input/input'; |
| 5 | +import Modal from '@/components/modal/modal'; |
| 6 | +import { useNodeTokensContext } from '@/context/node-tokens'; |
| 7 | +import { useOceanAccount } from '@/lib/use-ocean-account'; |
| 8 | +import { createAuthToken } from '@/services/nodeService'; |
| 9 | +import { DURATION_UNIT_OPTIONS, type DurationUnit, toSeconds } from '@/utils/duration'; |
| 10 | +import { useFormik } from 'formik'; |
| 11 | +import posthog from 'posthog-js'; |
| 12 | +import { toast } from 'react-toastify'; |
| 13 | +import * as Yup from 'yup'; |
| 14 | +import styles from './generate-token-modal.module.css'; |
| 15 | + |
| 16 | +type GenerateTokenModalProps = { |
| 17 | + isOpen: boolean; |
| 18 | + onClose: () => void; |
| 19 | + onTokenGenerated?: (token: string) => void; |
| 20 | +}; |
| 21 | + |
| 22 | +type FormValues = { |
| 23 | + nodeId: string; |
| 24 | + expiryValue: number | ''; |
| 25 | + expiryUnit: DurationUnit; |
| 26 | +}; |
| 27 | + |
| 28 | +const GenerateTokenModal: React.FC<GenerateTokenModalProps> = ({ isOpen, onClose, onTokenGenerated }) => { |
| 29 | + const { account, signMessage } = useOceanAccount(); |
| 30 | + const { addNodeToken } = useNodeTokensContext(); |
| 31 | + |
| 32 | + const formik = useFormik<FormValues>({ |
| 33 | + initialValues: { |
| 34 | + nodeId: '', |
| 35 | + expiryValue: '', |
| 36 | + expiryUnit: 'hours', |
| 37 | + }, |
| 38 | + validationSchema: Yup.object({ |
| 39 | + nodeId: Yup.string().required('Node ID is required'), |
| 40 | + expiryValue: Yup.number().typeError('Must be a number').min(0, 'Must be 0 or greater'), |
| 41 | + }), |
| 42 | + onSubmit: async (values, { resetForm }) => { |
| 43 | + if (!account.address) { |
| 44 | + return; |
| 45 | + } |
| 46 | + try { |
| 47 | + const expirySeconds = values.expiryValue !== '' ? toSeconds(Number(values.expiryValue), values.expiryUnit) : 0; |
| 48 | + const validUntil = expirySeconds > 0 ? Date.now() + expirySeconds * 1000 : undefined; |
| 49 | + const { token: generatedToken } = await createAuthToken({ |
| 50 | + consumerAddress: account.address, |
| 51 | + nodeUri: values.nodeId, |
| 52 | + signMessage, |
| 53 | + validUntil, |
| 54 | + }); |
| 55 | + addNodeToken({ |
| 56 | + createdAt: Date.now(), |
| 57 | + expiryTimestamp: validUntil, |
| 58 | + nodeId: values.nodeId, |
| 59 | + nodeUri: values.nodeId, |
| 60 | + token: generatedToken, |
| 61 | + }); |
| 62 | + posthog.capture('authToken_generated', { nodeId: values.nodeId }); |
| 63 | + toast.success('Auth token generated'); |
| 64 | + resetForm(); |
| 65 | + onTokenGenerated?.(generatedToken); |
| 66 | + onClose(); |
| 67 | + } catch (error) { |
| 68 | + console.error('Failed to generate auth token:', error); |
| 69 | + toast.error('Failed to generate auth token'); |
| 70 | + } |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const handleClose = () => { |
| 75 | + if (formik.isSubmitting) { |
| 76 | + return; |
| 77 | + } |
| 78 | + formik.resetForm(); |
| 79 | + onClose(); |
| 80 | + }; |
| 81 | + |
| 82 | + const expiryError = formik.touched.expiryValue && formik.errors.expiryValue ? formik.errors.expiryValue : undefined; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Modal |
| 86 | + hideCloseButton={formik.isSubmitting} |
| 87 | + isOpen={isOpen} |
| 88 | + onClose={handleClose} |
| 89 | + title="Generate auth token" |
| 90 | + width="sm" |
| 91 | + > |
| 92 | + <form className={styles.form} onSubmit={formik.handleSubmit}> |
| 93 | + <Input |
| 94 | + errorText={formik.touched.nodeId && formik.errors.nodeId ? formik.errors.nodeId : undefined} |
| 95 | + label="Node ID" |
| 96 | + name="nodeId" |
| 97 | + onBlur={formik.handleBlur} |
| 98 | + onChange={formik.handleChange} |
| 99 | + placeholder="Enter node peer ID" |
| 100 | + type="text" |
| 101 | + value={formik.values.nodeId} |
| 102 | + /> |
| 103 | + <Input |
| 104 | + endAdornment={ |
| 105 | + <div className={styles.expiryControls}> |
| 106 | + <select |
| 107 | + aria-label="Expiration unit" |
| 108 | + className={styles.unitSelect} |
| 109 | + name="expiryUnit" |
| 110 | + onBlur={formik.handleBlur} |
| 111 | + onChange={formik.handleChange} |
| 112 | + value={formik.values.expiryUnit} |
| 113 | + > |
| 114 | + {DURATION_UNIT_OPTIONS.map((opt) => ( |
| 115 | + <option key={opt.value} value={opt.value}> |
| 116 | + {opt.label} |
| 117 | + </option> |
| 118 | + ))} |
| 119 | + </select> |
| 120 | + </div> |
| 121 | + } |
| 122 | + errorText={expiryError} |
| 123 | + label="Token expiration (optional)" |
| 124 | + min={0} |
| 125 | + name="expiryValue" |
| 126 | + onBlur={formik.handleBlur} |
| 127 | + onChange={(e) => { |
| 128 | + if (e.target.value === '') { |
| 129 | + formik.setFieldValue('expiryValue', ''); |
| 130 | + return; |
| 131 | + } |
| 132 | + formik.setFieldValue('expiryValue', Math.max(0, Number(e.target.value))); |
| 133 | + }} |
| 134 | + type="number" |
| 135 | + value={formik.values.expiryValue} |
| 136 | + /> |
| 137 | + <div className="actionsGroupMdEnd"> |
| 138 | + <Button |
| 139 | + color="accent1" |
| 140 | + disabled={formik.isSubmitting} |
| 141 | + onClick={handleClose} |
| 142 | + size="md" |
| 143 | + type="button" |
| 144 | + variant="outlined" |
| 145 | + > |
| 146 | + Cancel |
| 147 | + </Button> |
| 148 | + <Button color="accent1" disabled={!formik.isValid} loading={formik.isSubmitting} size="md" type="submit"> |
| 149 | + Generate |
| 150 | + </Button> |
| 151 | + </div> |
| 152 | + </form> |
| 153 | + </Modal> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export default GenerateTokenModal; |
0 commit comments