|
| 1 | +import { Form } from '@heroui/form'; |
| 2 | +import { Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, useDisclosure } from '@heroui/modal'; |
| 3 | +import { createTunnel, renameTunnel } from '@lib/api/tunnels'; |
| 4 | +import Label from '@shared/Label'; |
| 5 | +import StyledInput from '@shared/StyledInput'; |
| 6 | +import SubmitButton from '@shared/SubmitButton'; |
| 7 | +import { Tunnel } from '@typedefs/tunnels'; |
| 8 | +import { forwardRef, useImperativeHandle, useState } from 'react'; |
| 9 | +import { toast } from 'react-hot-toast'; |
| 10 | + |
| 11 | +interface Props { |
| 12 | + action: 'rename' | 'create'; |
| 13 | +} |
| 14 | + |
| 15 | +interface TunnelAliasModalRef { |
| 16 | + trigger: (callback: () => any, tunnel?: Tunnel) => void; |
| 17 | +} |
| 18 | + |
| 19 | +const TunnelAliasModal = forwardRef<TunnelAliasModalRef, Props>(({ action }, ref) => { |
| 20 | + const [isLoading, setLoading] = useState<boolean>(false); |
| 21 | + const [alias, setAlias] = useState<string>(''); |
| 22 | + |
| 23 | + const { isOpen, onOpen, onClose, onOpenChange } = useDisclosure(); |
| 24 | + |
| 25 | + const [tunnel, setTunnel] = useState<Tunnel>(); |
| 26 | + const [callback, setCallback] = useState<(() => any) | null>(null); |
| 27 | + |
| 28 | + const trigger = (callback: () => any, tunnel?: Tunnel) => { |
| 29 | + setLoading(false); |
| 30 | + setTunnel(tunnel); |
| 31 | + |
| 32 | + if (tunnel) { |
| 33 | + setAlias(tunnel.alias); |
| 34 | + } |
| 35 | + |
| 36 | + setCallback(() => callback); |
| 37 | + onOpen(); |
| 38 | + }; |
| 39 | + |
| 40 | + useImperativeHandle(ref, () => ({ |
| 41 | + trigger, |
| 42 | + })); |
| 43 | + |
| 44 | + const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 45 | + e.preventDefault(); |
| 46 | + |
| 47 | + if (callback) { |
| 48 | + setLoading(true); |
| 49 | + |
| 50 | + try { |
| 51 | + if (action === 'rename' && tunnel) { |
| 52 | + await renameTunnel(tunnel.id, alias.trim()); |
| 53 | + } else { |
| 54 | + await createTunnel(alias.trim()); |
| 55 | + } |
| 56 | + |
| 57 | + toast.success(`Tunnel ${action === 'rename' ? 'renamed' : 'created'} successfully.`); |
| 58 | + callback(); |
| 59 | + onClose(); |
| 60 | + } catch (error) { |
| 61 | + console.error('Error renaming tunnel:', error); |
| 62 | + toast.error('Error renaming tunnel.'); |
| 63 | + } finally { |
| 64 | + setLoading(false); |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const getModalTitle = () => { |
| 70 | + return action === 'create' ? 'Create Tunnel' : 'Rename Tunnel'; |
| 71 | + }; |
| 72 | + |
| 73 | + const getButtonLabel = () => { |
| 74 | + return action === 'create' ? 'Create' : 'Rename'; |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Modal isOpen={isOpen} onOpenChange={onOpenChange} size="sm" shouldBlockScroll={false}> |
| 79 | + <Form className="w-full" validationBehavior="native" onSubmit={onSubmit}> |
| 80 | + <ModalContent> |
| 81 | + <ModalHeader>{getModalTitle()}</ModalHeader> |
| 82 | + |
| 83 | + <ModalBody> |
| 84 | + <div className="col w-full gap-2"> |
| 85 | + <Label value="Alias" /> |
| 86 | + |
| 87 | + <StyledInput |
| 88 | + autoFocus |
| 89 | + value={alias} |
| 90 | + onValueChange={(value) => setAlias(value)} |
| 91 | + validate={(value) => { |
| 92 | + const trimmedValue = value?.trim(); |
| 93 | + |
| 94 | + if (!trimmedValue) { |
| 95 | + return 'Alias is required'; |
| 96 | + } |
| 97 | + |
| 98 | + if (trimmedValue.length < 3) { |
| 99 | + return 'Alias must be at least 3 characters'; |
| 100 | + } |
| 101 | + |
| 102 | + return null; |
| 103 | + }} |
| 104 | + placeholder={action === 'create' ? 'My Tunnel' : 'Enter a new alias'} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </ModalBody> |
| 108 | + |
| 109 | + <ModalFooter> |
| 110 | + <div className="flex justify-end pb-0.5"> |
| 111 | + <SubmitButton label={getButtonLabel()} isLoading={isLoading} /> |
| 112 | + </div> |
| 113 | + </ModalFooter> |
| 114 | + </ModalContent> |
| 115 | + </Form> |
| 116 | + </Modal> |
| 117 | + ); |
| 118 | +}); |
| 119 | + |
| 120 | +TunnelAliasModal.displayName = 'TunnelAliasModal'; |
| 121 | + |
| 122 | +export default TunnelAliasModal; |
0 commit comments