Files
gridpilot.gg/apps/website/lib/builders/view-data/ProfileViewDataBuilder.ts
2026-01-14 02:02:24 +01:00

113 lines
4.3 KiB
TypeScript

import type { GetDriverProfileOutputDTO } from '@/lib/types/generated/GetDriverProfileOutputDTO';
import type { ProfileViewData } from '@/lib/view-data/ProfileViewData';
import { mediaConfig } from '@/lib/config/mediaConfig';
import { CountryFlagDisplay } from '@/lib/display-objects/CountryFlagDisplay';
export class ProfileViewDataBuilder {
static build(apiDto: GetDriverProfileOutputDTO): ProfileViewData {
const driver = apiDto.currentDriver;
if (!driver) {
return {
driver: {
id: '',
name: '',
countryCode: '',
countryFlag: CountryFlagDisplay.fromCountryCode(null).toString(),
avatarUrl: mediaConfig.avatars.defaultFallback,
bio: null,
iracingId: null,
joinedAtLabel: '',
},
stats: null,
teamMemberships: [],
extendedProfile: null,
};
}
const stats = apiDto.stats ?? null;
const socialSummary = apiDto.socialSummary;
const extended = apiDto.extendedProfile ?? null;
const joinedAtLabel = new Date(driver.joinedAt).toLocaleDateString('en-US', {
month: 'short',
year: 'numeric',
});
return {
driver: {
id: driver.id,
name: driver.name,
countryCode: driver.country,
countryFlag: CountryFlagDisplay.fromCountryCode(driver.country).toString(),
avatarUrl: driver.avatarUrl || mediaConfig.avatars.defaultFallback,
bio: driver.bio || null,
iracingId: driver.iracingId || null,
joinedAtLabel,
},
stats: stats
? {
ratingLabel: stats.rating != null ? String(stats.rating) : '0',
globalRankLabel: driver.globalRank != null ? `#${driver.globalRank}` : '—',
totalRacesLabel: String(stats.totalRaces),
winsLabel: String(stats.wins),
podiumsLabel: String(stats.podiums),
dnfsLabel: String(stats.dnfs),
bestFinishLabel: stats.bestFinish != null ? `P${stats.bestFinish}` : '—',
worstFinishLabel: stats.worstFinish != null ? `P${stats.worstFinish}` : '—',
avgFinishLabel: stats.avgFinish != null ? `P${stats.avgFinish.toFixed(1)}` : '—',
consistencyLabel: stats.consistency != null ? `${stats.consistency}%` : '0%',
percentileLabel: stats.percentile != null ? `${stats.percentile}%` : '—',
}
: null,
teamMemberships: apiDto.teamMemberships.map((m) => ({
teamId: m.teamId,
teamName: m.teamName,
teamTag: m.teamTag || null,
roleLabel: m.role,
joinedAtLabel: new Date(m.joinedAt).toLocaleDateString('en-US', {
month: 'short',
year: 'numeric',
}),
href: `/teams/${m.teamId}`,
})),
extendedProfile: extended
? {
timezone: extended.timezone,
racingStyle: extended.racingStyle,
favoriteTrack: extended.favoriteTrack,
favoriteCar: extended.favoriteCar,
availableHours: extended.availableHours,
lookingForTeamLabel: extended.lookingForTeam ? 'Looking for Team' : '',
openToRequestsLabel: extended.openToRequests ? 'Open to Requests' : '',
socialHandles: extended.socialHandles.map((h) => ({
platformLabel: h.platform,
handle: h.handle,
url: h.url,
})),
achievements: extended.achievements.map((a) => ({
id: a.id,
title: a.title,
description: a.description,
earnedAtLabel: new Date(a.earnedAt).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
}),
icon: a.icon as NonNullable<ProfileViewData['extendedProfile']>['achievements'][number]['icon'],
rarityLabel: a.rarity,
})),
friends: socialSummary.friends.slice(0, 8).map((f) => ({
id: f.id,
name: f.name,
countryFlag: CountryFlagDisplay.fromCountryCode(f.country).toString(),
avatarUrl: f.avatarUrl || mediaConfig.avatars.defaultFallback,
href: `/drivers/${f.id}`,
})),
friendsCountLabel: String(socialSummary.friendsCount),
}
: null,
};
}
}