stackchain-dashboard/frontend/my-work.js
timmy f34fc8b5f5
All checks were successful
CI / lint (pull_request) Successful in 12s
CI / build-frontend (pull_request) Successful in 5s
feat: add unread updates to My Work (#133)
2026-08-06 19:59:39 +00:00

93 lines
3.6 KiB
JavaScript

function buildMyWork(data) {
const login = data.user?.login || '';
const issues = (data.issues || []).map((item) => ({ ...item, kind: 'issue' }));
const pulls = (data.pull_requests || []).map((item) => ({ ...item, kind: 'pull' }));
const priorityLabels = ['p0', 'priority-high', 'critical'];
const work = issues.concat(pulls).map((item) => {
const labels = item.labels || [];
const priorityLabel = labels.find((label) =>
priorityLabels.includes(String(label).toLowerCase())
);
const assigned = (item.assignees || []).includes(login);
const isReview = (item.work_reasons || []).includes('review_requested');
return {
...item,
key: (item.repository || 'unknown') + '#' + item.number,
is_review: isReview,
is_assigned: assigned,
has_update: false,
reason: priorityLabel ? priorityLabel + ' priority' :
(isReview ? 'Needs your review' : (assigned ? 'Assigned to you' : 'Open work')),
_priority: priorityLabel ? 0 : (isReview ? 2 : (assigned ? 3 : 4)),
};
});
const byKey = new Map(work.map((item) => [item.kind + ':' + item.key, item]));
(data.notifications || []).filter((item) => item && item.unread).forEach((update) => {
const key = (update.repository || 'unknown') + '#' + update.number;
const subjectKind = String(update.subject_type || '').toLowerCase().includes('pull') ? 'pull' : 'issue';
const existing = byKey.get(subjectKind + ':' + key);
if (existing) {
existing.has_update = true;
existing.url = update.url || existing.url;
existing.updated_at = update.updated_at || existing.updated_at;
existing._priority = Math.min(existing._priority, 1);
return;
}
if (!update.url) return;
const item = {
...update,
key,
kind: 'update',
is_review: false,
is_assigned: false,
has_update: true,
reason: 'Unread update',
_priority: 1,
};
work.push(item);
byKey.set(subjectKind + ':' + key, item);
});
return work.sort((left, right) =>
left._priority - right._priority ||
String(right.updated_at || '').localeCompare(String(left.updated_at || '')) ||
left.key.localeCompare(right.key)
).map(({ _priority, ...item }) => item);
}
function filterMyWork(items, selectedFilter) {
if (selectedFilter === 'all') return items;
if (selectedFilter === 'review') return items.filter((item) => item.is_review);
if (selectedFilter === 'update') return items.filter((item) => item.has_update);
return items.filter((item) => item.kind === selectedFilter);
}
function summarizeMyWork(items) {
const updates = items.filter((item) => item.has_update).length;
const reviews = items.filter((item) => item.is_review).length;
const assigned = items.filter((item) => item.is_assigned).length;
const updateLabel = updates + ' unread update' + (updates === 1 ? '' : 's');
const reviewLabel = reviews + ' review' + (reviews === 1 ? '' : 's');
const assignedLabel = assigned + ' assigned';
return (updates ? updateLabel + ' · ' : '') + reviewLabel + ' · ' + assignedLabel;
}
function countMyWork(items) {
return {
all: items.length,
issue: items.filter((item) => item.kind === 'issue').length,
pull: items.filter((item) => item.kind === 'pull' && !item.is_review).length,
review: items.filter((item) => item.is_review).length,
update: items.filter((item) => item.has_update).length,
};
}
if (typeof module !== 'undefined' && module.exports) {
buildMyWork.filterMyWork = filterMyWork;
buildMyWork.summarizeMyWork = summarizeMyWork;
buildMyWork.countMyWork = countMyWork;
module.exports = buildMyWork;
}