frontend/composables/profile.ts
Temmie 10bf54b6fd
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
ci/woodpecker/pr/build-and-publish Pipeline was successful
refactor: move getDisplayAvatar into profile composable
2025-08-11 22:25:16 +02:00

113 lines
2.6 KiB
TypeScript

import type { GuildMemberResponse, UserResponse } from "~/types/interfaces"
const { fetchFriends } = useApi();
export const useProfile = () => {
function getAboutMe(profile: UserResponse | GuildMemberResponse): string | null {
if ("username" in profile) {
return profile.about
} else {
return profile.user.about
}
}
function getDisplayName(profile: UserResponse | GuildMemberResponse): string {
if ("username" in profile) {
// assume it's a UserResponse
if (profile.display_name) return profile.display_name
return profile.username
} else {
// assume it's a GuildMemberResponse
if (profile.nickname) return profile.nickname
if (profile.user.display_name) return profile.user.display_name
return profile.user.username
}
}
function getAvatarUrl(profile: UserResponse | GuildMemberResponse): string | null {
if ("username" in profile) {
return profile.avatar
} else {
return profile.user.avatar
}
}
async function getFriendsSince(profile: UserResponse | GuildMemberResponse): Promise<Date | null> {
let user_uuid: string;
if ("username" in profile) {
user_uuid = profile.uuid
} else {
user_uuid = profile.user.uuid
}
const friends = await fetchFriends()
const friend = friends.find(friend => friend.uuid === user_uuid);
if (friend?.friends_since) {
return new Date(friend.friends_since);
}
return null
}
function getGuildJoinDate(profile: UserResponse | GuildMemberResponse): Date | undefined {
if ("username" in profile) {
return undefined
} else {
return uuidToDate(profile.uuid)
}
}
function getPronouns(profile: UserResponse | GuildMemberResponse): string | null {
if ("username" in profile) {
return profile.pronouns
} else {
return profile.user.pronouns
}
}
function getRegistrationDate(profile: UserResponse | GuildMemberResponse): Date {
if ("username" in profile) {
return uuidToDate(profile.uuid)
} else {
return uuidToDate(profile.user.uuid)
}
}
function getUsername(profile: UserResponse | GuildMemberResponse): string {
if ("username" in profile) {
return profile.username
} else {
return profile.user.username
}
}
function getUserUuid(profile: UserResponse | GuildMemberResponse): string {
if ("username" in profile) {
return profile.uuid
} else {
return profile.user.uuid
}
}
function getUser(profile: UserResponse | GuildMemberResponse): UserResponse {
if ("username" in profile) {
return profile
} else {
return profile.user
}
}
return {
getAboutMe,
getDisplayName,
getAvatarUrl,
getFriendsSince,
getGuildJoinDate,
getRegistrationDate,
getPronouns,
getUsername,
getUserUuid,
getUser
}
}