forked from super-productivity/super-productivity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-shared-helpers.ts
More file actions
268 lines (237 loc) · 8.1 KB
/
task-shared-helpers.ts
File metadata and controls
268 lines (237 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { Update } from '@ngrx/entity';
import { RootState } from '../../root-state';
import { Tag } from '../../../features/tag/tag.model';
import { Project } from '../../../features/project/project.model';
import { Task } from '../../../features/tasks/task.model';
import {
PROJECT_FEATURE_NAME,
projectAdapter,
} from '../../../features/project/store/project.reducer';
import { TAG_FEATURE_NAME, tagAdapter } from '../../../features/tag/store/tag.reducer';
import {
plannerFeatureKey,
PlannerState,
} from '../../../features/planner/store/planner.reducer';
import { unique } from '../../../util/unique';
import { TODAY_TAG } from '../../../features/tag/tag.const';
// =============================================================================
// TYPES
// =============================================================================
export type ProjectTaskList = 'backlogTaskIds' | 'taskIds';
export type TaskEntity = { id: string; projectId?: string | null; tagIds?: string[] };
export type TaskWithTags = Task & { tagIds: string[] };
export type ActionHandler = (state: RootState) => RootState;
export type ActionHandlerMap = Record<string, ActionHandler>;
// =============================================================================
// STATE UPDATE HELPERS
// =============================================================================
export const updateProject = (
state: RootState,
projectId: string,
changes: Partial<Project>,
): RootState => ({
...state,
[PROJECT_FEATURE_NAME]: projectAdapter.updateOne(
{ id: projectId, changes },
state[PROJECT_FEATURE_NAME],
),
});
export const updateTags = (state: RootState, updates: Update<Tag>[]): RootState => ({
...state,
[TAG_FEATURE_NAME]: tagAdapter.updateMany(updates, state[TAG_FEATURE_NAME]),
});
// =============================================================================
// ENTITY GETTERS
// =============================================================================
/**
* Gets a tag entity from state. Throws if tag doesn't exist.
* Callers should check existence before calling if tag may not exist.
*/
export const getTag = (state: RootState, tagId: string): Tag => {
const tag = state[TAG_FEATURE_NAME].entities[tagId];
if (!tag) {
throw new Error(
`Tag ${tagId} not found in state. This may indicate an out-of-order remote operation.`,
);
}
return tag as Tag;
};
/**
* Gets a tag entity from state, or undefined if it doesn't exist.
* Use this when the tag may not exist (e.g., during remote sync).
*/
export const getTagOrUndefined = (state: RootState, tagId: string): Tag | undefined =>
state[TAG_FEATURE_NAME].entities[tagId] as Tag | undefined;
/**
* Gets a project entity from state. Throws if project doesn't exist.
* Callers should check existence before calling if project may not exist.
*/
export const getProject = (state: RootState, projectId: string): Project => {
const project = state[PROJECT_FEATURE_NAME].entities[projectId];
if (!project) {
throw new Error(
`Project ${projectId} not found in state. This may indicate an out-of-order remote operation.`,
);
}
return project as Project;
};
/**
* Gets a project entity from state, or undefined if it doesn't exist.
* Use this when the project may not exist (e.g., during remote sync).
*/
export const getProjectOrUndefined = (
state: RootState,
projectId: string,
): Project | undefined =>
state[PROJECT_FEATURE_NAME].entities[projectId] as Project | undefined;
// =============================================================================
// LIST MANIPULATION HELPERS
// =============================================================================
export const addTaskToList = (
taskIds: string[],
taskId: string,
isAddToBottom: boolean,
): string[] => {
if (taskIds.includes(taskId)) return taskIds;
return isAddToBottom ? [...taskIds, taskId] : [taskId, ...taskIds];
};
export const removeTasksFromList = (taskIds: string[], toRemove: string[]): string[] => {
// Use Set for O(1) lookup instead of O(n) Array.includes
// This changes overall complexity from O(n*m) to O(n+m)
const removeSet = new Set(toRemove);
return taskIds.filter((id) => !removeSet.has(id));
};
// =============================================================================
// PLANNER DAY HELPERS
// =============================================================================
/**
* Removes a single task from all planner days.
* @param state Root state
* @param taskId Task ID to remove
* @returns Updated state, or original state if no changes
*/
export const removeTaskFromPlannerDays = (
state: RootState,
taskId: string,
): RootState => {
if (!state.planner?.days) {
return state;
}
const plannerDaysCopy = { ...state.planner.days };
let hasChanges = false;
Object.keys(plannerDaysCopy).forEach((day) => {
const filtered = plannerDaysCopy[day].filter((id) => id !== taskId);
if (filtered.length !== plannerDaysCopy[day].length) {
plannerDaysCopy[day] = filtered;
hasChanges = true;
}
});
if (!hasChanges) {
return state;
}
return {
...state,
[plannerFeatureKey]: {
...state.planner,
days: plannerDaysCopy,
},
};
};
/**
* Removes multiple tasks from all planner days.
* @param state Root state
* @param taskIds Task IDs to remove
* @returns Updated state, or original state if no changes
*/
export const removeTasksFromPlannerDays = (
state: RootState,
taskIds: string[],
): RootState => {
if (!state.planner?.days || taskIds.length === 0) {
return state;
}
const taskIdSet = new Set(taskIds);
const plannerDaysCopy = { ...state.planner.days };
let hasChanges = false;
Object.keys(plannerDaysCopy).forEach((day) => {
const filtered = plannerDaysCopy[day].filter((id) => !taskIdSet.has(id));
if (filtered.length !== plannerDaysCopy[day].length) {
plannerDaysCopy[day] = filtered;
hasChanges = true;
}
});
if (!hasChanges) {
return state;
}
return {
...state,
[plannerFeatureKey]: {
...state.planner,
days: plannerDaysCopy,
},
};
};
/**
* Adds a task to a specific planner day.
* Removes the task from all other days first (task can only be in one day).
* @param state Root state
* @param taskId Task ID to add
* @param day Target day (date string)
* @param position Optional position index (default: top of list)
* @returns Updated state
*/
export const addTaskToPlannerDay = (
state: RootState,
taskId: string,
day: string,
position: number = 0,
): RootState => {
const plannerState = state[
plannerFeatureKey as keyof RootState
] as unknown as PlannerState;
const daysCopy = { ...(plannerState?.days || {}) };
// First remove from all days
Object.keys(daysCopy).forEach((d) => {
if (daysCopy[d].includes(taskId)) {
daysCopy[d] = daysCopy[d].filter((id: string) => id !== taskId);
}
});
// Add to target day at position
const targetDays = daysCopy[day] || [];
daysCopy[day] = unique([
...targetDays.slice(0, position),
taskId,
...targetDays.slice(position),
]);
return {
...state,
[plannerFeatureKey]: {
...plannerState,
days: daysCopy,
},
};
};
// =============================================================================
// TODAY_TAG HELPERS
// =============================================================================
/**
* Removes TODAY_TAG from a task's tagIds if present.
*
* TODAY_TAG is a virtual tag where membership is determined by task.dueDay,
* not by task.tagIds. This helper cleans up legacy data and ensures the
* invariant that TODAY_TAG should NEVER be in task.tagIds.
*
* See: docs/ai/today-tag-architecture.md
*
* @param tagIds Current task tagIds
* @returns Updated tagIds with TODAY_TAG removed, or original if not present
*/
export const filterOutTodayTag = (tagIds: string[]): string[] =>
tagIds.filter((id) => id !== TODAY_TAG.id);
/**
* Checks if a task has TODAY_TAG in its tagIds (legacy/incorrect data).
* @param tagIds Current task tagIds
* @returns true if TODAY_TAG is incorrectly present
*/
export const hasInvalidTodayTag = (tagIds: string[]): boolean =>
tagIds.includes(TODAY_TAG.id);