"use client"; import { useAutoAnimate } from "@formkit/auto-animate/react"; import { useMemo } from "react"; import { useForm, useFieldArray, Controller } from "react-hook-form"; import { checkAdminOrOwner } from "@calcom/features/auth/lib/checkAdminOrOwner"; import SectionBottomActions from "@calcom/features/settings/SectionBottomActions"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import type { RouterOutputs } from "@calcom/trpc/react"; import classNames from "@calcom/ui/classNames"; import { Button } from "@calcom/ui/components/button"; import { Form, Input, SettingsToggle } from "@calcom/ui/components/form"; import { showToast } from "@calcom/ui/components/toast"; import { CornerDownRightIcon, TrashIcon } from "@coss/ui/icons"; interface ProfileViewProps { team: RouterOutputs["viewer"]["teams"]["get"]; } const OTHER_FIELD_ID = -1; const InternalNotePresetsView = ({ team }: ProfileViewProps) => { const { t } = useLocale(); const utils = trpc.useUtils(); const { data: _loadedPresets } = trpc.viewer.teams.getInternalNotesPresets.useQuery({ teamId: team?.id as number, }); const loadedPresets = useMemo(() => { return (_loadedPresets ?? []).map((preset) => ({ ...preset, cancellationReason: preset.cancellationReason ?? undefined, })); }, [_loadedPresets]); const hasExistingPresets = loadedPresets.length > 0; type FormValues = { presets: { id: number; name: string; cancellationReason?: string | undefined }[]; }; const form = useForm({ values: { presets: loadedPresets, }, }); const { fields, append, remove, replace } = useFieldArray({ control: form.control, name: "presets", }); const addNewPreset = () => { append({ id: OTHER_FIELD_ID, name: "" }); }; const updatePresetsMutation = trpc.viewer.teams.updateInternalNotesPresets.useMutation({ onSuccess: () => { showToast(t("internal_note_presets_updated_successfully"), "success"); utils.viewer.teams.getInternalNotesPresets.invalidate(); }, onError: (error) => { showToast(error.message || t("something_went_wrong"), "error"); }, }); const onSubmit = async (data: FormValues) => { if (!team?.id) return; updatePresetsMutation.mutate({ teamId: team.id, presets: data.presets, }); }; const [animateRef] = useAutoAnimate(); const isAdmin = team && checkAdminOrOwner(team.membership.role); if (!isAdmin) { return (
{t("only_owner_change")}
); } return (
onSubmit(e)}> { const isChecked = hasExistingPresets || (value && value.length > 0); return ( { if (active && !value?.length) { append({ id: OTHER_FIELD_ID, name: "" }); } else { replace([]); if (!active && team?.id && hasExistingPresets) { updatePresetsMutation.mutate({ teamId: team.id, presets: [], }); } } }} switchContainerClassName={classNames( "border-subtle mt-6 rounded-lg border py-6 px-4 sm:px-6", isChecked && "rounded-b-none" )}>
{fields.map((field, index) => (
( )} />
( )} />
))}
); }} /> ); }; export default InternalNotePresetsView;