index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {
  2. type ComponentProps,
  3. type ReactNode,
  4. type FormEvent,
  5. useCallback,
  6. useMemo,
  7. useState,
  8. } from "react";
  9. import {
  10. DialogTrigger,
  11. TextField,
  12. Label,
  13. Input,
  14. Form,
  15. Group,
  16. } from "react-aria-components";
  17. import { StateType, useAsync } from "../../hooks/use-async";
  18. import { stringToTokens, tokensToString } from "../../tokens";
  19. import { Button } from "../Button";
  20. import { ModalDialog } from "../ModalDialog";
  21. import { Tokens } from "../Tokens";
  22. import PythTokensIcon from "../Tokens/pyth.svg";
  23. type Props = Omit<ComponentProps<typeof Button>, "children"> & {
  24. enableWithZeroMax?: boolean | undefined;
  25. actionName: ReactNode;
  26. actionDescription: ReactNode;
  27. title?: ReactNode | undefined;
  28. submitButtonText?: ReactNode | undefined;
  29. max: bigint;
  30. children?:
  31. | ((amount: Amount) => ReactNode | ReactNode[])
  32. | ReactNode
  33. | ReactNode[]
  34. | undefined;
  35. transfer?: ((amount: bigint) => Promise<void>) | undefined;
  36. };
  37. export const TransferButton = ({
  38. enableWithZeroMax,
  39. actionName,
  40. submitButtonText,
  41. actionDescription,
  42. title,
  43. max,
  44. transfer,
  45. children,
  46. isDisabled,
  47. ...props
  48. }: Props) => {
  49. return transfer === undefined ||
  50. isDisabled === true ||
  51. (max === 0n && !enableWithZeroMax) ? (
  52. <Button isDisabled={true} {...props}>
  53. {actionName}
  54. </Button>
  55. ) : (
  56. <DialogTrigger>
  57. <Button {...props}>{actionName}</Button>
  58. <TransferDialog
  59. title={title ?? actionName}
  60. description={actionDescription}
  61. max={max}
  62. transfer={transfer}
  63. submitButtonText={submitButtonText ?? actionName}
  64. >
  65. {children}
  66. </TransferDialog>
  67. </DialogTrigger>
  68. );
  69. };
  70. type TransferDialogProps = Omit<
  71. ComponentProps<typeof ModalDialog>,
  72. "children"
  73. > & {
  74. max: bigint;
  75. transfer: (amount: bigint) => Promise<void>;
  76. submitButtonText: ReactNode;
  77. children?:
  78. | ((amount: Amount) => ReactNode | ReactNode[])
  79. | ReactNode
  80. | ReactNode[]
  81. | undefined;
  82. };
  83. export const TransferDialog = ({
  84. max,
  85. transfer,
  86. submitButtonText,
  87. children,
  88. ...props
  89. }: TransferDialogProps) => {
  90. const [closeDisabled, setCloseDisabled] = useState(false);
  91. return (
  92. <ModalDialog closeDisabled={closeDisabled} {...props}>
  93. {({ close }) => (
  94. <DialogContents
  95. max={max}
  96. transfer={transfer}
  97. setCloseDisabled={setCloseDisabled}
  98. submitButtonText={submitButtonText}
  99. close={close}
  100. >
  101. {children}
  102. </DialogContents>
  103. )}
  104. </ModalDialog>
  105. );
  106. };
  107. type DialogContentsProps = {
  108. max: bigint;
  109. children: Props["children"];
  110. transfer: (amount: bigint) => Promise<void>;
  111. setCloseDisabled: (value: boolean) => void;
  112. submitButtonText: ReactNode;
  113. close: () => void;
  114. };
  115. const DialogContents = ({
  116. max,
  117. transfer,
  118. children,
  119. submitButtonText,
  120. setCloseDisabled,
  121. close,
  122. }: DialogContentsProps) => {
  123. const { amount, setAmount, setMax, stringValue } = useAmountInput(max);
  124. const validationError = useMemo(() => {
  125. switch (amount.type) {
  126. case AmountType.Empty: {
  127. return "Enter an amount";
  128. }
  129. case AmountType.AboveMax: {
  130. return "Amount exceeds maximum";
  131. }
  132. case AmountType.NotPositive: {
  133. return "Amount must be greater than zero";
  134. }
  135. case AmountType.Invalid: {
  136. return "Enter a valid amount";
  137. }
  138. case AmountType.Valid: {
  139. return;
  140. }
  141. }
  142. }, [amount]);
  143. const doTransfer = useCallback(
  144. () =>
  145. amount.type === AmountType.Valid
  146. ? transfer(amount.amount)
  147. : Promise.reject(new InvalidAmountError()),
  148. [amount, transfer],
  149. );
  150. const { execute, state } = useAsync(doTransfer);
  151. const handleSubmit = useCallback(
  152. (e: FormEvent<HTMLFormElement>) => {
  153. e.preventDefault();
  154. setCloseDisabled(true);
  155. execute()
  156. .then(() => {
  157. close();
  158. })
  159. .catch(() => {
  160. /* no-op since this is already handled in the UI using `state` and is logged in useTransfer */
  161. })
  162. .finally(() => {
  163. setCloseDisabled(false);
  164. });
  165. },
  166. [execute, close, setCloseDisabled],
  167. );
  168. return (
  169. <Form onSubmit={handleSubmit}>
  170. <TextField
  171. // eslint-disable-next-line jsx-a11y/no-autofocus
  172. autoFocus
  173. isInvalid={validationError !== undefined}
  174. value={stringValue}
  175. onChange={setAmount}
  176. validationBehavior="aria"
  177. name="amount"
  178. className="mb-8 flex w-full flex-col gap-1 sm:min-w-96"
  179. >
  180. <div className="flex flex-row items-center justify-between">
  181. <Label className="text-sm">Amount</Label>
  182. <div className="flex flex-row items-center gap-2">
  183. <Tokens>{max}</Tokens>
  184. <span className="text-xs opacity-60">Max</span>
  185. </div>
  186. </div>
  187. <Group className="relative w-full">
  188. <Input
  189. required
  190. className="focused:outline-none focused:ring-0 focused:border-pythpurple-400 w-full truncate border border-neutral-600/50 bg-transparent py-3 pl-12 pr-24 focus:border-pythpurple-400 focus:outline-none focus:ring-0 focus-visible:border-pythpurple-400 focus-visible:outline-none focus-visible:ring-0"
  191. placeholder="0.00"
  192. />
  193. <div className="pointer-events-none absolute inset-y-0 flex w-full items-center justify-between px-4">
  194. <PythTokensIcon className="size-6" />
  195. <Button
  196. size="small"
  197. variant="secondary"
  198. className="pointer-events-auto"
  199. onPress={setMax}
  200. isDisabled={state.type === StateType.Running}
  201. >
  202. max
  203. </Button>
  204. </div>
  205. </Group>
  206. {state.type === StateType.Error && (
  207. <p className="mt-1 text-red-600">
  208. Uh oh, an error occurred! Please try again
  209. </p>
  210. )}
  211. </TextField>
  212. {children && (
  213. <>{typeof children === "function" ? children(amount) : children}</>
  214. )}
  215. <Button
  216. className="mt-6 w-full"
  217. type="submit"
  218. isLoading={state.type === StateType.Running}
  219. isDisabled={amount.type !== AmountType.Valid}
  220. >
  221. {validationError ?? submitButtonText}
  222. </Button>
  223. </Form>
  224. );
  225. };
  226. const useAmountInput = (max: bigint) => {
  227. const [stringValue, setAmount] = useState<string>("");
  228. return {
  229. stringValue,
  230. setAmount,
  231. setMax: useCallback(() => {
  232. setAmount(tokensToString(max));
  233. }, [setAmount, max]),
  234. amount: useMemo((): Amount => {
  235. if (stringValue === "") {
  236. return Amount.Empty();
  237. } else {
  238. const amountAsTokens = stringToTokens(stringValue);
  239. if (amountAsTokens === undefined) {
  240. return Amount.Invalid();
  241. } else if (amountAsTokens > max) {
  242. return Amount.AboveMax(amountAsTokens);
  243. } else if (amountAsTokens <= 0) {
  244. return Amount.NotPositive(amountAsTokens);
  245. } else {
  246. return Amount.Valid(amountAsTokens);
  247. }
  248. }
  249. }, [stringValue, max]),
  250. };
  251. };
  252. export enum AmountType {
  253. Empty,
  254. NotPositive,
  255. Valid,
  256. Invalid,
  257. AboveMax,
  258. }
  259. const Amount = {
  260. Empty: () => ({ type: AmountType.Empty as const }),
  261. NotPositive: (amount: bigint) => ({
  262. type: AmountType.NotPositive as const,
  263. amount,
  264. }),
  265. Valid: (amount: bigint) => ({ type: AmountType.Valid as const, amount }),
  266. Invalid: () => ({ type: AmountType.Invalid as const }),
  267. AboveMax: (amount: bigint) => ({
  268. type: AmountType.AboveMax as const,
  269. amount,
  270. }),
  271. };
  272. type Amount = ReturnType<(typeof Amount)[keyof typeof Amount]>;
  273. class InvalidAmountError extends Error {
  274. constructor() {
  275. super("Invalid amount");
  276. }
  277. }