-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderActivitySummary.ts
More file actions
55 lines (49 loc) · 1.5 KB
/
Copy pathrenderActivitySummary.ts
File metadata and controls
55 lines (49 loc) · 1.5 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
interface MovedChanges {
fromBoardColumnId: string
toBoardColumnId: string
}
interface Activity {
action: 'created' | 'updated' | 'moved'
changes: unknown
}
const isObject = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null
const renderUpdatedSummary = (changes: unknown): string => {
if (!isObject(changes)) {
return 'updated this task'
}
const fieldNames = Object.keys(changes)
if (fieldNames.length === 0) {
return 'updated this task'
}
return `changed ${fieldNames.join(', ')}`
}
const renderMovedSummary = (changes: unknown, columnTitlesById?: Map<string, string>): string => {
if (!isObject(changes)) {
return 'moved this task'
}
if (!columnTitlesById) {
return 'moved this task between columns'
}
const moved = changes as Partial<MovedChanges>
const fromTitle = moved.fromBoardColumnId
? (columnTitlesById.get(moved.fromBoardColumnId) ?? 'a column')
: 'a column'
const toTitle = moved.toBoardColumnId
? (columnTitlesById.get(moved.toBoardColumnId) ?? 'a column')
: 'a column'
return `moved from ${fromTitle} to ${toTitle}`
}
const renderActivitySummary = (
activity: Activity,
columnTitlesById?: Map<string, string>
): string => {
if (activity.action === 'created') {
return 'created this task'
}
if (activity.action === 'moved') {
return renderMovedSummary(activity.changes, columnTitlesById)
}
return renderUpdatedSummary(activity.changes)
}
export default renderActivitySummary