diff --git a/app/db/tables.ts b/app/db/tables.ts index e61110516..e15be88e9 100644 --- a/app/db/tables.ts +++ b/app/db/tables.ts @@ -918,6 +918,10 @@ export interface Friendship { userOneId: number; userTwoId: number; createdAt: Generated; + /** userOne keeps this friend at the top of their friends list */ + isPinnedByUserOne: Generated; + /** userTwo keeps this friend at the top of their friends list */ + isPinnedByUserTwo: Generated; } /** Pending friend request from one user to another. */ diff --git a/app/features/availability/availability-schemas.ts b/app/features/availability/availability-schemas.ts index 5aa089e43..f048ec509 100644 --- a/app/features/availability/availability-schemas.ts +++ b/app/features/availability/availability-schemas.ts @@ -9,7 +9,13 @@ import { stringConstant, textField, } from "~/form/fields"; -import { _action, id, superRefine, type ValidationCtx } from "~/utils/schema"; +import { + _action, + id, + revalidateRoot, + superRefine, + type ValidationCtx, +} from "~/utils/schema"; import { AVAILABILITY } from "./availability-constants"; const DAY_MINUTES = 24 * 60; @@ -54,7 +60,7 @@ export const saveWeekSchema = v.object({ export const dismissScheduleNudgeSchema = v.object({ _action: _action("DISMISS_SCHEDULE_NUDGE"), - revalidateRoot: v.optional(v.nullable(v.literal(true))), + revalidateRoot, }); export const saveScheduleVisibilitySchema = v.object({ @@ -63,7 +69,7 @@ export const saveScheduleVisibilitySchema = v.object({ label: "labels.scheduleSharedWith", minLength: 0, }), - revalidateRoot: v.optional(v.nullable(v.literal(true))), + revalidateRoot, }); export const eventsActionSchema = v.union([ diff --git a/app/features/calendar/routes/events.tsx b/app/features/calendar/routes/events.tsx index 8c5e87795..63e4f9343 100644 --- a/app/features/calendar/routes/events.tsx +++ b/app/features/calendar/routes/events.tsx @@ -86,6 +86,7 @@ export default function EventsPage() { controlled active={filter === value} defaultShouldRevalidate={false} + preventScrollReset > {viewLabels[value]} diff --git a/app/features/friends/FriendRepository.server.test.ts b/app/features/friends/FriendRepository.server.test.ts index 2e01af3e3..f0b0c06a9 100644 --- a/app/features/friends/FriendRepository.server.test.ts +++ b/app/features/friends/FriendRepository.server.test.ts @@ -420,3 +420,69 @@ describe("findByUserIdWithActivity", () => { expect(result).toHaveLength(0); }); }); + +describe("updateOwnFriendshipPinned", () => { + beforeEach(async () => { + await createUsers(3); + }); + + const isPinnedFor = async (userId: number) => { + const [friend] = await FriendRepository.findByUserIdWithActivity(userId); + + return friend.isPinned; + }; + + test("pins the friendship for the acting user only", async () => { + const friendship = await FriendshipFactory.create({ + userOneId: users.id(1), + userTwoId: users.id(2), + }); + + await withUserId(users.id(2), () => + FriendRepository.updateOwnFriendshipPinned({ + friendshipId: friendship.id, + isPinned: true, + }), + ); + + expect(await isPinnedFor(users.id(2))).toBe(1); + expect(await isPinnedFor(users.id(1))).toBe(0); + }); + + test("unpins a pinned friendship", async () => { + const friendship = await FriendshipFactory.create({ + userOneId: users.id(1), + userTwoId: users.id(2), + }); + + await withUserId(users.id(1), async () => { + await FriendRepository.updateOwnFriendshipPinned({ + friendshipId: friendship.id, + isPinned: true, + }); + await FriendRepository.updateOwnFriendshipPinned({ + friendshipId: friendship.id, + isPinned: false, + }); + }); + + expect(await isPinnedFor(users.id(1))).toBe(0); + }); + + test("does not pin a friendship the user is not part of", async () => { + const friendship = await FriendshipFactory.create({ + userOneId: users.id(1), + userTwoId: users.id(2), + }); + + await withUserId(users.id(3), () => + FriendRepository.updateOwnFriendshipPinned({ + friendshipId: friendship.id, + isPinned: true, + }), + ); + + expect(await isPinnedFor(users.id(1))).toBe(0); + expect(await isPinnedFor(users.id(2))).toBe(0); + }); +}); diff --git a/app/features/friends/FriendRepository.server.ts b/app/features/friends/FriendRepository.server.ts index cde2ef1ae..73ea79da5 100644 --- a/app/features/friends/FriendRepository.server.ts +++ b/app/features/friends/FriendRepository.server.ts @@ -1,10 +1,11 @@ import { sub } from "date-fns"; import { type SelectQueryBuilder, sql } from "kysely"; import { db } from "~/db/sql"; -import type { DB } from "~/db/tables"; +import type { DB, DBBoolean } from "~/db/tables"; import { actorId } from "~/features/auth/core/user.server"; import { dateToDatabaseTimestamp } from "~/utils/dates"; import { commonUserSelect } from "~/utils/kysely.server"; +import { toDBBoolean } from "~/utils/sql"; import { FRIEND } from "./friends-constants"; export async function findByUserIdWithActivity(userId: number) { @@ -33,9 +34,16 @@ export async function findByUserIdWithActivity(userId: number) { ]), ), ) - .select([ + .select((eb) => [ "Friendship.id as friendshipId", "Friendship.createdAt as friendshipCreatedAt", + eb + .case() + .when("Friendship.userOneId", "=", userId) + .then(eb.ref("Friendship.isPinnedByUserOne")) + .else(eb.ref("Friendship.isPinnedByUserTwo")) + .end() + .as("isPinned"), ]) .orderBy("Friendship.createdAt", "desc") .execute(), @@ -58,6 +66,7 @@ export async function findByUserIdWithActivity(userId: number) { ...row, friendshipId: null as number | null, friendshipCreatedAt: null as number | null, + isPinned: 0 as DBBoolean, })), ]; } @@ -228,6 +237,43 @@ export async function deleteOwnFriendshipById(id: number) { .execute(); } +/** Pins or unpins the friendship for the acting user only, the other side keeps their own choice. */ +export async function updateOwnFriendshipPinned({ + friendshipId, + isPinned, +}: { + friendshipId: number; + isPinned: boolean; +}) { + const userId = actorId(); + const value = toDBBoolean(isPinned); + + return db + .updateTable("Friendship") + .set((eb) => ({ + isPinnedByUserOne: eb + .case() + .when("Friendship.userOneId", "=", userId) + .then(value) + .else(eb.ref("Friendship.isPinnedByUserOne")) + .end(), + isPinnedByUserTwo: eb + .case() + .when("Friendship.userTwoId", "=", userId) + .then(value) + .else(eb.ref("Friendship.isPinnedByUserTwo")) + .end(), + })) + .where("Friendship.id", "=", friendshipId) + .where((eb) => + eb.or([ + eb("Friendship.userOneId", "=", userId), + eb("Friendship.userTwoId", "=", userId), + ]), + ) + .execute(); +} + export async function findPendingReceivedRequests(receiverId: number) { return db .selectFrom("FriendRequest") diff --git a/app/features/friends/actions/friends.server.ts b/app/features/friends/actions/friends.server.ts index a2fe72a16..4dda9a03a 100644 --- a/app/features/friends/actions/friends.server.ts +++ b/app/features/friends/actions/friends.server.ts @@ -67,6 +67,22 @@ export const action: ActionFunction = async ({ request }) => { break; } + case "PIN_FRIEND": { + await FriendRepository.updateOwnFriendshipPinned({ + friendshipId: result.data.friendshipId, + isPinned: true, + }); + + break; + } + case "UNPIN_FRIEND": { + await FriendRepository.updateOwnFriendshipPinned({ + friendshipId: result.data.friendshipId, + isPinned: false, + }); + + break; + } case "ACCEPT_REQUEST": { const friendRequest = await FriendRepository.findFriendRequestByIdAndReceiver({ diff --git a/app/features/friends/friends-constants.test.ts b/app/features/friends/friends-constants.test.ts new file mode 100644 index 000000000..9b425517b --- /dev/null +++ b/app/features/friends/friends-constants.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, test } from "vitest"; +import { friendSectionSortValue } from "./friends-constants"; + +describe("friendSectionSortValue", () => { + test.each([ + { + why: "pinned with activity", + isPinned: true, + activityType: "SENDOUQ", + expected: 0, + }, + { + why: "unpinned with activity", + isPinned: false, + activityType: "TOURNAMENT_MATCH", + expected: 1, + }, + { + why: "pinned without activity", + isPinned: true, + activityType: null, + expected: 2, + }, + { + why: "unpinned without activity", + isPinned: false, + activityType: null, + expected: 3, + }, + ] as const)( + "$why sorts as $expected", + ({ isPinned, activityType, expected }) => { + expect(friendSectionSortValue({ isPinned, activityType })).toBe(expected); + }, + ); +}); diff --git a/app/features/friends/friends-constants.ts b/app/features/friends/friends-constants.ts index 7500215cf..8806e7b21 100644 --- a/app/features/friends/friends-constants.ts +++ b/app/features/friends/friends-constants.ts @@ -38,3 +38,19 @@ export function friendActivitySortValue(type: FriendActivityType | null) { if (isInProgressFriendActivity(type)) return 2; return 0; } + +/** Section of the friends list, pinned friends with activity at the top and idle unpinned ones at the bottom. Lower sorts first. */ +export function friendSectionSortValue({ + isPinned, + activityType, +}: { + isPinned: boolean; + activityType: FriendActivityType | null; +}) { + const hasActivity = activityType !== null; + + if (isPinned && hasActivity) return 0; + if (hasActivity) return 1; + if (isPinned) return 2; + return 3; +} diff --git a/app/features/friends/friends-schemas.server.ts b/app/features/friends/friends-schemas.server.ts index 89cf52974..d88486117 100644 --- a/app/features/friends/friends-schemas.server.ts +++ b/app/features/friends/friends-schemas.server.ts @@ -7,7 +7,9 @@ import { cancelFriendRequestSchema, declineFriendRequestSchema, deleteFriendSchema, + pinFriendSchema, sendFriendRequestBaseSchema, + unpinFriendSchema, } from "./friends-schemas"; const sendFriendRequestSchemaServer = v.pipeAsync( @@ -54,4 +56,6 @@ export const friendsActionSchema = v.unionAsync([ deleteFriendSchema, acceptFriendRequestSchema, declineFriendRequestSchema, + pinFriendSchema, + unpinFriendSchema, ]); diff --git a/app/features/friends/friends-schemas.ts b/app/features/friends/friends-schemas.ts index 7dda77d34..5b15abdff 100644 --- a/app/features/friends/friends-schemas.ts +++ b/app/features/friends/friends-schemas.ts @@ -1,6 +1,6 @@ import * as v from "valibot"; import { stringConstant, userSearch } from "~/form/fields"; -import { _action, id } from "~/utils/schema"; +import { _action, id, revalidateRoot } from "~/utils/schema"; export const sendFriendRequestBaseSchema = v.object({ _action: stringConstant("SEND_REQUEST"), @@ -26,3 +26,15 @@ export const declineFriendRequestSchema = v.object({ _action: _action("DECLINE_REQUEST"), friendRequestId: id, }); + +export const pinFriendSchema = v.object({ + _action: _action("PIN_FRIEND"), + friendshipId: id, + revalidateRoot, +}); + +export const unpinFriendSchema = v.object({ + _action: _action("UNPIN_FRIEND"), + friendshipId: id, + revalidateRoot, +}); diff --git a/app/features/friends/loaders/friends.server.ts b/app/features/friends/loaders/friends.server.ts index 73192c1f9..ebff4bd70 100644 --- a/app/features/friends/loaders/friends.server.ts +++ b/app/features/friends/loaders/friends.server.ts @@ -4,7 +4,10 @@ import * as FriendSchedule from "~/features/availability/core/FriendSchedule.ser import { getViewerTimezone } from "~/features/timezone/timezone-context.server"; import { userPage } from "~/utils/urls"; import * as FriendRepository from "../FriendRepository.server"; -import { friendActivitySortValue } from "../friends-constants"; +import { + friendActivitySortValue, + friendSectionSortValue, +} from "../friends-constants"; import { resolveFriendActivity, resolveSendouQMatchStreams, @@ -63,9 +66,11 @@ export const loader = async () => { tournamentId: activity.tournamentId ?? friend.tournamentId, streamUrl: activity.streamUrl, friendshipCreatedAt: friend.friendshipCreatedAt, + isPinned: Boolean(friend.isPinned), schedule: schedules.get(friend.id) ?? null, }; }), + [(friend) => friendSectionSortValue(friend), "asc"], [(friend) => friendActivitySortValue(friend.activityType), "desc"], [(friend) => (friend.schedule ? 1 : 0), "desc"], [(friend) => friend.friendshipCreatedAt ?? 0, "desc"], @@ -100,6 +105,7 @@ export const loader = async () => { matchId: activity.matchId, tournamentId: activity.tournamentId ?? tm.tournamentId, streamUrl: activity.streamUrl, + isPinned: false, schedule: schedules.get(tm.id) ?? null, }; }), diff --git a/app/features/friends/routes/friends.module.css b/app/features/friends/routes/friends.module.css index c2f34dfe4..d435c26a5 100644 --- a/app/features/friends/routes/friends.module.css +++ b/app/features/friends/routes/friends.module.css @@ -37,11 +37,20 @@ gap: var(--s-1-5); } -.scheduleSlot { +.rowActions { + display: flex; + gap: var(--s-1-5); + margin-block-end: var(--s-1); +} + +.actionSlot { display: flex; width: 18px; - margin-block-end: var(--s-1); & > button { height: auto; } } + +.pinForm { + display: contents; +} diff --git a/app/features/friends/routes/friends.tsx b/app/features/friends/routes/friends.tsx index d05e73b23..b38b851b5 100644 --- a/app/features/friends/routes/friends.tsx +++ b/app/features/friends/routes/friends.tsx @@ -1,7 +1,8 @@ -import { CalendarDays } from "lucide-react"; +import { CalendarDays, Pin } from "lucide-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link, type MetaFunction, useLoaderData } from "react-router"; +import * as R from "remeda"; import { ActionButton } from "~/components/ActionButton"; import { Avatar } from "~/components/Avatar"; import { Divider } from "~/components/Divider"; @@ -15,11 +16,14 @@ import { useSearchParam } from "~/modules/search-params/hooks"; import { metaTags } from "~/utils/remix"; import type { SendouRouteHandle } from "~/utils/remix.server"; import { FriendMenu } from "../components/FriendMenu"; +import { friendSectionSortValue } from "../friends-constants"; import { acceptFriendRequestSchema, cancelFriendRequestSchema, declineFriendRequestSchema, + pinFriendSchema, sendFriendRequestBaseSchema, + unpinFriendSchema, } from "../friends-schemas"; import { friendsSearchParams, @@ -212,6 +216,7 @@ function FriendsListSection() { secondary controlled active={filter === value} + preventScrollReset > {viewLabels[value]} @@ -234,22 +239,71 @@ function FriendsListSection() { } function FriendRow({ item }: { item: ShownItem }) { + const friendshipId = + "friendshipId" in item && typeof item.friendshipId === "number" + ? item.friendshipId + : null; + return (
-
- {item.schedule ? ( - - ) : null} +
+
+ {item.schedule ? ( + + ) : null} +
+
+ {typeof friendshipId === "number" ? ( + + ) : null} +
); } +function PinButton({ + userId, + username, + friendshipId, + isPinned, +}: { + userId: number; + username: string; + friendshipId: number; + isPinned: boolean; +}) { + const { t } = useTranslation(["friends"]); + + return ( + } + aria-label={t( + isPinned ? "friends:friendsList.unpin" : "friends:friendsList.pin", + { name: username }, + )} + aria-pressed={isPinned} + testId={`friend-pin-button-${userId}`} + /> + ); +} + function ScheduleButton({ userId, username, @@ -298,10 +352,11 @@ function resolveShownItems( ...data.teamMembers.filter((tm) => !friendIds.has(tm.id)), ]; - // same order the loader sorted each group in: active first, then the ones - // who shared a schedule - const sortValue = (item: (typeof combined)[number]) => - (item.subtitle ? 2 : 0) + (item.schedule ? 1 : 0); - - return combined.sort((a, b) => sortValue(b) - sortValue(a)); + // same order the loader sorted each group in: pinned and active first, then + // the ones who shared a schedule + return R.sortBy( + combined, + [(item) => friendSectionSortValue(item), "asc"], + [(item) => (item.schedule ? 1 : 0), "desc"], + ); } diff --git a/app/features/settings/settings-schemas.ts b/app/features/settings/settings-schemas.ts index d17cf9ff1..d3d5f2d6b 100644 --- a/app/features/settings/settings-schemas.ts +++ b/app/features/settings/settings-schemas.ts @@ -1,11 +1,11 @@ import * as v from "valibot"; import { hidden, select, stringConstant, toggle } from "~/form/fields"; -import { themeInputSchema } from "~/utils/schema"; +import { revalidateRoot, themeInputSchema } from "~/utils/schema"; export const customThemeSchema = v.object({ _action: stringConstant("UPDATE_CUSTOM_THEME"), newValue: hidden(v.nullable(themeInputSchema), null), - revalidateRoot: v.optional(v.nullable(v.literal(true))), + revalidateRoot, }); export const clockFormatSchema = v.object({ diff --git a/app/features/sidebar/core/sidebar.server.test.ts b/app/features/sidebar/core/sidebar.server.test.ts new file mode 100644 index 000000000..a672fed35 --- /dev/null +++ b/app/features/sidebar/core/sidebar.server.test.ts @@ -0,0 +1,129 @@ +import { describe, expect, test } from "vitest"; +import type { FriendActivityType } from "~/features/friends/friends-constants"; +import { orderSidebarFriends } from "./sidebar.server"; + +describe("orderSidebarFriends", () => { + test("ranks pinned active, active, pinned idle and idle in that order", () => { + const ordered = orderSidebarFriends([ + entry("idle"), + entry("pinnedIdle", { isPinned: true }), + entry("active", { activityType: "SENDOUQ_MATCH" }), + entry("pinnedActive", { + isPinned: true, + activityType: "SENDOUQ_MATCH", + }), + ]); + + expect(idsOf(ordered)).toEqual([ + "pinnedActive", + "active", + "pinnedIdle", + "idle", + ]); + }); + + test("keeps the activity quotas in order within a section", () => { + const ordered = orderSidebarFriends([ + entry("q1", { activityType: "SENDOUQ" }), + entry("q2", { activityType: "SENDOUQ" }), + entry("q3", { activityType: "SENDOUQ" }), + entry("sub1", { activityType: "TOURNAMENT_SUB" }), + entry("sub2", { activityType: "TOURNAMENT_SUB" }), + entry("sub3", { activityType: "TOURNAMENT_SUB" }), + entry("match", { activityType: "SENDOUQ_MATCH" }), + ]); + + expect(idsOf(ordered)).toEqual([ + "q1", + "q2", + "sub1", + "sub2", + "q3", + "sub3", + "match", + ]); + }); + + test("pinning a friend does not disturb the quota order of the rest", () => { + const ordered = orderSidebarFriends([ + entry("q1", { activityType: "SENDOUQ" }), + entry("q2", { activityType: "SENDOUQ" }), + entry("q3", { activityType: "SENDOUQ", isPinned: true }), + entry("sub1", { activityType: "TOURNAMENT_SUB" }), + ]); + + expect(idsOf(ordered)).toEqual(["q3", "q1", "q2", "sub1"]); + }); + + test("shows a pinned friend that would otherwise fall past the visible quota", () => { + const ordered = orderSidebarFriends([ + ...Array.from({ length: 10 }, (_, i) => entry(`idle${i}`)), + entry("pinned", { isPinned: true }), + ]); + + expect(ordered).toHaveLength(8); + expect(idsOf(ordered)[0]).toBe("pinned"); + }); + + test("drops active friends past the visible quota", () => { + const ordered = orderSidebarFriends( + Array.from({ length: 12 }, (_, i) => + entry(`match${i}`, { activityType: "TOURNAMENT_MATCH" }), + ), + ); + + expect(idsOf(ordered)).toEqual([ + "match0", + "match1", + "match2", + "match3", + "match4", + "match5", + "match6", + "match7", + ]); + }); + + test("places friends before team members sharing their activity", () => { + const ordered = orderSidebarFriends([ + entry("teamMember", { + isFriend: false, + activityType: "SENDOUQ_MATCH", + }), + entry("friend", { activityType: "SENDOUQ_MATCH" }), + ]); + + expect(idsOf(ordered)).toEqual(["friend", "teamMember"]); + }); + + test("ranks an active team member above an idle friend", () => { + const ordered = orderSidebarFriends([ + entry("idleFriend"), + entry("activeTeamMember", { + isFriend: false, + activityType: "SENDOUQ", + }), + ]); + + expect(idsOf(ordered)).toEqual(["activeTeamMember", "idleFriend"]); + }); +}); + +function entry( + id: string, + { + isFriend = true, + isPinned = false, + activityType = null, + }: { + isFriend?: boolean; + isPinned?: boolean; + activityType?: FriendActivityType | null; + } = {}, +) { + return { id, isFriend, isPinned, activityType }; +} + +function idsOf(entries: Array<{ id: string }>) { + return entries.map(({ id }) => id); +} diff --git a/app/features/sidebar/core/sidebar.server.ts b/app/features/sidebar/core/sidebar.server.ts index 32d579e1e..ae7f6b0f5 100644 --- a/app/features/sidebar/core/sidebar.server.ts +++ b/app/features/sidebar/core/sidebar.server.ts @@ -17,6 +17,7 @@ import { import * as FriendRepository from "~/features/friends/FriendRepository.server"; import { type FriendActivityType, + friendSectionSortValue, isInProgressFriendActivity, } from "~/features/friends/friends-constants"; import { @@ -330,8 +331,8 @@ function resolveFriends( friendsWithActivity: FriendWithActivity[], streamedSendouQMatches: ReadonlyMap, ) { - const activityForRow = (row: FriendWithActivity) => - resolveFriendActivity({ + const entries = R.uniqueBy(friendsWithActivity, (f) => f.id).map((row) => { + const activity = resolveFriendActivity({ friendId: row.id, tournamentId: row.tournamentId, tournamentName: row.tournamentName, @@ -340,91 +341,69 @@ function resolveFriends( sendouQMatchStreams: streamedSendouQMatches, }); - const unique = R.uniqueBy(friendsWithActivity, (f) => f.id); - const friendRows = unique.filter((f) => f.friendshipId !== null); - const teamMemberRows = unique.filter((f) => f.friendshipId === null); + return { + row, + activity, + isFriend: row.friendshipId !== null, + isPinned: Boolean(row.isPinned), + activityType: activity.type, + }; + }); - const activeFriends: SidebarFriend[] = []; - const sendouqFriends: SidebarFriend[] = []; - const tournamentSubFriends: SidebarFriend[] = []; - const inactiveFriends: FriendWithActivity[] = []; + return orderSidebarFriends(entries).map(({ row, activity }) => + rowToSidebarFriend(row, activity), + ); +} - for (const friend of friendRows) { - const activity = activityForRow(friend); +type OrderableFriend = { + /** Mutual friend rather than a team member shown alongside them. */ + isFriend: boolean; + isPinned: boolean; + activityType: FriendActivityType | null; +}; - if (!activity.type) { - inactiveFriends.push(friend); - continue; - } +/** Pinned friends with activity, the rest with activity, pinned idle friends, the rest, truncated to what fits. Each section keeps its own order of activity type quotas, friends before team members. */ +export function orderSidebarFriends(entries: T[]) { + const friends = entries.filter(({ isFriend }) => isFriend); + const teamMembers = entries.filter(({ isFriend }) => !isFriend); - const sidebarFriend = rowToSidebarFriend(friend, activity); + const sendouqFriends = friends.filter( + ({ activityType }) => activityType === "SENDOUQ", + ); + const tournamentSubFriends = friends.filter( + ({ activityType }) => activityType === "TOURNAMENT_SUB", + ); + const inMatchFriends = friends.filter(({ activityType }) => + isInProgressFriendActivity(activityType), + ); + const idleFriends = friends.filter(({ activityType }) => !activityType); + const activeTeamMembers = teamMembers.filter( + ({ activityType }) => activityType, + ); + const idleTeamMembers = teamMembers.filter( + ({ activityType }) => !activityType, + ); - if (isInProgressFriendActivity(activity.type)) { - activeFriends.push(sidebarFriend); - } else if (activity.type === "SENDOUQ") { - sendouqFriends.push(sidebarFriend); - } else { - tournamentSubFriends.push(sidebarFriend); - } - } + const ordered = [ + ...sendouqFriends.slice(0, SENDOUQ_QUOTA), + ...tournamentSubFriends.slice(0, TOURNAMENT_SUB_QUOTA), + ...sendouqFriends.slice(SENDOUQ_QUOTA), + ...tournamentSubFriends.slice(TOURNAMENT_SUB_QUOTA), + ...inMatchFriends, + ...activeTeamMembers, + ...idleFriends, + ...idleTeamMembers, + ]; - const result: SidebarFriend[] = []; - - const sendouqToShow = sendouqFriends.slice(0, SENDOUQ_QUOTA); - const tournamentToShow = tournamentSubFriends.slice(0, TOURNAMENT_SUB_QUOTA); - - result.push(...sendouqToShow, ...tournamentToShow); - - const remaining = MAX_FRIENDS_VISIBLE - result.length; - if (remaining > 0) { - const extraSendouq = sendouqFriends.slice(SENDOUQ_QUOTA); - const extraTournament = tournamentSubFriends.slice(TOURNAMENT_SUB_QUOTA); - result.push(...[...extraSendouq, ...extraTournament].slice(0, remaining)); - } - - if (result.length < MAX_FRIENDS_VISIBLE) { - result.push(...activeFriends.slice(0, MAX_FRIENDS_VISIBLE - result.length)); - } - - if (result.length < MAX_FRIENDS_VISIBLE) { - const shownIds = new Set(result.map((f) => f.id)); - const inactiveTeamMembers: FriendWithActivity[] = []; - - for (const tm of teamMemberRows) { - if (result.length >= MAX_FRIENDS_VISIBLE) break; - if (shownIds.has(tm.id)) continue; - - const activity = activityForRow(tm); - if (!activity.type) { - inactiveTeamMembers.push(tm); - continue; - } - - result.push(rowToSidebarFriend(tm, activity)); - shownIds.add(tm.id); - } - - for (const friend of inactiveFriends) { - if (result.length >= MAX_FRIENDS_VISIBLE) break; - if (shownIds.has(friend.id)) continue; - - result.push(rowToSidebarFriend(friend, null)); - shownIds.add(friend.id); - } - - for (const tm of inactiveTeamMembers) { - if (result.length >= MAX_FRIENDS_VISIBLE) break; - - result.push(rowToSidebarFriend(tm, null)); - } - } - - return result.slice(0, MAX_FRIENDS_VISIBLE); + return R.sortBy(ordered, friendSectionSortValue).slice( + 0, + MAX_FRIENDS_VISIBLE, + ); } function rowToSidebarFriend( row: FriendWithActivity, - activity: FriendActivity | null, + activity: FriendActivity, ): SidebarFriend { return { id: row.id, @@ -433,12 +412,12 @@ function rowToSidebarFriend( discordAvatar: row.discordAvatar, customAvatarUrl: row.customAvatarUrl, url: userPage({ discordId: row.discordId, customUrl: row.customUrl }), - subtitle: activity?.subtitle ?? "", - badge: activity?.badge ?? "", - activityType: activity?.type ?? null, - matchId: activity?.matchId ?? null, - tournamentId: activity?.tournamentId ?? row.tournamentId, - streamUrl: activity?.streamUrl ?? null, + subtitle: activity.subtitle ?? "", + badge: activity.badge ?? "", + activityType: activity.type, + matchId: activity.matchId, + tournamentId: activity.tournamentId ?? row.tournamentId, + streamUrl: activity.streamUrl, }; } diff --git a/app/utils/schema.ts b/app/utils/schema.ts index b3edbc479..1d63266ba 100644 --- a/app/utils/schema.ts +++ b/app/utils/schema.ts @@ -510,6 +510,11 @@ export function checkboxValueToBoolean(value: unknown) { export const _action = (value: T) => preprocess(deduplicate, v.literal(value)); +/** Marker field asking the root loader to revalidate too (see `shouldRevalidate` in root.tsx), `true` in a JSON body and `"true"` in form data. */ +export const revalidateRoot = v.optional( + v.nullable(v.union([v.literal(true), v.literal("true")])), +); + /** Works around a bug at least in Safari 15 where a SubmitButton value might get sent twice */ export function deduplicate(value: unknown) { if (Array.isArray(value)) { diff --git a/changelog/2026-09-20-pin-friends.md b/changelog/2026-09-20-pin-friends.md new file mode 100644 index 000000000..8914c7338 --- /dev/null +++ b/changelog/2026-09-20-pin-friends.md @@ -0,0 +1,4 @@ +--- +type: feature +--- +Friends can be pinned from the friends page, keeping them at the top of the friends list and the sidebar diff --git a/changelog/2026-09-20-tab-scroll-reset.md b/changelog/2026-09-20-tab-scroll-reset.md new file mode 100644 index 000000000..54a61eaee --- /dev/null +++ b/changelog/2026-09-20-tab-scroll-reset.md @@ -0,0 +1,5 @@ +--- +navItem: calendar +type: bug +--- +Switching tabs on the friends and events pages no longer scrolls back to the top diff --git a/e2e/friends.spec.ts b/e2e/friends.spec.ts index 13ab4baf7..2434fc72f 100644 --- a/e2e/friends.spec.ts +++ b/e2e/friends.spec.ts @@ -17,6 +17,7 @@ import { } from "./helpers/sidebar"; import { FriendsPage } from "./pages/friends/friends-page"; import { NotificationPopover } from "./pages/layout/notification-popover"; +import { SideNav } from "./pages/layout/side-nav"; const WEDNESDAY = 2; @@ -114,6 +115,64 @@ test.describe("Friends", () => { await friends.locators.nextWeekToggle.click(); await expect(friends.locators.noScheduleText).toBeVisible(); }); + + test("pinned friends sort to the top of their section on the page and in the sidebar", async ({ + page, + factories, + }) => { + const [pinnedQueueing, queueing, pinnedIdle, idle] = await createNamedUsers( + factories, + ["PinQueue", "PlainQueue", "PinIdle", "PlainIdle"], + ); + await befriend( + factories, + [idle.id, pinnedIdle.id, queueing.id, pinnedQueueing.id], + ADMIN_ID, + ); + await factories.SQGroupFactory.create({ memberUserIds: [queueing.id] }); + await factories.SQGroupFactory.create({ + memberUserIds: [pinnedQueueing.id], + }); + + await impersonate(page, ADMIN_ID); + + const friends = new FriendsPage(page); + await friends.goto(); + + await friends.togglePin(pinnedIdle.id); + await friends.togglePin(pinnedQueueing.id); + await expect(friends.pinButton(pinnedIdle.id)).toHaveAttribute( + "aria-pressed", + "true", + ); + await expect(friends.pinButton(idle.id)).toHaveAttribute( + "aria-pressed", + "false", + ); + + await expectTopToBottom([ + friends.row(pinnedQueueing.id), + friends.row(queueing.id), + friends.row(pinnedIdle.id), + friends.row(idle.id), + ]); + + // the sidebar refreshes with the page, no navigation needed + const sideNav = new SideNav(page); + await expectTopToBottom([ + sideNav.friend("PinQueue").trigger, + sideNav.friend("PlainQueue").trigger, + sideNav.friend("PinIdle").trigger, + sideNav.friend("PlainIdle").trigger, + ]); + + await friends.goto(); + await friends.togglePin(pinnedIdle.id); + await expect(friends.pinButton(pinnedIdle.id)).toHaveAttribute( + "aria-pressed", + "false", + ); + }); }); function daySlot(dayIndex: number, start: string, end: string) { diff --git a/e2e/pages/friends/friends-page.ts b/e2e/pages/friends/friends-page.ts index 66b8fa1f1..66f73be97 100644 --- a/e2e/pages/friends/friends-page.ts +++ b/e2e/pages/friends/friends-page.ts @@ -52,9 +52,11 @@ export class FriendsPage { ); } - /** Scoped to the page content: the mobile friends panel still shows the same name for a frame after navigating here from it. */ + /** Scoped to the page content: the mobile friends panel still shows the same name for a frame after navigating here from it. Exact, as the pin button's label names the friend too. */ friendButton(name: string) { - return this.page.getByRole("main").getByRole("button", { name }); + return this.page + .getByRole("main") + .getByRole("button", { name, exact: true }); } friend(name: string) { @@ -69,6 +71,14 @@ export class FriendsPage { return this.page.getByTestId(`friend-schedule-button-${userId}`); } + pinButton(userId: number) { + return this.page.getByTestId(`friend-pin-button-${userId}`); + } + + async togglePin(userId: number) { + await waitForPOSTResponse(this.page, () => this.pinButton(userId).click()); + } + /** One day row of the open week modal, Monday being 0. */ day(dayIndex: number) { return this.locators.scheduleDays.getByRole("listitem").nth(dayIndex); @@ -82,8 +92,11 @@ class FriendMenu { constructor(page: Page, name: string) { this.page = page; // scoped to the page content because the sidebar's friends section shows - // a button with the same name once the friendship data refreshes - this.trigger = page.getByRole("main").getByRole("button", { name }); + // a button with the same name once the friendship data refreshes, exact + // because the pin button's label names the friend too + this.trigger = page + .getByRole("main") + .getByRole("button", { name, exact: true }); } async deleteFriend() { diff --git a/locales/da/friends.json b/locales/da/friends.json index c6a2e1a7e..94d0e6267 100644 --- a/locales/da/friends.json +++ b/locales/da/friends.json @@ -21,5 +21,7 @@ "view.all": "", "teamMembers.empty": "", "unseenRequests_one": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/de/friends.json b/locales/de/friends.json index c6a2e1a7e..94d0e6267 100644 --- a/locales/de/friends.json +++ b/locales/de/friends.json @@ -21,5 +21,7 @@ "view.all": "", "teamMembers.empty": "", "unseenRequests_one": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/en/friends.json b/locales/en/friends.json index 9041ef6e9..5dfcbfd27 100644 --- a/locales/en/friends.json +++ b/locales/en/friends.json @@ -21,5 +21,7 @@ "view.all": "All", "teamMembers.empty": "No team members yet", "unseenRequests_one": "{{count}} unseen friend request", - "unseenRequests_other": "{{count}} unseen friend requests" + "unseenRequests_other": "{{count}} unseen friend requests", + "friendsList.pin": "Pin {{name}} to the top", + "friendsList.unpin": "Unpin {{name}}" } diff --git a/locales/es-ES/friends.json b/locales/es-ES/friends.json index 2a716040e..5618ab94b 100644 --- a/locales/es-ES/friends.json +++ b/locales/es-ES/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "Aún no hay miembros en el equipo", "unseenRequests_one": "{{count}} solicitud de amistad sin ver", "unseenRequests_many": "", - "unseenRequests_other": "{{count}} solicitudes de amistad sin ver" + "unseenRequests_other": "{{count}} solicitudes de amistad sin ver", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/es-US/friends.json b/locales/es-US/friends.json index 2a716040e..5618ab94b 100644 --- a/locales/es-US/friends.json +++ b/locales/es-US/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "Aún no hay miembros en el equipo", "unseenRequests_one": "{{count}} solicitud de amistad sin ver", "unseenRequests_many": "", - "unseenRequests_other": "{{count}} solicitudes de amistad sin ver" + "unseenRequests_other": "{{count}} solicitudes de amistad sin ver", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/fr-CA/friends.json b/locales/fr-CA/friends.json index c56663f98..666eb8792 100644 --- a/locales/fr-CA/friends.json +++ b/locales/fr-CA/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "", "unseenRequests_one": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/fr-EU/friends.json b/locales/fr-EU/friends.json index c56663f98..666eb8792 100644 --- a/locales/fr-EU/friends.json +++ b/locales/fr-EU/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "", "unseenRequests_one": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/he/friends.json b/locales/he/friends.json index a73596580..f757ec75e 100644 --- a/locales/he/friends.json +++ b/locales/he/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "", "unseenRequests_one": "", "unseenRequests_two": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/it/friends.json b/locales/it/friends.json index c56663f98..666eb8792 100644 --- a/locales/it/friends.json +++ b/locales/it/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "", "unseenRequests_one": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/ja/friends.json b/locales/ja/friends.json index 7ee6b8c8a..4364c2c48 100644 --- a/locales/ja/friends.json +++ b/locales/ja/friends.json @@ -19,5 +19,7 @@ "view.friends": "", "view.teamMembers": "", "view.all": "", - "teamMembers.empty": "" + "teamMembers.empty": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/ko/friends.json b/locales/ko/friends.json index 7ee6b8c8a..4364c2c48 100644 --- a/locales/ko/friends.json +++ b/locales/ko/friends.json @@ -19,5 +19,7 @@ "view.friends": "", "view.teamMembers": "", "view.all": "", - "teamMembers.empty": "" + "teamMembers.empty": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/nl/friends.json b/locales/nl/friends.json index c6a2e1a7e..94d0e6267 100644 --- a/locales/nl/friends.json +++ b/locales/nl/friends.json @@ -21,5 +21,7 @@ "view.all": "", "teamMembers.empty": "", "unseenRequests_one": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/pl/friends.json b/locales/pl/friends.json index 6e35b7508..b8b030566 100644 --- a/locales/pl/friends.json +++ b/locales/pl/friends.json @@ -23,5 +23,7 @@ "unseenRequests_one": "", "unseenRequests_few": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/pt-BR/friends.json b/locales/pt-BR/friends.json index c56663f98..666eb8792 100644 --- a/locales/pt-BR/friends.json +++ b/locales/pt-BR/friends.json @@ -22,5 +22,7 @@ "teamMembers.empty": "", "unseenRequests_one": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/ru/friends.json b/locales/ru/friends.json index 6e35b7508..b8b030566 100644 --- a/locales/ru/friends.json +++ b/locales/ru/friends.json @@ -23,5 +23,7 @@ "unseenRequests_one": "", "unseenRequests_few": "", "unseenRequests_many": "", - "unseenRequests_other": "" + "unseenRequests_other": "", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/locales/zh/friends.json b/locales/zh/friends.json index 123ff8bba..7ab4096b3 100644 --- a/locales/zh/friends.json +++ b/locales/zh/friends.json @@ -20,5 +20,7 @@ "view.teamMembers": "队伍成员", "view.all": "全部", "teamMembers.empty": "暂无队伍成员", - "unseenRequests": "有 {{count}} 条未查看的好友请求" + "unseenRequests": "有 {{count}} 条未查看的好友请求", + "friendsList.pin": "", + "friendsList.unpin": "" } diff --git a/migrations/20260920115346-friendship-pinned.ts b/migrations/20260920115346-friendship-pinned.ts new file mode 100644 index 000000000..64959a910 --- /dev/null +++ b/migrations/20260920115346-friendship-pinned.ts @@ -0,0 +1,19 @@ +import type { Kysely } from "kysely"; + +/** Either side of a friendship can pin it to the top of their friends list */ +export async function up(db: Kysely): Promise { + await db.transaction().execute(async (trx) => { + await trx.schema + .alterTable("Friendship") + .addColumn("isPinnedByUserOne", "integer", (col) => + col.notNull().defaultTo(0), + ) + .execute(); + await trx.schema + .alterTable("Friendship") + .addColumn("isPinnedByUserTwo", "integer", (col) => + col.notNull().defaultTo(0), + ) + .execute(); + }); +}