Harden optimistic comment IDs

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
dotta 2026-03-28 10:04:46 -05:00
parent 52bb4ea37a
commit cfb7dd4818
2 changed files with 34 additions and 2 deletions

View file

@ -16,6 +16,14 @@ function toTimestamp(value: Date | string) {
return new Date(value).getTime();
}
function createOptimisticCommentId() {
const randomUuid = globalThis.crypto?.randomUUID?.();
if (randomUuid) {
return `optimistic-${randomUuid}`;
}
return `optimistic-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
}
export function sortIssueComments<T extends { createdAt: Date | string; id: string }>(comments: T[]) {
return [...comments].sort((a, b) => {
const createdAtDiff = toTimestamp(a.createdAt) - toTimestamp(b.createdAt);
@ -31,7 +39,7 @@ export function createOptimisticIssueComment(params: {
authorUserId: string | null;
}): OptimisticIssueComment {
const now = new Date();
const clientId = `optimistic-${crypto.randomUUID()}`;
const clientId = createOptimisticCommentId();
return {
id: clientId,
clientId,