Improve historical context tense handling
This commit is contained in:
parent
8bd43c2c3a
commit
f90376708b
5 changed files with 145 additions and 7 deletions
|
|
@ -573,6 +573,7 @@ function formatHistoryResults(
|
|||
authorId: result.metadata.authorId,
|
||||
authorName: result.metadata.authorName,
|
||||
timestamp: result.metadata.timestamp,
|
||||
timeContext: formatTimeContext(result.metadata.timestamp),
|
||||
jumpUrl: result.metadata.jumpUrl,
|
||||
threadId: result.metadata.threadId,
|
||||
}));
|
||||
|
|
@ -604,6 +605,7 @@ function formatArchivedMessageContext(
|
|||
id: message.id,
|
||||
isAnchor: message.id === anchorMessageId,
|
||||
timestamp: message.timestamp,
|
||||
timeContext: formatTimeContext(message.timestamp),
|
||||
authorId: message.authorId,
|
||||
authorName: message.authorName,
|
||||
channelId: message.channelId,
|
||||
|
|
@ -614,3 +616,58 @@ function formatArchivedMessageContext(
|
|||
editedAt: message.editedAt,
|
||||
};
|
||||
}
|
||||
|
||||
function formatTimeContext(timestamp: string | undefined): string | undefined {
|
||||
if (!timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parsed = new Date(timestamp);
|
||||
|
||||
if (Number.isNaN(parsed.getTime())) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const absolute = formatUtcTimestamp(timestamp);
|
||||
const now = new Date();
|
||||
const deltaMs = now.getTime() - parsed.getTime();
|
||||
const absDeltaMs = Math.abs(deltaMs);
|
||||
const suffix = deltaMs >= 0 ? "ago" : "from now";
|
||||
|
||||
if (absDeltaMs < 60_000) {
|
||||
return `${absolute} (just now)`;
|
||||
}
|
||||
|
||||
const minuteMs = 60_000;
|
||||
const hourMs = 60 * minuteMs;
|
||||
const dayMs = 24 * hourMs;
|
||||
const yearMs = 365 * dayMs;
|
||||
|
||||
if (absDeltaMs < hourMs) {
|
||||
const minutes = Math.round(absDeltaMs / minuteMs);
|
||||
return `${absolute} (${minutes} minute${minutes === 1 ? "" : "s"} ${suffix})`;
|
||||
}
|
||||
|
||||
if (absDeltaMs < dayMs) {
|
||||
const hours = Math.round(absDeltaMs / hourMs);
|
||||
return `${absolute} (${hours} hour${hours === 1 ? "" : "s"} ${suffix})`;
|
||||
}
|
||||
|
||||
if (absDeltaMs < yearMs) {
|
||||
const days = Math.round(absDeltaMs / dayMs);
|
||||
return `${absolute} (${days} day${days === 1 ? "" : "s"} ${suffix})`;
|
||||
}
|
||||
|
||||
const years = Math.round(absDeltaMs / yearMs);
|
||||
return `${absolute} (${years} year${years === 1 ? "" : "s"} ${suffix})`;
|
||||
}
|
||||
|
||||
function formatUtcTimestamp(timestamp: string): string {
|
||||
const date = new Date(timestamp);
|
||||
const year = date.getUTCFullYear();
|
||||
const month = `${date.getUTCMonth() + 1}`.padStart(2, "0");
|
||||
const day = `${date.getUTCDate()}`.padStart(2, "0");
|
||||
const hours = `${date.getUTCHours()}`.padStart(2, "0");
|
||||
const minutes = `${date.getUTCMinutes()}`.padStart(2, "0");
|
||||
return `${year}-${month}-${day} ${hours}:${minutes} UTC`;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue