"use client"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import AuthPageSettings, { AuthPageSettingsRef } from "@app/components/private/AuthPageSettings"; import { Button } from "@app/components/ui/button"; import { useOrgContext } from "@app/hooks/useOrgContext"; import { userOrgUserContext } from "@app/hooks/useOrgUserContext"; import { toast } from "@app/hooks/useToast"; import { useState, useRef, useTransition, useActionState, type ComponentRef } from "react"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { formatAxiosError } from "@app/lib/api"; import { AxiosResponse } from "axios"; import { DeleteOrgResponse, ListUserOrgsResponse } from "@server/routers/org"; import { useRouter } from "next/navigation"; import { SettingsContainer, SettingsSection, SettingsSectionHeader, SettingsSectionTitle, SettingsSectionDescription, SettingsSectionBody, SettingsSectionForm, SettingsSectionFooter } from "@app/components/Settings"; import { useUserContext } from "@app/hooks/useUserContext"; import { useTranslations } from "next-intl"; import { build } from "@server/build"; import { SwitchInput } from "@app/components/SwitchInput"; import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert"; import { useLicenseStatusContext } from "@app/hooks/useLicenseStatusContext"; import { useSubscriptionStatusContext } from "@app/hooks/useSubscriptionStatusContext"; import { usePaidStatus } from "@app/hooks/usePaidStatus"; import type { t } from "@faker-js/faker/dist/airline-DF6RqYmq"; import type { OrgContextType } from "@app/contexts/orgContext"; // Session length options in hours const SESSION_LENGTH_OPTIONS = [ { value: null, labelKey: "unenforced" }, { value: 1, labelKey: "1Hour" }, { value: 3, labelKey: "3Hours" }, { value: 6, labelKey: "6Hours" }, { value: 12, labelKey: "12Hours" }, { value: 24, labelKey: "1DaySession" }, { value: 72, labelKey: "3Days" }, { value: 168, labelKey: "7Days" }, { value: 336, labelKey: "14Days" }, { value: 720, labelKey: "30DaysSession" }, { value: 2160, labelKey: "90DaysSession" }, { value: 4320, labelKey: "180DaysSession" } ]; // Password expiry options in days - will be translated in component const PASSWORD_EXPIRY_OPTIONS = [ { value: null, labelKey: "neverExpire" }, { value: 1, labelKey: "1Day" }, { value: 30, labelKey: "30Days" }, { value: 60, labelKey: "60Days" }, { value: 90, labelKey: "90Days" }, { value: 180, labelKey: "180Days" }, { value: 365, labelKey: "1Year" } ]; // Schema for general organization settings const GeneralFormSchema = z.object({ name: z.string(), subnet: z.string().optional(), requireTwoFactor: z.boolean().optional(), maxSessionLengthHours: z.number().nullable().optional(), passwordExpiryDays: z.number().nullable().optional(), settingsLogRetentionDaysRequest: z.number(), settingsLogRetentionDaysAccess: z.number(), settingsLogRetentionDaysAction: z.number() }); type GeneralFormValues = z.infer; const LOG_RETENTION_OPTIONS = [ { label: "logRetentionDisabled", value: 0 }, { label: "logRetention3Days", value: 3 }, { label: "logRetention7Days", value: 7 }, { label: "logRetention14Days", value: 14 }, { label: "logRetention30Days", value: 30 }, { label: "logRetention90Days", value: 90 }, ...(build != "saas" ? [ { label: "logRetentionForever", value: -1 }, { label: "logRetentionEndOfFollowingYear", value: 9001 } ] : []) ]; export default function GeneralPage() { const { org } = useOrgContext(); return ( {build !== "oss" && } {build !== "saas" && } ); } type SectionFormProps = { org: OrgContextType["org"]["org"]; }; function DeleteForm({ org }: SectionFormProps) { const t = useTranslations(); const api = createApiClient(useEnvContext()); const router = useRouter(); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [loadingDelete, startTransition] = useTransition(); const { user } = useUserContext(); async function pickNewOrgAndNavigate() { try { const res = await api.get>( `/user/${user.userId}/orgs` ); if (res.status === 200) { if (res.data.data.orgs.length > 0) { const orgId = res.data.data.orgs[0].orgId; // go to `/${orgId}/settings`); router.push(`/${orgId}/settings`); } else { // go to `/setup` router.push("/setup"); } } } catch (err) { console.error(err); toast({ variant: "destructive", title: t("orgErrorFetch"), description: formatAxiosError(err, t("orgErrorFetchMessage")) }); } } async function deleteOrg() { try { const res = await api.delete>( `/org/${org.orgId}` ); toast({ title: t("orgDeleted"), description: t("orgDeletedMessage") }); if (res.status === 200) { pickNewOrgAndNavigate(); } } catch (err) { console.error(err); toast({ variant: "destructive", title: t("orgErrorDelete"), description: formatAxiosError(err, t("orgErrorDeleteMessage")) }); } } return ( <> { setIsDeleteModalOpen(val); }} dialog={

{t("orgQuestionRemove")}

{t("orgMessageRemove")}

} buttonText={t("orgDeleteConfirm")} onConfirm={async () => startTransition(deleteOrg)} string={org.name || ""} title={t("orgDelete")} /> {t("dangerSection")} {t("dangerSectionDescription")} ); } function GeneralSectionForm({ org }: SectionFormProps) { const { updateOrg } = useOrgContext(); const form = useForm({ resolver: zodResolver( GeneralFormSchema.pick({ name: true, subnet: true }) ), defaultValues: { name: org.name, subnet: org.subnet || "" // Add default value for subnet }, mode: "onChange" }); const t = useTranslations(); const router = useRouter(); const [, formAction, loadingSave] = useActionState(performSave, null); const api = createApiClient(useEnvContext()); async function performSave() { const isValid = await form.trigger(); if (!isValid) return; const data = form.getValues(); try { const reqData = { name: data.name } as any; // Update organization await api.post(`/org/${org.orgId}`, reqData); // Update the org context to reflect the change in the info card updateOrg({ name: data.name }); toast({ title: t("orgUpdated"), description: t("orgUpdatedDescription") }); router.refresh(); } catch (e) { toast({ variant: "destructive", title: t("orgErrorUpdate"), description: formatAxiosError(e, t("orgErrorUpdateMessage")) }); } } return ( {t("general")} {t("orgGeneralSettingsDescription")}
( {t("name")} {t("orgDisplayName")} )} />
); } function LogRetentionSectionForm({ org }: SectionFormProps) { const form = useForm({ resolver: zodResolver( GeneralFormSchema.pick({ settingsLogRetentionDaysRequest: true, settingsLogRetentionDaysAccess: true, settingsLogRetentionDaysAction: true }) ), defaultValues: { settingsLogRetentionDaysRequest: org.settingsLogRetentionDaysRequest ?? 15, settingsLogRetentionDaysAccess: org.settingsLogRetentionDaysAccess ?? 15, settingsLogRetentionDaysAction: org.settingsLogRetentionDaysAction ?? 15 }, mode: "onChange" }); const router = useRouter(); const t = useTranslations(); const { isPaidUser, hasSaasSubscription } = usePaidStatus(); const [, formAction, loadingSave] = useActionState(performSave, null); const api = createApiClient(useEnvContext()); async function performSave() { const isValid = await form.trigger(); if (!isValid) return; const data = form.getValues(); try { const reqData = { settingsLogRetentionDaysRequest: data.settingsLogRetentionDaysRequest, settingsLogRetentionDaysAccess: data.settingsLogRetentionDaysAccess, settingsLogRetentionDaysAction: data.settingsLogRetentionDaysAction } as any; // Update organization await api.post(`/org/${org.orgId}`, reqData); toast({ title: t("orgUpdated"), description: t("orgUpdatedDescription") }); router.refresh(); } catch (e) { toast({ variant: "destructive", title: t("orgErrorUpdate"), description: formatAxiosError(e, t("orgErrorUpdateMessage")) }); } } return ( {t("logRetention")} {t("logRetentionDescription")}
( {t("logRetentionRequestLabel")} )} /> {build !== "oss" && ( <> { const isDisabled = !isPaidUser; return ( {t( "logRetentionAccessLabel" )} ); }} /> { const isDisabled = !isPaidUser; return ( {t( "logRetentionActionLabel" )} ); }} /> )}
); } function SecuritySettingsSectionForm({ org }: SectionFormProps) { const router = useRouter(); const form = useForm({ resolver: zodResolver( GeneralFormSchema.pick({ requireTwoFactor: true, maxSessionLengthHours: true, passwordExpiryDays: true }) ), defaultValues: { requireTwoFactor: org.requireTwoFactor || false, maxSessionLengthHours: org.maxSessionLengthHours || null, passwordExpiryDays: org.passwordExpiryDays || null }, mode: "onChange" }); const t = useTranslations(); const { isPaidUser } = usePaidStatus(); // Track initial security policy values const initialSecurityValues = { requireTwoFactor: org.requireTwoFactor || false, maxSessionLengthHours: org.maxSessionLengthHours || null, passwordExpiryDays: org.passwordExpiryDays || null }; const [isSecurityPolicyConfirmOpen, setIsSecurityPolicyConfirmOpen] = useState(false); // Check if security policies have changed const hasSecurityPolicyChanged = () => { const currentValues = form.getValues(); return ( currentValues.requireTwoFactor !== initialSecurityValues.requireTwoFactor || currentValues.maxSessionLengthHours !== initialSecurityValues.maxSessionLengthHours || currentValues.passwordExpiryDays !== initialSecurityValues.passwordExpiryDays ); }; const [, formAction, loadingSave] = useActionState(onSubmit, null); const api = createApiClient(useEnvContext()); const formRef = useRef>(null); async function onSubmit() { // Check if security policies have changed if (hasSecurityPolicyChanged()) { setIsSecurityPolicyConfirmOpen(true); return; } await performSave(); } async function performSave() { const isValid = await form.trigger(); if (!isValid) return; const data = form.getValues(); try { const reqData = { requireTwoFactor: data.requireTwoFactor || false, maxSessionLengthHours: data.maxSessionLengthHours, passwordExpiryDays: data.passwordExpiryDays } as any; // Update organization await api.post(`/org/${org.orgId}`, reqData); toast({ title: t("orgUpdated"), description: t("orgUpdatedDescription") }); router.refresh(); } catch (e) { toast({ variant: "destructive", title: t("orgErrorUpdate"), description: formatAxiosError(e, t("orgErrorUpdateMessage")) }); } } return ( <>

{t("securityPolicyChangeDescription")}

} buttonText={t("saveSettings")} onConfirm={performSave} string={t("securityPolicyChangeConfirmMessage")} title={t("securityPolicyChangeWarning")} warningText={t("securityPolicyChangeWarningText")} /> {t("securitySettings")} {t("securitySettingsDescription")}
{ const isDisabled = !isPaidUser; return (
{ if ( !isDisabled ) { form.setValue( "requireTwoFactor", val ); } }} />
{t( "requireTwoFactorDescription" )}
); }} /> { const isDisabled = !isPaidUser; return ( {t("maxSessionLength")} {t( "maxSessionLengthDescription" )} ); }} /> { const isDisabled = !isPaidUser; return ( {t("passwordExpiryDays")} {t( "editPasswordExpiryDescription" )} ); }} />
); }