| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import dayjs from 'dayjs'
- import { useEffect, useState } from 'react'
- import { Control, ControllerRenderProps } from 'react-hook-form'
- import {
- FormControl,
- FormField,
- Input,
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
- WarningIcon,
- } from 'ui'
- import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
- import {
- CUSTOM_EXPIRY_VALUE,
- EXPIRES_AT_OPTIONS,
- NON_EXPIRING_TOKEN_VALUE,
- } from '../../AccessToken.constants'
- import { type TokenFormValues } from '../../AccessToken.schemas'
- import { DatePicker } from '@/components/ui/DatePicker'
- interface BasicInfoProps {
- control: Control<TokenFormValues>
- expirationDate: string
- onCustomDateChange?: (date: { date: string } | undefined) => void
- onCustomExpiryChange?: (isCustom: boolean) => void
- }
- export const BasicInfo = ({
- control,
- expirationDate,
- onCustomDateChange,
- onCustomExpiryChange,
- }: BasicInfoProps) => {
- const [customDate, setCustomDate] = useState<Date>()
- const [isCustomSelected, setIsCustomSelected] = useState(false)
- useEffect(() => {
- const isCustom = expirationDate === CUSTOM_EXPIRY_VALUE
- setIsCustomSelected(isCustom)
- onCustomExpiryChange?.(isCustom)
- }, [expirationDate, onCustomExpiryChange])
- const handleCustomDateChange = (date: Date | undefined) => {
- setCustomDate(date)
- if (date) {
- onCustomDateChange?.({ date: date.toISOString() })
- } else {
- onCustomDateChange?.(undefined)
- }
- }
- const handleExpiryChange = (
- value: string,
- field: ControllerRenderProps<TokenFormValues, 'expiresAt'>
- ) => {
- const isCustom = value === CUSTOM_EXPIRY_VALUE
- setIsCustomSelected(isCustom)
- onCustomExpiryChange?.(isCustom)
- field.onChange(value)
- }
- return (
- <div className="space-y-4 px-5 sm:px-6 py-6">
- <FormField
- key="tokenName"
- name="tokenName"
- control={control}
- render={({ field }) => (
- <FormItemLayout name="tokenName" label="Name">
- <FormControl>
- <Input id="tokenName" {...field} placeholder="Provide a name for your token" />
- </FormControl>
- </FormItemLayout>
- )}
- />
- <FormField
- key="expiresAt"
- name="expiresAt"
- control={control}
- render={({ field }) => (
- <FormItemLayout name="expiresAt" label="Expires in">
- <div className="flex gap-2">
- <FormControl className="grow">
- <Select
- value={field.value}
- onValueChange={(value) => handleExpiryChange(value, field)}
- >
- <SelectTrigger>
- <SelectValue placeholder="Expires at" />
- </SelectTrigger>
- <SelectContent>
- {Object.values(EXPIRES_AT_OPTIONS).map(
- (option: { value: string; label: string }) => (
- <SelectItem key={option.value} value={option.value}>
- {option.label}
- </SelectItem>
- )
- )}
- </SelectContent>
- </Select>
- </FormControl>
- {isCustomSelected && (
- <DatePicker
- selectsRange={false}
- triggerButtonSize="small"
- contentSide="top"
- minDate={new Date()}
- maxDate={dayjs().add(1, 'year').toDate()}
- onChange={(date) => {
- const selectedDate = date.to || date.from
- if (selectedDate) {
- handleCustomDateChange(new Date(selectedDate))
- } else {
- handleCustomDateChange(undefined)
- }
- }}
- >
- {customDate ? `${dayjs(customDate).format('DD MMM, HH:mm')}` : 'Select date'}
- </DatePicker>
- )}
- </div>
- {field.value === NON_EXPIRING_TOKEN_VALUE && (
- <div className="w-full flex gap-x-2 items-center mt-3 mx-0.5">
- <WarningIcon />
- <span className="text-xs text-left text-foreground-lighter">
- Make sure to keep your non-expiring token safe and secure.
- </span>
- </div>
- )}
- </FormItemLayout>
- )}
- />
- </div>
- )
- }
|