Skip to content

Use toSorted() instead of mutating .sort() on shared/cached arrays #1046

@MODSetter

Description

@MODSetter

Description

Two instances use .sort() which mutates arrays in place. If the source returns a cached/shared array, the sort order can leak across requests or corrupt data held in a Map.

Vercel React Best Practices Rule: js-tosorted-immutable (7.12)

Files to change

1. surfsense_web/app/(home)/changelog/page.tsx (lines 31-36)

const allPages = source.getPages() as ChangelogPageItem[];
const sortedChangelogs = allPages.sort((a, b) => {
  const dateA = new Date(a.data.date).getTime();
  const dateB = new Date(b.data.date).getTime();
  return dateB - dateA;
});

Fix:

const sortedChangelogs = allPages.toSorted((a, b) => {
  const dateA = new Date(a.data.date).getTime();
  const dateB = new Date(b.data.date).getTime();
  return dateB - dateA;
});

2. surfsense_web/hooks/use-comments-sync.ts (lines 119-122)

const replies = (group.replies.get(raw.id) || [])
  .sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime())
  .map((r) => transformReply(r, memberMap, currentUserId, isOwner));

Fix:

const replies = [...(group.replies.get(raw.id) ?? [])]
  .sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime())
  .map((r) => transformReply(r, memberMap, currentUserId, isOwner));
// Or use .toSorted() if available

Acceptance criteria

  • Neither file mutates the source array
  • Changelog page still displays entries in reverse chronological order
  • Comment replies still display in chronological order

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions