Skip to content

Add sticky current-user leaderboard row#182

Open
shriya53 wants to merge 3 commits into
Coder-s-OG-s:mainfrom
shriya53:fix-leaderboard
Open

Add sticky current-user leaderboard row#182
shriya53 wants to merge 3 commits into
Coder-s-OG-s:mainfrom
shriya53:fix-leaderboard

Conversation

@shriya53
Copy link
Copy Markdown
Contributor

Summary

Adds a persistent “YOUR RANK” section to leaderboard views so users can always see their own ranking and XP even when they are outside the visible leaderboard range.
Also improves leaderboard data handling by extending the leaderboard action to return current-user ranking data separately instead of relying only on visible leaderboard entries.

Type of Change

  • Bug fix
  • UI / UX improvement
  • New feature
  • Refactor
  • Documentation
  • Other

Related Issue

Closes #78

What was changed?

  • Updated leaderboard UI to show a dedicated “YOUR RANK” section when the current user is outside the visible leaderboard list
  • Prevented duplicate rendering when the current user is already visible in the leaderboard
  • Improved leaderboard action logic to return current user rank data separately for accurate rendering
  • Added current-user rank lookup support in leaderboard.ts
  • Applied the fix to both:
    • Dashboard leaderboard snapshot
    • Full /leaderboard page

Checklist

  • My code follows the project structure and conventions
  • I tested this locally ('npm run dev')
  • No hardcoded secrets or credentials
  • I have updated documentation if needed

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 19, 2026

@shriya53 is attempting to deploy a commit to the codersogs-3057's projects Team on Vercel.

A member of the Team first needs to authorize it.

@shriya53
Copy link
Copy Markdown
Contributor Author

shriya53 commented May 19, 2026

Hi @Ayush-Patel-56 ,

Could you please add the gssoc:approved and level tags to this merged PR so my points can be tracked on the leaderboard?

Thank you!

Copy link
Copy Markdown
Collaborator

@Siddhartha-singh01 Siddhartha-singh01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feature @shriya53 the sticky "YOUR RANK" idea is good, but there
are two critical issues with the implementation that block merge.

1. Critical: cache key isn't user-scoped, so users see each other's rank.

The cache key is leaderboard:${scope}:${scopeId}:${limit} shared across users.
But the cached response now contains currentUserRank, which is user-specific.
Concretely: User A loads the page → A's rank gets cached. User B loads → cache hit
→ B sees A's handle, avatar, XP, rank as B's own "YOUR RANK" card. That's a real
privacy leak.

Fix: either include user.id in the cache key, or (cleaner) cache only the shared
entries and compute currentUserRank per-request outside the cache:

const cached = await cacheGet<LeaderboardEntry[]>(cacheKey);
let entries: LeaderboardEntry[];
if (cached) { entries = cached; }
else { entries = /* compute */; await cacheSet(cacheKey, entries, TTL); }

const currentUserRank = await computeCurrentUserRank(db, user.id, scope, scopeId);
return ok({ entries, currentUserRank });

2. Critical: loads every profile in the DB to find one user's rank.

select * from profiles order by xp desc with no LIMIT pulls every row, then does
findIndex in JS. At 1k users that's 1k rows transferred per request; it doesn't
scale. The DB can do this in one row:

SELECT count(*) + 1 AS rank
FROM profiles
WHERE xp > (SELECT xp FROM profiles WHERE id = $1);

Or use ROW_NUMBER() OVER (ORDER BY xp DESC). Either returns exactly one row.

3. The current-user-rank query ignores scope and scopeId.

It always queries the global profiles table so a user viewing the "Python"
leaderboard sees their global rank in the "YOUR RANK" card, not their Python rank.
The sticky row will mismatch the displayed list. The rank query needs to apply the
same scope filters as the entries query.

Minor: await getServerSupabase() that function is synchronous, the await is
unnecessary. And a unit test for the currentUserRank logic (the existing
recommendations.test.ts is a good template) would be nice.

Once 1, 2, and 3 are fixed, this is ready to merge. The feature itself is welcome.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mergeship Ready Ready Preview, Comment May 22, 2026 2:47pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leaderboard 'YOU' row isn't visible when user is ranked below the fold

2 participants