import { Button, Input, Modal, ModalDialog, Radio, RadioGroup } from "@mui/joy"; import { useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { userServiceClient } from "@/grpcweb"; import useLoading from "@/hooks/useLoading"; import { useUserStore } from "@/stores"; import Icon from "./Icon"; interface Props { onClose: () => void; onConfirm?: () => void; } const expirationOptions = [ { label: "8 hours", value: 3600 * 8, }, { label: "1 month", value: 3600 * 24 * 30, }, { label: "Never", value: 0, }, ]; interface State { description: string; expiration: number; } const CreateAccessTokenDialog: React.FC = (props: Props) => { const { onClose, onConfirm } = props; const { t } = useTranslation(); const currentUser = useUserStore().getCurrentUser(); const [state, setState] = useState({ description: "", expiration: 3600 * 8, }); const requestState = useLoading(false); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; const handleDescriptionInputChange = (e: React.ChangeEvent) => { setPartialState({ description: e.target.value, }); }; const handleRoleInputChange = (e: React.ChangeEvent) => { setPartialState({ expiration: Number(e.target.value), }); }; const handleSaveBtnClick = async () => { if (!state.description) { toast.error("Description is required"); return; } try { await userServiceClient.createUserAccessToken({ id: currentUser.id, description: state.description, expiresAt: state.expiration ? new Date(Date.now() + state.expiration * 1000) : undefined, }); if (onConfirm) { onConfirm(); } onClose(); } catch (error: any) { console.error(error); toast.error(error.details); } }; return (
Create Access Token
Description *
Expiration *
{expirationOptions.map((option) => ( ))}
); }; export default CreateAccessTokenDialog;