|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { Button, Modal } from 'antd'; |
| 5 | +import { CloseOutlined } from '@ant-design/icons'; |
| 6 | +import { useSearchParams, useRouter } from 'next/navigation'; |
| 7 | + |
| 8 | +import { InviteErrorCodes } from '@/types/virtual-lab/invites'; |
| 9 | +import { generateLabUrl, generateVlProjectUrl } from '@/util/virtual-lab/urls'; |
| 10 | + |
| 11 | +const getInviteErrorMessage = (code?: string): { title: string; message: string } => { |
| 12 | + try { |
| 13 | + const errorCode = Number(code); |
| 14 | + switch (errorCode) { |
| 15 | + case InviteErrorCodes.UNAUTHORIZED: |
| 16 | + return { |
| 17 | + title: 'Unauthorized access', |
| 18 | + message: |
| 19 | + 'You need to be signed in to accept this invitation. Please log in and try again.', |
| 20 | + }; |
| 21 | + case InviteErrorCodes.DATA_CONFLICT: |
| 22 | + return { |
| 23 | + title: 'Data conflict error', |
| 24 | + message: |
| 25 | + 'The invitation details do not match your account information. Please check with the sender or try again with the correct account.', |
| 26 | + }; |
| 27 | + case InviteErrorCodes.TOKEN_EXPIRED: |
| 28 | + return { |
| 29 | + title: 'Invitation expired', |
| 30 | + message: 'This invitation link has expired. Please request a new invite from the sender.', |
| 31 | + }; |
| 32 | + case InviteErrorCodes.INVALID_LINK: |
| 33 | + return { |
| 34 | + title: 'Invalid invitation link', |
| 35 | + message: |
| 36 | + 'The invitation link provided is invalid. Ensure you are using the correct link or request a new one.', |
| 37 | + }; |
| 38 | + case InviteErrorCodes.INVITE_ALREADY_ACCEPTED: |
| 39 | + return { |
| 40 | + title: 'Invitation already accepted', |
| 41 | + message: |
| 42 | + 'This invitation has already been accepted. You can proceed to sign in and access your environment.', |
| 43 | + }; |
| 44 | + case InviteErrorCodes.UNKNOWN: |
| 45 | + default: |
| 46 | + return { |
| 47 | + title: 'Unexpected error', |
| 48 | + message: |
| 49 | + 'We were unable to process your invitation due to an unexpected error. Please confirm that you are logged in and try again or contact support for assistance.', |
| 50 | + }; |
| 51 | + } |
| 52 | + } catch { |
| 53 | + return { |
| 54 | + title: 'Invitation Processing Error', |
| 55 | + message: |
| 56 | + 'An error occurred while processing your invitation. Please check the link or contact support for help.', |
| 57 | + }; |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +export default function AcceptInviteErrorDialog({ errorCode }: { errorCode: string }) { |
| 62 | + // This is needed to prevent hydration errors. |
| 63 | + // The Dialog is not rendered correctly on server side, so we need to prevent it from rendering until the client side hydration is complete (and `useEffect` is run). |
| 64 | + // https://github.com/vercel/next.js/discussions/35773 |
| 65 | + const [open, setOpen] = useState(false); |
| 66 | + const isInviteAcceptedError = errorCode === `${InviteErrorCodes.INVITE_ALREADY_ACCEPTED}`; |
| 67 | + useEffect(() => { |
| 68 | + setOpen(true); |
| 69 | + }, []); |
| 70 | + |
| 71 | + const { title, message } = getInviteErrorMessage(errorCode); |
| 72 | + const onClose = () => setOpen(false); |
| 73 | + |
| 74 | + return ( |
| 75 | + <Modal centered open={open} closeIcon={null} footer={null}> |
| 76 | + <div className="flex flex-col"> |
| 77 | + <div className="flex items-center justify-between border-b border-gray-200 pb-3"> |
| 78 | + <h2 className="text-2xl font-bold text-primary-8">{title}</h2> |
| 79 | + <button |
| 80 | + type="button" |
| 81 | + aria-label="close" |
| 82 | + onClick={onClose} |
| 83 | + className="p-2 hover:bg-gray-100" |
| 84 | + > |
| 85 | + <CloseOutlined className="h-5 w-5 text-gray-500" /> |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + <p className="mt-4 text-lg text-gray-700">{message}</p> |
| 89 | + <div className="ml-auto mt-5 justify-end"> |
| 90 | + {isInviteAcceptedError && <InviteRedirectButton />} |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </Modal> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +function InviteRedirectButton() { |
| 98 | + const searchParams = useSearchParams(); |
| 99 | + const origin = searchParams.get('origin'); |
| 100 | + const labId = searchParams.get('lab_id'); |
| 101 | + const projectId = searchParams.get('project_id'); |
| 102 | + const { push } = useRouter(); |
| 103 | + |
| 104 | + if (origin === 'Lab' && !!labId) { |
| 105 | + return ( |
| 106 | + <Button |
| 107 | + htmlType="button" |
| 108 | + type="primary" |
| 109 | + size="large" |
| 110 | + onClick={() => push(`${generateLabUrl(labId)}/overview`)} |
| 111 | + className="rounded-none bg-primary-8 text-white" |
| 112 | + > |
| 113 | + Go to virtual lab |
| 114 | + </Button> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + if (origin === 'Project' && !!labId && !!projectId) { |
| 119 | + return ( |
| 120 | + <Button |
| 121 | + htmlType="button" |
| 122 | + type="primary" |
| 123 | + size="large" |
| 124 | + onClick={() => push(`${generateVlProjectUrl(labId, projectId)}/home`)} |
| 125 | + className="rounded-none bg-primary-8 text-white" |
| 126 | + > |
| 127 | + Go to project |
| 128 | + </Button> |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + return null; |
| 133 | +} |
0 commit comments