Skip to content

Commit 551e01f

Browse files
committed
Should be proper read of likes on ui
1 parent e708ea5 commit 551e01f

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

app/pages/package/[...package].vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,27 @@ const canonicalUrl = computed(() => {
352352
return requestedVersion.value ? `${base}/v/${requestedVersion.value}` : base
353353
})
354354
355+
//atproto
356+
const { user } = useAtproto()
357+
const showAuthModal = ref(false)
358+
359+
const { data: likesData } = useFetch(() => `/api/likes/${packageName.value}`, {
360+
default: () => ({ totalPackageLikes: 0, userHasLiked: false }),
361+
})
362+
363+
// const { mutate: likePackage } = useLikePackage(subjectRef)
364+
365+
const likeAction = async () => {
366+
if (user.value?.handle == null) {
367+
showAuthModal.value = true
368+
} else {
369+
// const result = await likePackage()
370+
// if (result?.likes) {
371+
// likesData.value = result.likes
372+
// }
373+
}
374+
}
375+
355376
useHead({
356377
link: [{ rel: 'canonical', href: canonicalUrl }],
357378
})
@@ -501,6 +522,26 @@ defineOgImageComponent('Package', {
501522
</template>
502523
</ClientOnly>
503524

525+
<button
526+
@click="likeAction"
527+
type="button"
528+
class="inline-flex items-center gap-1.5 font-mono text-sm text-fg hover:text-fg-muted transition-colors duration-200"
529+
:title="$t('package.links.like')"
530+
>
531+
<span
532+
:class="
533+
likesData?.userHasLiked
534+
? 'i-lucide-heart-minus text-red-500'
535+
: 'i-lucide-heart-plus'
536+
"
537+
class="w-4 h-4"
538+
aria-hidden="true"
539+
/>
540+
<span>{{
541+
formatCompactNumber(likesData?.totalPackageLikes ?? 0, { decimals: 1 })
542+
}}</span>
543+
</button>
544+
504545
<!-- Internal navigation: Docs + Code + Compare (hidden on mobile, shown in external links instead) -->
505546
<nav
506547
v-if="resolvedVersion"

server/utils/atproto/utils/likes.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getCacheAdatper } from '../../cache'
22
import { $nsid as likeNsid } from '#shared/types/lexicons/dev/npmx/feed/like.defs'
3-
import { SUBJECT_REF_PREFIX } from '~~/shared/utils/constants'
43

54
/**
65
* Likes for a npm package on npmx
@@ -41,8 +40,6 @@ export class PackageLikesUtils {
4140
'.subjectRef',
4241
//Limit doesn't matter here since we are just counting the total likes
4342
1,
44-
undefined,
45-
CACHE_MAX_AGE_ONE_MINUTE * 10,
4643
)
4744
return totalLinks.total
4845
}
@@ -77,7 +74,7 @@ export class PackageLikesUtils {
7774
async getLikes(packageName: string, usersDid?: string | undefined) {
7875
//TODO: May need to do some clean up on the package name, and maybe even hash it? some of the charcteres may be a bit odd as keys
7976
const totalLikesKey = CACHE_PACKAGE_TOTAL_KEY(packageName)
80-
const subjectRef = `${SUBJECT_REF_PREFIX}/${packageName}`
77+
const subjectRef = PACKAGE_SUBJECT_REF(packageName)
8178

8279
const cachedLikes = await this.cache.get<number>(totalLikesKey)
8380
let totalLikes = 0
@@ -120,10 +117,9 @@ export class PackageLikesUtils {
120117
if (cached !== undefined) {
121118
return cached
122119
}
123-
const userHasLiked = await this.constellationUserHasLiked(
124-
`${SUBJECT_REF_PREFIX}/${packageName}`,
125-
usersDid,
126-
)
120+
const subjectRef = PACKAGE_SUBJECT_REF(packageName)
121+
122+
const userHasLiked = await this.constellationUserHasLiked(subjectRef, usersDid)
127123
await this.cache.set(CACHE_USER_LIKES_KEY(packageName, usersDid), userHasLiked, CACHE_MAX_AGE)
128124
return userHasLiked
129125
}
@@ -141,7 +137,7 @@ export class PackageLikesUtils {
141137
if (totalLikes !== undefined) {
142138
await this.cache.set(totalLikesKey, totalLikes + 1, CACHE_MAX_AGE)
143139
} else {
144-
const subjectRef = `${SUBJECT_REF_PREFIX}/${packageName}`
140+
const subjectRef = PACKAGE_SUBJECT_REF(packageName)
145141
totalLikes = await this.constellationLikes(subjectRef)
146142
}
147143
// We already know the user has not liked the package so set in the cache

shared/utils/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export const CONSTELLATION_HOST = 'constellation.microcosm.blue'
3131
export const SLINGSHOT_HOST = 'slingshot.microcosm.blue'
3232

3333
// ATProtocol
34-
// Refrence prefix used to link packages to things that are not inherently atproto
35-
export const SUBJECT_REF_PREFIX = 'https://npmx.dev'
34+
// Refrences used to link packages to things that are not inherently atproto
35+
export const PACKAGE_SUBJECT_REF = (packageName: string) =>
36+
`https://npmx.dev/package/${packageName}`
3637

3738
// Theming
3839
export const ACCENT_COLORS = {

0 commit comments

Comments
 (0)