|
| 1 | +<script setup lang="ts"> |
| 2 | +import { updateProfile as updateProfileUtil } from '~/utils/atproto/profile' |
| 3 | +
|
| 4 | +const route = useRoute('profile-handle') |
| 5 | +const handle = computed(() => route.params.handle) |
| 6 | +
|
| 7 | +const { data: profile, error: profileError } = await useFetch<NPMXProfile>( |
| 8 | + () => `/api/social/profile/${handle.value}`, |
| 9 | + { |
| 10 | + default: () => ({ |
| 11 | + displayName: handle.value, |
| 12 | + description: '', |
| 13 | + website: '', |
| 14 | + recordExists: false, |
| 15 | + }), |
| 16 | + }, |
| 17 | +) |
| 18 | +if (!profile.value || profileError.value?.statusCode === 404) { |
| 19 | + throw createError({ |
| 20 | + statusCode: 404, |
| 21 | + statusMessage: $t('profile.not_found'), |
| 22 | + message: $t('profile.not_found_message', { handle: handle.value }), |
| 23 | + }) |
| 24 | +} |
| 25 | +
|
| 26 | +const { user } = useAtproto() |
| 27 | +const isEditing = ref(false) |
| 28 | +const displayNameInput = ref() |
| 29 | +const descriptionInput = ref() |
| 30 | +const websiteInput = ref() |
| 31 | +const isUpdateProfileActionPending = ref(false) |
| 32 | +
|
| 33 | +watchEffect(() => { |
| 34 | + if (isEditing.value) { |
| 35 | + if (profile) { |
| 36 | + displayNameInput.value = profile.value.displayName |
| 37 | + descriptionInput.value = profile.value.description |
| 38 | + websiteInput.value = profile.value.website |
| 39 | + } |
| 40 | + } |
| 41 | +}) |
| 42 | +
|
| 43 | +async function updateProfile() { |
| 44 | + if (!user.value?.handle || !displayNameInput.value) { |
| 45 | + return |
| 46 | + } |
| 47 | +
|
| 48 | + isUpdateProfileActionPending.value = true |
| 49 | + const currentProfile = profile.value |
| 50 | +
|
| 51 | + // optimistic update |
| 52 | + profile.value = { |
| 53 | + displayName: displayNameInput.value, |
| 54 | + description: descriptionInput.value || undefined, |
| 55 | + website: websiteInput.value || undefined, |
| 56 | + recordExists: true, |
| 57 | + } |
| 58 | +
|
| 59 | + try { |
| 60 | + const result = await updateProfileUtil(handle.value, { |
| 61 | + displayName: displayNameInput.value, |
| 62 | + description: descriptionInput.value || undefined, |
| 63 | + website: websiteInput.value || undefined, |
| 64 | + }) |
| 65 | +
|
| 66 | + if (result.success) { |
| 67 | + isEditing.value = false |
| 68 | + } else { |
| 69 | + profile.value = currentProfile |
| 70 | + } |
| 71 | +
|
| 72 | + isUpdateProfileActionPending.value = false |
| 73 | + } catch (e) { |
| 74 | + profile.value = currentProfile |
| 75 | + isUpdateProfileActionPending.value = false |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +const { data: likes, status } = useProfileLikes(handle) |
| 80 | +
|
| 81 | +const showInviteSection = computed(() => { |
| 82 | + return ( |
| 83 | + profile.value.recordExists === false && |
| 84 | + status.value === 'success' && |
| 85 | + !likes.value?.records?.length && |
| 86 | + user.value?.handle !== handle.value |
| 87 | + ) |
| 88 | +}) |
| 89 | +
|
| 90 | +const inviteUrl = computed(() => { |
| 91 | + const text = $t('profile.invite.compose_text', { handle: handle.value }) |
| 92 | + return `https://bsky.app/intent/compose?text=${encodeURIComponent(text)}` |
| 93 | +}) |
| 94 | +
|
| 95 | +useSeoMeta({ |
| 96 | + title: () => $t('profile.seo_title', { handle: handle.value }), |
| 97 | + description: () => $t('profile.seo_description', { handle: handle.value }), |
| 98 | +}) |
| 99 | +
|
| 100 | +/** |
| 101 | +defineOgImageComponent('Default', { |
| 102 | + title: () => `~${username.value}`, |
| 103 | + description: () => (results.value ? `${results.value.total} packages` : 'npm user profile'), |
| 104 | + primaryColor: '#60a5fa', |
| 105 | +}) |
| 106 | +**/ |
| 107 | +</script> |
| 108 | + |
| 109 | +<template> |
| 110 | + <main class="container flex-1 flex flex-col py-8 sm:py-12 w-full"> |
| 111 | + <!-- Header --> |
| 112 | + <header class="mb-8 pb-8 border-b border-border"> |
| 113 | + <!-- Editing Profile --> |
| 114 | + <div v-if="isEditing" class="flex flex-col flex-wrap gap-4"> |
| 115 | + <label for="displayName" class="text-sm flex flex-col gap-2"> |
| 116 | + {{ $t('profile.display_name') }} |
| 117 | + <input |
| 118 | + required |
| 119 | + name="displayName" |
| 120 | + type="text" |
| 121 | + class="w-full min-w-25 bg-bg-subtle border border-border rounded-md ps-3 pe-3 py-1.5 font-mono text-sm text-fg placeholder:text-fg-subtle transition-[border-color,outline-color] duration-300 hover:border-fg-subtle outline-2 outline-transparent focus:border-accent focus-visible:(outline-2 outline-accent/70)" |
| 122 | + v-model="displayNameInput" |
| 123 | + /> |
| 124 | + </label> |
| 125 | + <label for="description" class="text-sm flex flex-col gap-2"> |
| 126 | + {{ $t('profile.description') }} |
| 127 | + <input |
| 128 | + name="description" |
| 129 | + type="text" |
| 130 | + :placeholder="$t('profile.no_description')" |
| 131 | + v-model="descriptionInput" |
| 132 | + class="w-full min-w-25 bg-bg-subtle border border-border rounded-md ps-3 pe-3 py-1.5 font-mono text-sm text-fg placeholder:text-fg-subtle transition-[border-color,outline-color] duration-300 hover:border-fg-subtle outline-2 outline-transparent focus:border-accent focus-visible:(outline-2 outline-accent/70)" |
| 133 | + /> |
| 134 | + </label> |
| 135 | + <label for="website" class="text-sm flex flex-col gap-2"> |
| 136 | + {{ $t('profile.website') }} |
| 137 | + <input |
| 138 | + name="website" |
| 139 | + type="url" |
| 140 | + :placeholder="$t('profile.website_placeholder')" |
| 141 | + v-model="websiteInput" |
| 142 | + class="w-full min-w-25 bg-bg-subtle border border-border rounded-md ps-3 pe-3 py-1.5 font-mono text-sm text-fg placeholder:text-fg-subtle transition-[border-color,outline-color] duration-300 hover:border-fg-subtle outline-2 outline-transparent focus:border-accent focus-visible:(outline-2 outline-accent/70)" |
| 143 | + /> |
| 144 | + </label> |
| 145 | + <div class="flex gap-4 items-center font-mono text-sm"> |
| 146 | + <h2>@{{ handle }}</h2> |
| 147 | + <ButtonBase @click="isEditing = false"> |
| 148 | + {{ $t('common.cancel') }} |
| 149 | + </ButtonBase> |
| 150 | + <ButtonBase |
| 151 | + @click="updateProfile" |
| 152 | + variant="primary" |
| 153 | + :disabled="isUpdateProfileActionPending" |
| 154 | + > |
| 155 | + {{ $t('common.save') }} |
| 156 | + </ButtonBase> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + |
| 160 | + <!-- Display Profile --> |
| 161 | + <div v-else class="flex flex-col flex-wrap gap-4"> |
| 162 | + <h1 v-if="profile.displayName" class="font-mono text-2xl sm:text-3xl font-medium"> |
| 163 | + {{ profile.displayName }} |
| 164 | + </h1> |
| 165 | + <p v-if="profile.description">{{ profile.description }}</p> |
| 166 | + <div class="flex gap-4 items-center font-mono text-sm"> |
| 167 | + <h2>@{{ handle }}</h2> |
| 168 | + <LinkBase v-if="profile.website" :to="profile.website" classicon="i-lucide:link"> |
| 169 | + {{ profile.website }} |
| 170 | + </LinkBase> |
| 171 | + <ButtonBase |
| 172 | + @click="isEditing = true" |
| 173 | + :class="user?.handle === handle ? '' : 'invisible'" |
| 174 | + class="hidden sm:inline-flex" |
| 175 | + > |
| 176 | + {{ $t('common.edit') }} |
| 177 | + </ButtonBase> |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + </header> |
| 181 | + |
| 182 | + <section class="flex flex-col gap-8"> |
| 183 | + <h2 |
| 184 | + class="font-mono text-2xl sm:text-3xl font-medium min-w-0 break-words" |
| 185 | + :title="$t('profile.likes')" |
| 186 | + dir="ltr" |
| 187 | + > |
| 188 | + {{ $t('profile.likes') }} |
| 189 | + <span v-if="likes">({{ likes.records?.length ?? 0 }})</span> |
| 190 | + </h2> |
| 191 | + <div v-if="status === 'pending'" class="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 192 | + <SkeletonBlock v-for="i in 4" :key="i" class="h-16 rounded-lg" /> |
| 193 | + </div> |
| 194 | + <div v-else-if="status === 'error'"> |
| 195 | + <p>{{ $t('common.error') }}</p> |
| 196 | + </div> |
| 197 | + <div v-else-if="likes?.records" class="grid grid-cols-1 lg:grid-cols-2 gap-4"> |
| 198 | + <PackageLikeCard v-for="like in likes.records" :packageUrl="like.value.subjectRef" /> |
| 199 | + </div> |
| 200 | + |
| 201 | + <!-- Invite section: shown when user does not have npmx profile or any like lexicons --> |
| 202 | + <div |
| 203 | + v-if="showInviteSection" |
| 204 | + class="flex flex-col items-start gap-4 p-6 bg-bg-subtle border border-border rounded-lg" |
| 205 | + > |
| 206 | + <p class="text-fg-muted"> |
| 207 | + {{ $t('profile.invite.message') }} |
| 208 | + </p> |
| 209 | + <LinkBase variant="button-secondary" classicon="i-simple-icons:bluesky" :to="inviteUrl"> |
| 210 | + {{ $t('profile.invite.share_button') }} |
| 211 | + </LinkBase> |
| 212 | + </div> |
| 213 | + </section> |
| 214 | + </main> |
| 215 | +</template> |
0 commit comments