Skip to content

Commit 4048ab6

Browse files
committed
refactor: usefetch + apply to orgs page too
1 parent d7fe2d8 commit 4048ab6

File tree

3 files changed

+38
-49
lines changed

3 files changed

+38
-49
lines changed

app/components/User/Avatar.vue

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script setup lang="ts">
2+
const props = defineProps<{
3+
username: string
4+
}>()
5+
6+
const { data: gravatarUrl } = useLazyFetch('/api/gravatar', {
7+
query: {
8+
username: computed(() => props.username),
9+
size: 128,
10+
},
11+
transform: res => res.url,
12+
})
13+
</script>
14+
15+
<template>
16+
<!-- Avatar -->
17+
<div
18+
class="size-16 shrink-0 rounded-full bg-bg-muted border border-border flex items-center justify-center overflow-hidden"
19+
role="img"
20+
:aria-label="`Avatar for ${username}`"
21+
>
22+
<!-- If Gravatar was fetched, display it -->
23+
<img
24+
v-if="gravatarUrl"
25+
:src="gravatarUrl"
26+
alt=""
27+
width="64"
28+
height="64"
29+
class="w-full h-full object-cover"
30+
/>
31+
<!-- Else fallback to initials -->
32+
<span v-else class="text-2xl text-fg-subtle font-mono" aria-hidden="true">
33+
{{ username.charAt(0).toUpperCase() }}
34+
</span>
35+
</div>
36+
</template>

app/pages/~[username]/index.vue

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ const router = useRouter()
66
77
const username = computed(() => route.params.username)
88
9-
async function fetchGravatarUrl(handle: string): Promise<string | null> {
10-
if (!handle) return null
11-
12-
try {
13-
const response = await $fetch<{ url: string | null }>(
14-
`/api/gravatar?username=${encodeURIComponent(handle)}&size=64`,
15-
)
16-
return response.url ?? null
17-
} catch {
18-
// Gravatar couldn't be fetched, it is ignored as not considered an error
19-
return null
20-
}
21-
}
22-
23-
const { data: gravatarUrl } = useLazyAsyncData(
24-
() => `gravatar:${username.value}`,
25-
() => fetchGravatarUrl(username.value),
26-
{ watch: [username] },
27-
)
28-
299
// Infinite scroll state
3010
const pageSize = 50
3111
const maxResults = 250 // npm API hard limit
@@ -199,26 +179,7 @@ defineOgImageComponent('Default', {
199179
<!-- Header -->
200180
<header class="mb-8 pb-8 border-b border-border">
201181
<div class="flex flex-wrap items-center gap-4">
202-
<!-- Avatar -->
203-
<div
204-
class="w-16 h-16 rounded-full bg-bg-muted border border-border flex items-center justify-center overflow-hidden"
205-
role="img"
206-
:aria-label="`Avatar for ${username}`"
207-
>
208-
<!-- If Gravatar was fetched, display it -->
209-
<img
210-
v-if="gravatarUrl"
211-
:src="gravatarUrl"
212-
alt=""
213-
width="64"
214-
height="64"
215-
class="w-full h-full object-cover"
216-
/>
217-
<!-- Else fallback to initials -->
218-
<span v-else class="text-2xl text-fg-subtle font-mono" aria-hidden="true">
219-
{{ username.charAt(0).toUpperCase() }}
220-
</span>
221-
</div>
182+
<UserAvatar :username="username" />
222183
<div>
223184
<h1 class="font-mono text-2xl sm:text-3xl font-medium">~{{ username }}</h1>
224185
<p v-if="results?.total" class="text-fg-muted text-sm mt-1">

app/pages/~[username]/orgs.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,7 @@ defineOgImageComponent('Default', {
120120
<!-- Header -->
121121
<header class="mb-8 pb-8 border-b border-border">
122122
<div class="flex flex-wrap items-center gap-4 mb-4">
123-
<!-- Avatar placeholder -->
124-
<div
125-
class="size-16 shrink-0 rounded-full bg-bg-muted border border-border flex items-center justify-center"
126-
aria-hidden="true"
127-
>
128-
<span class="text-2xl text-fg-subtle font-mono">{{
129-
username.charAt(0).toUpperCase()
130-
}}</span>
131-
</div>
123+
<UserAvatar :username="username" />
132124
<div>
133125
<h1 class="font-mono text-2xl sm:text-3xl font-medium">~{{ username }}</h1>
134126
<p class="text-fg-muted text-sm mt-1">{{ $t('user.orgs_page.title') }}</p>

0 commit comments

Comments
 (0)