taskTimelineGroups.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import type { Locale, ZCodeTaskMeta } from "@zcode/shared";
  2. const DAY_MS = 24 * 60 * 60 * 1000;
  3. type TaskTimelineGroupKind =
  4. | "today"
  5. | "yesterday"
  6. | "daysAgo"
  7. | "thisWeek"
  8. | "lastWeek"
  9. | "thisMonth"
  10. | "lastMonth"
  11. | "older";
  12. interface TaskTimelineGroupKey {
  13. kind: TaskTimelineGroupKind;
  14. daysAgo?: number;
  15. }
  16. interface TaskTimelineGroup<TTask> {
  17. key: string;
  18. label: TaskTimelineGroupKey;
  19. items: TTask[];
  20. }
  21. function startOfDay(timestamp: number): number {
  22. const date = new Date(timestamp);
  23. return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
  24. }
  25. function startOfMonth(timestamp: number): number {
  26. const date = new Date(timestamp);
  27. return new Date(date.getFullYear(), date.getMonth(), 1).getTime();
  28. }
  29. function startOfWeek(timestamp: number, locale: Locale): number {
  30. const date = new Date(startOfDay(timestamp));
  31. const day = date.getDay();
  32. const weekStartsOn = locale === "en-US" ? 0 : 1;
  33. const offset = (day - weekStartsOn + 7) % 7;
  34. date.setDate(date.getDate() - offset);
  35. return date.getTime();
  36. }
  37. function resolveTaskTimelineGroupKey(
  38. timestamp: number,
  39. now: number,
  40. locale: Locale,
  41. ): TaskTimelineGroupKey {
  42. const todayStart = startOfDay(now);
  43. const itemDayStart = startOfDay(timestamp);
  44. const dayDiff = Math.floor((todayStart - itemDayStart) / DAY_MS);
  45. if (dayDiff <= 0) {
  46. return { kind: "today" };
  47. }
  48. if (dayDiff === 1) {
  49. return { kind: "yesterday" };
  50. }
  51. if (dayDiff <= 3) {
  52. return { kind: "daysAgo", daysAgo: dayDiff };
  53. }
  54. const thisWeekStart = startOfWeek(now, locale);
  55. if (itemDayStart >= thisWeekStart) {
  56. return { kind: "thisWeek" };
  57. }
  58. const lastWeekStart = thisWeekStart - 7 * DAY_MS;
  59. if (itemDayStart >= lastWeekStart) {
  60. return { kind: "lastWeek" };
  61. }
  62. const thisMonthStart = startOfMonth(now);
  63. if (itemDayStart >= thisMonthStart) {
  64. return { kind: "thisMonth" };
  65. }
  66. const nowDate = new Date(now);
  67. const lastMonthStart = new Date(nowDate.getFullYear(), nowDate.getMonth() - 1, 1).getTime();
  68. if (itemDayStart >= lastMonthStart) {
  69. return { kind: "lastMonth" };
  70. }
  71. return { kind: "older" };
  72. }
  73. export function getTaskTimelineGroupMessage(groupKey: TaskTimelineGroupKey): {
  74. id: string;
  75. values?: Record<string, string>;
  76. } {
  77. if (groupKey.kind === "daysAgo") {
  78. return {
  79. id: "taskTimeline.daysAgo",
  80. values: { days: String(groupKey.daysAgo ?? 0) },
  81. };
  82. }
  83. return { id: `taskTimeline.${groupKey.kind}` };
  84. }
  85. function serializeTaskTimelineGroupKey(groupKey: TaskTimelineGroupKey): string {
  86. return groupKey.kind === "daysAgo" ? `${groupKey.kind}:${groupKey.daysAgo ?? 0}` : groupKey.kind;
  87. }
  88. export function groupTaskTimelineItems<
  89. TTask extends Pick<ZCodeTaskMeta, "createdAt" | "updatedAt">,
  90. >(
  91. items: TTask[],
  92. options: {
  93. sortBy: "created" | "updated";
  94. now: number;
  95. locale: Locale;
  96. },
  97. ): TaskTimelineGroup<TTask>[] {
  98. const groups: TaskTimelineGroup<TTask>[] = [];
  99. const groupByKey = new Map<string, TaskTimelineGroup<TTask>>();
  100. for (const item of items) {
  101. const timestamp = options.sortBy === "created" ? item.createdAt : item.updatedAt;
  102. const label = resolveTaskTimelineGroupKey(timestamp, options.now, options.locale);
  103. const key = serializeTaskTimelineGroupKey(label);
  104. const existingGroup = groupByKey.get(key);
  105. if (existingGroup) {
  106. existingGroup.items.push(item);
  107. continue;
  108. }
  109. const group = { key, label, items: [item] };
  110. groupByKey.set(key, group);
  111. groups.push(group);
  112. }
  113. return groups;
  114. }