|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useForm } from "react-hook-form"; |
| 4 | +import { Button, ButtonGroup, Card, CardBody, CardHeader, Input, useDisclosure } from "@heroui/react"; |
| 5 | +import React, { useState } from "react"; |
| 6 | +import slugify from "slugify"; |
| 7 | + |
| 8 | +import { StatsIcon } from "@/src/components/icons"; |
| 9 | +import { DynamicAccessKeyStats } from "@/src/core/definitions"; |
| 10 | +import DynamicAccessKeyValidityChip from "@/src/components/dynamic-access-key-validity-chip"; |
| 11 | +import DynamicAccessKeyDataUsageChip from "@/src/components/dynamic-access-key-data-usage-chip"; |
| 12 | +import NoResult from "@/src/components/no-result"; |
| 13 | +import { getDynamicAccessKeyStatsByPath } from "@/src/core/actions/dynamic-access-key"; |
| 14 | +import MessageModal from "@/src/components/modals/message-modal"; |
| 15 | + |
| 16 | +function extractPath(url: string): string | null { |
| 17 | + try { |
| 18 | + const parsedUrl = new URL(url); |
| 19 | + const path = parsedUrl.pathname.replace(/^\/+|\/+$/g, ""); |
| 20 | + const pathSegments = path.split("/").filter((segment) => segment.length > 0); |
| 21 | + |
| 22 | + const dakIndex = pathSegments.indexOf("dak"); |
| 23 | + |
| 24 | + if (dakIndex === -1 || dakIndex === pathSegments.length - 1) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + return slugify(pathSegments[dakIndex + 1]); |
| 29 | + } catch (error) { |
| 30 | + return null; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +interface FormProps { |
| 35 | + accessKey: string; |
| 36 | +} |
| 37 | + |
| 38 | +// TODO: add captcha |
| 39 | +export default function DynamicAccessKeyStatsForm() { |
| 40 | + const form = useForm<FormProps>(); |
| 41 | + const [stats, setStats] = useState<DynamicAccessKeyStats | null>(); |
| 42 | + const errorModalDisclosure = useDisclosure(); |
| 43 | + const [error, setError] = useState<string>(); |
| 44 | + |
| 45 | + const actualSubmit = async (data: FormProps) => { |
| 46 | + try { |
| 47 | + const path = extractPath(data.accessKey); |
| 48 | + |
| 49 | + if (path) { |
| 50 | + setStats(await getDynamicAccessKeyStatsByPath(path)); |
| 51 | + } else { |
| 52 | + setStats(null); |
| 53 | + } |
| 54 | + } catch (error) { |
| 55 | + setError((error as object).toString()); |
| 56 | + errorModalDisclosure.onOpen(); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const handleReset = () => { |
| 61 | + form.reset(); |
| 62 | + setStats(undefined); |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div className="grid gap-8"> |
| 67 | + <MessageModal |
| 68 | + body={ |
| 69 | + <div className="grid gap-2"> |
| 70 | + <span>Something went wrong.</span> |
| 71 | + <pre className="text-sm break-words whitespace-pre-wrap text-danger-500">{error}</pre> |
| 72 | + </div> |
| 73 | + } |
| 74 | + disclosure={errorModalDisclosure} |
| 75 | + title="Error!" |
| 76 | + /> |
| 77 | + |
| 78 | + <form |
| 79 | + className="flex flex-col items-center justify-center gap-2" |
| 80 | + onSubmit={form.handleSubmit(actualSubmit)} |
| 81 | + > |
| 82 | + <div className="mb-8 text-foreground grid gap-2 place-items-center px-4"> |
| 83 | + <StatsIcon size={86} /> |
| 84 | + <div className="text-center">Enter your access key to view your usage statistics</div> |
| 85 | + </div> |
| 86 | + |
| 87 | + <Input |
| 88 | + className="w-[320px]" |
| 89 | + color="primary" |
| 90 | + errorMessage={form.formState.errors.accessKey?.message} |
| 91 | + isInvalid={!!form.formState.errors.accessKey} |
| 92 | + label="Access Key" |
| 93 | + variant="underlined" |
| 94 | + {...form.register("accessKey", { |
| 95 | + required: true, |
| 96 | + maxLength: 512 |
| 97 | + })} |
| 98 | + /> |
| 99 | + |
| 100 | + <ButtonGroup className="w-[320px]" fullWidth={true}> |
| 101 | + <Button color="primary" isLoading={form.formState.isSubmitting} type="submit" variant="shadow"> |
| 102 | + Check |
| 103 | + </Button> |
| 104 | + {stats !== undefined && ( |
| 105 | + <Button variant="shadow" onPress={handleReset}> |
| 106 | + Reset |
| 107 | + </Button> |
| 108 | + )} |
| 109 | + </ButtonGroup> |
| 110 | + </form> |
| 111 | + |
| 112 | + {stats !== undefined && |
| 113 | + (stats ? ( |
| 114 | + <Card className="w-[320px] mx-auto"> |
| 115 | + <CardHeader> |
| 116 | + <div className="grid gap-1"> |
| 117 | + <span className="max-w-[320px] truncate">{stats.name}</span> |
| 118 | + <span className="max-w-[320px] truncate text-foreground-400 text-sm">{stats.path}</span> |
| 119 | + </div> |
| 120 | + </CardHeader> |
| 121 | + <CardBody className="text-sm grid gap-2"> |
| 122 | + <div className="flex gap-1 justify-between items-center"> |
| 123 | + <span>Data usage</span> |
| 124 | + <DynamicAccessKeyDataUsageChip item={stats} /> |
| 125 | + </div> |
| 126 | + |
| 127 | + <div className="flex gap-1 justify-between items-center"> |
| 128 | + <span>Validity</span> |
| 129 | + <DynamicAccessKeyValidityChip dak={stats} /> |
| 130 | + </div> |
| 131 | + </CardBody> |
| 132 | + </Card> |
| 133 | + ) : ( |
| 134 | + <NoResult /> |
| 135 | + ))} |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments