From e8d8b965c723e0503ee7f448151393aff2a31964 Mon Sep 17 00:00:00 2001 From: Civolilah Date: Wed, 2 Dec 2026 18:00:09 +0100 Subject: [PATCH] Added default task description setting --- src/common/interfaces/company.interface.ts | 1 + .../settings/task-settings/TaskSettings.tsx | 26 +++++- .../common/components/CreateTaskModal.tsx | 1 + .../tasks/common/components/TaskDetails.tsx | 30 ++++--- .../common/helpers/sanitize-description.ts | 20 +++++ .../common/hooks/useDefaultTaskDescription.ts | 82 +++++++++++++++++++ src/pages/tasks/create/Create.tsx | 16 +++- 7 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 src/pages/tasks/common/helpers/sanitize-description.ts create mode 100644 src/pages/tasks/common/hooks/useDefaultTaskDescription.ts diff --git a/src/common/interfaces/company.interface.ts b/src/common/interfaces/company.interface.ts index 32120bedd3..dd7af864aa 100644 --- a/src/common/interfaces/company.interface.ts +++ b/src/common/interfaces/company.interface.ts @@ -137,6 +137,7 @@ export interface Settings { custom_value3: string; custom_value4: string; default_task_rate: number; + default_task_description: string; payment_terms: string; send_reminders: boolean; custom_message_dashboard: string; diff --git a/src/pages/settings/task-settings/TaskSettings.tsx b/src/pages/settings/task-settings/TaskSettings.tsx index 065f69a6f7..88fa6a03f6 100644 --- a/src/pages/settings/task-settings/TaskSettings.tsx +++ b/src/pages/settings/task-settings/TaskSettings.tsx @@ -25,7 +25,7 @@ import { NumberInputField } from '$app/components/forms/NumberInputField'; import { PropertyCheckbox } from '$app/components/PropertyCheckbox'; import { SettingsLabel } from '$app/components/SettingsLabel'; import { Card, Element } from '../../../components/cards'; -import { SelectField } from '../../../components/forms'; +import { InputField, SelectField } from '../../../components/forms'; import Toggle from '../../../components/forms/Toggle'; import { Settings } from '../../../components/layouts/Settings'; import { TaskStatuses } from '..'; @@ -125,6 +125,30 @@ export function TaskSettings() { /> + + } + /> + } + > + + handleSettingsChange('settings.default_task_description', value) + } + disabled={disableSettingsField('default_task_description')} + errorMessage={errors?.errors['settings.default_task_description']} + /> + + {isCompanySettingsActive && ( )} {task && } diff --git a/src/pages/tasks/common/components/TaskDetails.tsx b/src/pages/tasks/common/components/TaskDetails.tsx index 1e9fe6a1c6..89e9c054b1 100644 --- a/src/pages/tasks/common/components/TaskDetails.tsx +++ b/src/pages/tasks/common/components/TaskDetails.tsx @@ -11,16 +11,17 @@ import classNames from 'classnames'; import dayjs from 'dayjs'; import duration from 'dayjs/plugin/duration'; +import { useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { MdLaunch } from 'react-icons/md'; import { useMediaQuery } from 'react-responsive'; import { useColorScheme } from '$app/common/colors'; -import { sanitizeHTML } from '$app/common/helpers/html-string'; import { route } from '$app/common/helpers/route'; import { useHasPermission } from '$app/common/hooks/permissions/useHasPermission'; import { useCurrentCompany } from '$app/common/hooks/useCurrentCompany'; import { useDisableNavigation } from '$app/common/hooks/useDisableNavigation'; import { useEntityAssigned } from '$app/common/hooks/useEntityAssigned'; +import { Client } from '$app/common/interfaces/client'; import { TAG_ENTITY_TYPES } from '$app/common/interfaces/tag'; import { Task } from '$app/common/interfaces/task'; import { TaskStatus } from '$app/common/interfaces/task-status'; @@ -41,7 +42,9 @@ import { ClientActionButtons } from '$app/pages/invoices/common/components/Clien import { formatTime, TaskClock } from '../../kanban/components/TaskClock'; import { isTaskRunning } from '../helpers/calculate-entity-state'; import { calculateTime } from '../helpers/calculate-time'; +import { sanitizeTaskDescription } from '../helpers/sanitize-description'; import { shouldShowStartTaskButton } from '../helpers/task'; +import { useDefaultTaskDescription } from '../hooks/useDefaultTaskDescription'; import { useResolveTaskRate } from '../hooks/useResolveTaskRate'; import { useStart } from '../hooks/useStart'; import { useStop } from '../hooks/useStop'; @@ -56,6 +59,7 @@ interface Props { errors: ValidationBag | undefined; taskModal?: boolean; page?: 'create' | 'edit'; + withDefaultDescription?: boolean; } export function TaskDetails(props: Props) { @@ -75,6 +79,18 @@ export function TaskDetails(props: Props) { const start = useStart(); const resolveTaskRate = useResolveTaskRate(); + const descriptionRef = useRef(null); + + const [selectedClient, setSelectedClient] = useState(); + + useDefaultTaskDescription({ + task, + selectedClient, + enabled: Boolean(props.withDefaultDescription), + inputRef: descriptionRef, + onChange: (description) => handleChange('description', description), + }); + const calculation = calculateTime(task.time_log, { inSeconds: true, calculateLastTimeLog: false, @@ -221,6 +237,7 @@ export function TaskDetails(props: Props) { inputLabel={t('client')} value={task.client_id} onChange={(client) => { + setSelectedClient(client); handleChange('client_id', client.id); if (!task.id) { @@ -389,19 +406,12 @@ export function TaskDetails(props: Props) {
- handleChange( - 'description', - sanitizeHTML(value, { - ALLOWED_TAGS: [], - ALLOWED_ATTR: [], - KEEP_CONTENT: true, - USE_PROFILES: { html: false }, - }) - ) + handleChange('description', sanitizeTaskDescription(value)) } errorMessage={errors?.errors.description} width="100%" diff --git a/src/pages/tasks/common/helpers/sanitize-description.ts b/src/pages/tasks/common/helpers/sanitize-description.ts new file mode 100644 index 0000000000..b43b50cdda --- /dev/null +++ b/src/pages/tasks/common/helpers/sanitize-description.ts @@ -0,0 +1,20 @@ +/** + * Invoice Ninja (https://invoiceninja.com). + * + * @link https://github.com/invoiceninja/invoiceninja source repository + * + * @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com) + * + * @license https://www.elastic.co/licensing/elastic-license + */ + +import { sanitizeHTML } from '$app/common/helpers/html-string'; + +export const sanitizeTaskDescription = (value: string) => { + return sanitizeHTML(value.replace(/\r\n?/g, '\n'), { + ALLOWED_TAGS: [], + ALLOWED_ATTR: [], + KEEP_CONTENT: true, + USE_PROFILES: { html: false }, + }); +}; diff --git a/src/pages/tasks/common/hooks/useDefaultTaskDescription.ts b/src/pages/tasks/common/hooks/useDefaultTaskDescription.ts new file mode 100644 index 0000000000..f5aae92584 --- /dev/null +++ b/src/pages/tasks/common/hooks/useDefaultTaskDescription.ts @@ -0,0 +1,82 @@ +/** + * Invoice Ninja (https://invoiceninja.com). + * + * @link https://github.com/invoiceninja/invoiceninja source repository + * + * @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com) + * + * @license https://www.elastic.co/licensing/elastic-license + */ + +import { RefObject, useEffect, useRef } from 'react'; +import { useHasPermission } from '$app/common/hooks/permissions/useHasPermission'; +import { useCurrentCompany } from '$app/common/hooks/useCurrentCompany'; +import { Client } from '$app/common/interfaces/client'; +import { Task } from '$app/common/interfaces/task'; +import { useClientQuery } from '$app/common/queries/clients'; +import { useGroupSettingsQuery } from '$app/common/queries/group-settings'; +import { sanitizeTaskDescription } from '../helpers/sanitize-description'; + +interface Params { + task: Task; + selectedClient: Client | undefined; + enabled: boolean; + inputRef: RefObject; + onChange: (description: string) => void; +} + +export function useDefaultTaskDescription(params: Params) { + const { task, selectedClient, enabled, inputRef, onChange } = params; + + const company = useCurrentCompany(); + const hasPermission = useHasPermission(); + + const previousDefault = useRef(task.description); + + const { data: groupSettings } = useGroupSettingsQuery({ perPage: 1000 }); + + const isClientSelected = + Boolean(task.client_id) && selectedClient?.id === task.client_id; + + const { data: fetchedClient } = useClientQuery({ + id: task.client_id, + enabled: + enabled && + Boolean(task.client_id) && + !isClientSelected && + (hasPermission('view_client') || hasPermission('edit_client')), + }); + + const client = isClientSelected ? selectedClient : fetchedClient; + + useEffect(() => { + if (!enabled) { + previousDefault.current = null; + return; + } + + const currentClient = client?.id === task.client_id ? client : undefined; + + const group = + currentClient?.group_settings ?? + groupSettings?.find(({ id }) => id === currentClient?.group_settings_id); + + const description = sanitizeTaskDescription( + [ + currentClient?.settings?.default_task_description, + group?.settings?.default_task_description, + company?.settings?.default_task_description, + ].find((value) => Boolean(value)) ?? '' + ); + + if ( + task.description === previousDefault.current && + task.description === inputRef.current?.value && + task.description !== description + ) { + onChange(description); + } + + previousDefault.current = description; + }, [enabled, task.client_id, client, groupSettings]); +} diff --git a/src/pages/tasks/create/Create.tsx b/src/pages/tasks/create/Create.tsx index 14feb7c036..f91b14f679 100644 --- a/src/pages/tasks/create/Create.tsx +++ b/src/pages/tasks/create/Create.tsx @@ -46,6 +46,7 @@ export default function Create() { const [isFormBusy, setIsFormBusy] = useState(false); const [isInitialConfiguration, setIsInitialConfiguration] = useState(true); + const [isTaskReady, setIsTaskReady] = useState(false); const { data: taskStatuses } = useTaskStatusesQuery({ status: 'active' }); const { data } = useBlankTaskQuery({ enabled: typeof task === 'undefined' }); @@ -89,6 +90,8 @@ export default function Create() { return value; }); + + setIsTaskReady(true); }, [data]); useEffect(() => { @@ -150,10 +153,17 @@ export default function Create() { onSaveClick={() => task && handleSave(task)} breadcrumbs={pages} > - {task && ( - + {task && isTaskReady && ( + + )} + {task && isTaskReady && ( + )} - {task && } ); }