2024-01-13 00:20:06 -06:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import TextInput from "@/components/TextInput";
|
2024-01-24 14:48:40 -06:00
|
|
|
import { TokenExpiry } from "@/types/global";
|
2024-01-13 00:20:06 -06:00
|
|
|
import toast from "react-hot-toast";
|
|
|
|
import Modal from "../Modal";
|
2024-01-24 15:19:24 -06:00
|
|
|
import { dropdownTriggerer } from "@/lib/client/utils";
|
2024-05-24 16:12:47 -05:00
|
|
|
import Button from "../ui/Button";
|
2024-06-09 08:27:16 -05:00
|
|
|
import { useTranslation } from "next-i18next";
|
2024-08-01 16:42:57 -05:00
|
|
|
import { useAddToken } from "@/hooks/store/tokens";
|
2024-08-18 12:49:33 -05:00
|
|
|
import CopyButton from "../CopyButton";
|
2024-01-13 00:20:06 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
};
|
|
|
|
|
2024-01-24 14:48:40 -06:00
|
|
|
export default function NewTokenModal({ onClose }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
2024-01-24 11:51:16 -06:00
|
|
|
const [newToken, setNewToken] = useState("");
|
2024-08-01 16:42:57 -05:00
|
|
|
const addToken = useAddToken();
|
2024-01-24 14:48:40 -06:00
|
|
|
|
2024-01-13 00:20:06 -06:00
|
|
|
const initial = {
|
|
|
|
name: "",
|
2024-01-24 14:48:40 -06:00
|
|
|
expires: 0 as TokenExpiry,
|
2024-01-13 00:20:06 -06:00
|
|
|
};
|
|
|
|
|
2024-01-24 14:48:40 -06:00
|
|
|
const [token, setToken] = useState(initial as any);
|
2024-01-13 00:20:06 -06:00
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
if (!submitLoader) {
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2024-08-14 14:22:28 -05:00
|
|
|
const load = toast.loading(t("creating_token"));
|
|
|
|
|
2024-08-01 16:42:57 -05:00
|
|
|
await addToken.mutateAsync(token, {
|
2024-08-14 14:22:28 -05:00
|
|
|
onSettled: (data, error) => {
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
toast.error(error.message);
|
|
|
|
} else {
|
|
|
|
setNewToken(data.secretKey);
|
|
|
|
}
|
2024-08-01 16:42:57 -05:00
|
|
|
},
|
|
|
|
});
|
2024-01-13 00:20:06 -06:00
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-09 08:27:16 -05:00
|
|
|
const getLabel = (expiry: TokenExpiry) => {
|
|
|
|
switch (expiry) {
|
|
|
|
case TokenExpiry.sevenDays:
|
|
|
|
return t("7_days");
|
|
|
|
case TokenExpiry.oneMonth:
|
|
|
|
return t("30_days");
|
|
|
|
case TokenExpiry.twoMonths:
|
|
|
|
return t("60_days");
|
|
|
|
case TokenExpiry.threeMonths:
|
|
|
|
return t("90_days");
|
|
|
|
case TokenExpiry.never:
|
|
|
|
return t("no_expiration");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-13 00:20:06 -06:00
|
|
|
return (
|
|
|
|
<Modal toggleModal={onClose}>
|
2024-01-24 11:51:16 -06:00
|
|
|
{newToken ? (
|
|
|
|
<div className="flex flex-col justify-center space-y-4">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin">{t("access_token_created")}</p>
|
|
|
|
<p>{t("token_creation_notice")}</p>
|
2024-08-18 12:49:33 -05:00
|
|
|
<div className="relative">
|
|
|
|
<div className="w-full hide-scrollbar overflow-x-auto whitespace-nowrap rounded-md p-2 bg-base-200 border-neutral-content border-solid border flex items-center gap-2 justify-between pr-14">
|
|
|
|
{newToken}
|
|
|
|
<div className="absolute right-0 px-2 border-neutral-content border-solid border-r bg-base-200">
|
|
|
|
<CopyButton text={newToken} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-01-13 00:20:06 -06:00
|
|
|
</div>
|
2024-01-24 11:51:16 -06:00
|
|
|
) : (
|
|
|
|
<>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin">{t("create_access_token")}</p>
|
2024-01-24 11:51:16 -06:00
|
|
|
|
|
|
|
<div className="divider mb-3 mt-1"></div>
|
|
|
|
|
2024-02-05 01:42:54 -06:00
|
|
|
<div className="flex sm:flex-row flex-col gap-2 items-center">
|
2024-01-24 11:51:16 -06:00
|
|
|
<div className="w-full">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("name")}</p>
|
2024-01-24 11:51:16 -06:00
|
|
|
|
|
|
|
<TextInput
|
2024-01-24 14:48:40 -06:00
|
|
|
value={token.name}
|
|
|
|
onChange={(e) => setToken({ ...token, name: e.target.value })}
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("token_name_placeholder")}
|
2024-01-24 11:51:16 -06:00
|
|
|
className="bg-base-200"
|
|
|
|
/>
|
|
|
|
</div>
|
2024-01-13 00:20:06 -06:00
|
|
|
|
2024-02-05 01:42:54 -06:00
|
|
|
<div className="w-full sm:w-fit">
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("expires_in")}</p>
|
2024-01-13 00:20:06 -06:00
|
|
|
|
2024-02-05 01:42:54 -06:00
|
|
|
<div className="dropdown dropdown-bottom dropdown-end w-full">
|
2024-05-24 16:12:47 -05:00
|
|
|
<Button
|
2024-01-13 00:20:06 -06:00
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
2024-05-24 16:12:47 -05:00
|
|
|
intent="secondary"
|
2024-01-24 15:19:24 -06:00
|
|
|
onMouseDown={dropdownTriggerer}
|
2024-05-24 16:12:47 -05:00
|
|
|
className="whitespace-nowrap w-32"
|
2024-01-13 00:20:06 -06:00
|
|
|
>
|
2024-06-09 08:27:16 -05:00
|
|
|
{getLabel(token.expires)}
|
2024-05-24 16:12:47 -05:00
|
|
|
</Button>
|
2024-08-14 18:13:19 -05:00
|
|
|
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-xl mt-1">
|
2024-01-24 11:51:16 -06:00
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="sort-radio"
|
|
|
|
className="radio checked:bg-primary"
|
2024-01-24 14:48:40 -06:00
|
|
|
checked={token.expires === TokenExpiry.sevenDays}
|
2024-01-24 11:51:16 -06:00
|
|
|
onChange={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-01-24 14:48:40 -06:00
|
|
|
setToken({
|
|
|
|
...token,
|
|
|
|
expires: TokenExpiry.sevenDays,
|
|
|
|
});
|
2024-01-24 11:51:16 -06:00
|
|
|
}}
|
|
|
|
/>
|
2024-08-14 18:13:19 -05:00
|
|
|
<span className="label-text whitespace-nowrap">
|
|
|
|
{t("7_days")}
|
|
|
|
</span>
|
2024-01-24 11:51:16 -06:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="sort-radio"
|
|
|
|
className="radio checked:bg-primary"
|
2024-01-24 14:48:40 -06:00
|
|
|
checked={token.expires === TokenExpiry.oneMonth}
|
2024-01-24 11:51:16 -06:00
|
|
|
onChange={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-01-24 14:48:40 -06:00
|
|
|
setToken({ ...token, expires: TokenExpiry.oneMonth });
|
2024-01-24 11:51:16 -06:00
|
|
|
}}
|
|
|
|
/>
|
2024-08-14 18:13:19 -05:00
|
|
|
<span className="label-text whitespace-nowrap">
|
|
|
|
{t("30_days")}
|
|
|
|
</span>
|
2024-01-24 11:51:16 -06:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="sort-radio"
|
|
|
|
className="radio checked:bg-primary"
|
2024-01-24 14:48:40 -06:00
|
|
|
checked={token.expires === TokenExpiry.twoMonths}
|
2024-01-24 11:51:16 -06:00
|
|
|
onChange={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-01-24 14:48:40 -06:00
|
|
|
setToken({
|
|
|
|
...token,
|
|
|
|
expires: TokenExpiry.twoMonths,
|
|
|
|
});
|
2024-01-24 11:51:16 -06:00
|
|
|
}}
|
|
|
|
/>
|
2024-08-14 18:13:19 -05:00
|
|
|
<span className="label-text whitespace-nowrap">
|
|
|
|
{t("60_days")}
|
|
|
|
</span>
|
2024-01-24 11:51:16 -06:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="sort-radio"
|
|
|
|
className="radio checked:bg-primary"
|
2024-01-24 14:48:40 -06:00
|
|
|
checked={token.expires === TokenExpiry.threeMonths}
|
2024-01-24 11:51:16 -06:00
|
|
|
onChange={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-01-24 14:48:40 -06:00
|
|
|
setToken({
|
|
|
|
...token,
|
|
|
|
expires: TokenExpiry.threeMonths,
|
|
|
|
});
|
2024-01-24 11:51:16 -06:00
|
|
|
}}
|
|
|
|
/>
|
2024-08-14 18:13:19 -05:00
|
|
|
<span className="label-text whitespace-nowrap">
|
|
|
|
{t("90_days")}
|
|
|
|
</span>
|
2024-01-24 11:51:16 -06:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<label
|
|
|
|
className="label cursor-pointer flex justify-start"
|
|
|
|
tabIndex={0}
|
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="sort-radio"
|
|
|
|
className="radio checked:bg-primary"
|
2024-01-24 14:48:40 -06:00
|
|
|
checked={token.expires === TokenExpiry.never}
|
2024-01-24 11:51:16 -06:00
|
|
|
onChange={() => {
|
|
|
|
(document?.activeElement as HTMLElement)?.blur();
|
2024-01-24 14:48:40 -06:00
|
|
|
setToken({ ...token, expires: TokenExpiry.never });
|
2024-01-24 11:51:16 -06:00
|
|
|
}}
|
|
|
|
/>
|
2024-08-14 18:13:19 -05:00
|
|
|
<span className="label-text whitespace-nowrap">
|
|
|
|
{t("no_expiration")}
|
|
|
|
</span>
|
2024-01-24 11:51:16 -06:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-01-13 00:20:06 -06:00
|
|
|
</div>
|
2024-01-24 11:51:16 -06:00
|
|
|
|
2024-02-05 01:42:54 -06:00
|
|
|
<div className="flex justify-end items-center mt-5">
|
2024-01-24 11:51:16 -06:00
|
|
|
<button
|
|
|
|
className="btn btn-accent dark:border-violet-400 text-white"
|
|
|
|
onClick={submit}
|
|
|
|
>
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("create_token")}
|
2024-01-24 11:51:16 -06:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2024-01-13 00:20:06 -06:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|