Files
Tasks/src/js/timeline.js
T
2026-08-01 02:11:12 +08:00

64 lines
2.4 KiB
JavaScript

import { t } from './i18n/index.js';
import { getTask, updateTask } from './store.js';
import { formatLocalDate, generateUniqueId } from './utils.js';
export function addSystemEntries(task, changes) {
const timeline = [...task.timeline];
const fields = [
['progressNotes', 'progressNotesUpdated', 'progressNotesUpdated', (a, b) => ({ value: b })],
['status', 'status-change', 'statusChanged', (a, b) => ({ from: a, to: b })],
['progress', 'progress-update', 'progressUpdated', (a, b) => ({ from: a, to: b })],
['assignee', 'status-change', 'assigneeChanged', (a, b) => ({ from: a, to: b })],
['dueDate', 'status-change', 'dueDateChanged', (a, b) => ({ from: a, to: b })],
['priority', 'status-change', 'priorityChanged', (a, b) => ({ from: a, to: b })],
];
const used = new Set(timeline.map((item) => item.id));
fields.forEach(([field, type, key, values]) => {
if (Object.hasOwn(changes, field) && task[field] !== changes[field])
timeline.push({
id: generateUniqueId(used),
date: formatLocalDate(),
type,
content: '',
event: { key, values: values(task[field], changes[field]) },
userType: 'system',
});
});
return timeline;
}
export function addTimelineEntry(taskId, content) {
const task = getTask(taskId);
if (!task || !content.trim()) return false;
const id = generateUniqueId(new Set(task.timeline.map((item) => item.id)));
task.timeline.push({
id,
date: formatLocalDate(),
type: 'manual-entry',
content: content.trim(),
user: task.assignee || t('defaultUser'),
});
updateTask(taskId, { timeline: task.timeline });
return true;
}
export function updateTimelineEntry(taskId, timelineId, content) {
const task = getTask(taskId);
const item = task?.timeline.find((entry) => entry.id === timelineId);
if (!item || !content.trim()) return false;
const wasSystem = item.userType === 'system';
item.content = content.trim();
item.type = 'manual-entry';
if (wasSystem || !item.user) item.user = task.assignee || t('defaultUser');
delete item.event;
delete item.userType;
updateTask(taskId, { timeline: task.timeline });
return true;
}
export function deleteTimelineEntry(taskId, timelineId) {
const task = getTask(taskId);
if (!task) return false;
const timeline = task.timeline.filter((item) => item.id !== timelineId);
if (timeline.length === task.timeline.length) return false;
updateTask(taskId, { timeline });
return true;
}