/** * Copyright (c) 2023-present Plane Software, Inc. and contributors * SPDX-License-Identifier: AGPL-3.0-only * See the LICENSE file for details. */ import { useMemo, useState } from "react"; import { observer } from "mobx-react"; import { useParams } from "next/navigation"; import { AutoCloseOutline, StateOutline } from "@makeplane/propel/icons"; // plane imports import { PROJECT_AUTOMATION_MONTHS, EUserPermissions, EUserPermissionsLevel, EIconSize } from "@plane/constants"; import { useTranslation } from "@plane/i18n"; import { StateGroupIcon } from "@plane/propel/icons"; import type { IProject } from "@plane/types"; import { Switch } from "@makeplane/propel/components/switch"; import { CustomSelect, CustomSearchSelect, Loader } from "@plane/ui"; import { SelectMonthModal } from "@/components/automation"; import { SettingsControlItem } from "@/components/settings/control-item"; // hooks import { useProject } from "@/hooks/store/use-project"; import { useProjectState } from "@/hooks/store/use-project-state"; import { useUserPermissions } from "@/hooks/store/user"; type Props = { handleChange: (formData: Partial) => Promise; }; export const AutoCloseAutomation = observer(function AutoCloseAutomation(props: Props) { const { handleChange } = props; // router const { workspaceSlug } = useParams(); // states const [monthModal, setmonthModal] = useState(false); // store hooks const { currentProjectDetails } = useProject(); const { projectStates } = useProjectState(); const { allowPermissions } = useUserPermissions(); const { t } = useTranslation(); // const stateGroups = projectStateStore.groupedProjectStates ?? undefined; const options = projectStates ?.filter((state) => state.group === "cancelled") .map((state) => ({ value: state.id, query: state.name, content: (
{state.name}
), })); const multipleOptions = (options ?? []).length > 1; const defaultState = projectStates?.find((s) => s.group === "cancelled")?.id || null; const selectedOption = projectStates?.find((s) => s.id === (currentProjectDetails?.default_state ?? defaultState)); const currentDefaultState = projectStates?.find((s) => s.id === defaultState); const initialValues: Partial = { close_in: 1, default_state: defaultState, }; const isAdmin = allowPermissions( [EUserPermissions.ADMIN], EUserPermissionsLevel.PROJECT, workspaceSlug?.toString(), currentProjectDetails?.id ); const autoCloseStatus = useMemo(() => { if (currentProjectDetails?.close_in === undefined) return false; return currentProjectDetails.close_in !== 0; }, [currentProjectDetails]); return ( <> setmonthModal(false)} handleChange={handleChange} />
{ if (currentProjectDetails?.close_in === 0) { void handleChange({ close_in: 1, default_state: defaultState }); } else { void handleChange({ close_in: 0, default_state: null }); } }} disabled={!isAdmin} aria-label={t("project_settings.automations.auto-close.title")} /> } />
{currentProjectDetails ? ( autoCloseStatus && (
{t("project_settings.automations.auto-close.duration")}
void handleChange({ close_in: val })} input disabled={!isAdmin} > <> {PROJECT_AUTOMATION_MONTHS.map((month) => ( {t(month.i18n_label, { months: month.value })} ))}
{t("project_settings.automations.auto-close.auto_close_status")}
{selectedOption ? ( ) : currentDefaultState ? ( ) : ( )} {selectedOption?.name ? selectedOption.name : (currentDefaultState?.name ?? {t("state")})}
} onChange={(val: string) => void handleChange({ default_state: val })} options={options} disabled={!multipleOptions} input />
) ) : ( )} ); });