|
| 1 | +import type { |
| 2 | + Project, |
| 3 | + ProjectActivitySummary, |
| 4 | + ProjectIssueListItem, |
| 5 | + ProjectPullRequestListItem, |
| 6 | + ProjectRepoSnapshot, |
| 7 | +} from "@/features/projects/hooks"; |
| 8 | + |
| 9 | +const WEEK_SECONDS = 7 * 24 * 60 * 60; |
| 10 | + |
| 11 | +export type ProjectsActivityDigest = { |
| 12 | + highlights: string[]; |
| 13 | + prefix: string; |
| 14 | + suffix: string; |
| 15 | +}; |
| 16 | + |
| 17 | +function plural(count: number, singular: string, pluralForm = `${singular}s`) { |
| 18 | + return `${count} ${count === 1 ? singular : pluralForm}`; |
| 19 | +} |
| 20 | + |
| 21 | +/** Builds a short, deterministic sentence from the currently loaded activity. */ |
| 22 | +export function buildProjectsActivityDigest({ |
| 23 | + issues, |
| 24 | + nowSeconds, |
| 25 | + projects, |
| 26 | + pullRequests, |
| 27 | + snapshots, |
| 28 | + summaries, |
| 29 | +}: { |
| 30 | + issues: ProjectIssueListItem[]; |
| 31 | + nowSeconds: number; |
| 32 | + projects: Project[]; |
| 33 | + pullRequests: ProjectPullRequestListItem[]; |
| 34 | + snapshots?: Record<string, ProjectRepoSnapshot>; |
| 35 | + summaries?: Record<string, ProjectActivitySummary>; |
| 36 | +}): ProjectsActivityDigest { |
| 37 | + const since = nowSeconds - WEEK_SECONDS; |
| 38 | + const activeProjectIds = new Set<string>(); |
| 39 | + let commitCount = 0; |
| 40 | + for (const [projectId, snapshot] of Object.entries(snapshots ?? {})) { |
| 41 | + const recent = snapshot.commits.filter( |
| 42 | + (commit) => commit.timestamp >= since, |
| 43 | + ).length; |
| 44 | + commitCount += recent; |
| 45 | + if (recent > 0) activeProjectIds.add(projectId); |
| 46 | + } |
| 47 | + const taskCount = issues.filter(({ issue, project }) => { |
| 48 | + const recent = issue.createdAt >= since; |
| 49 | + if (recent) activeProjectIds.add(project.id); |
| 50 | + return recent; |
| 51 | + }).length; |
| 52 | + const reviewCount = pullRequests.filter(({ project, pullRequest }) => { |
| 53 | + const recent = pullRequest.createdAt >= since; |
| 54 | + if (recent) activeProjectIds.add(project.id); |
| 55 | + return recent; |
| 56 | + }).length; |
| 57 | + const highlights = [ |
| 58 | + commitCount > 0 ? `${plural(commitCount, "new commit")}` : null, |
| 59 | + taskCount > 0 ? `${plural(taskCount, "task")} opened` : null, |
| 60 | + reviewCount > 0 ? `${plural(reviewCount, "review")} opened` : null, |
| 61 | + ].filter((value): value is string => value !== null); |
| 62 | + |
| 63 | + if (highlights.length > 0) { |
| 64 | + highlights.push(`${plural(activeProjectIds.size, "active project")}`); |
| 65 | + return { |
| 66 | + highlights, |
| 67 | + prefix: "This week:", |
| 68 | + suffix: ".", |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + const totals = Object.values(summaries ?? {}).reduce( |
| 73 | + (result, summary) => ({ |
| 74 | + reviews: result.reviews + summary.prCount, |
| 75 | + tasks: result.tasks + summary.issueCount, |
| 76 | + }), |
| 77 | + { reviews: 0, tasks: 0 }, |
| 78 | + ); |
| 79 | + return { |
| 80 | + highlights: [ |
| 81 | + plural(projects.length, "project"), |
| 82 | + plural(totals.tasks, "task"), |
| 83 | + plural(totals.reviews, "review"), |
| 84 | + ], |
| 85 | + prefix: "Currently tracking", |
| 86 | + suffix: ".", |
| 87 | + }; |
| 88 | +} |
0 commit comments