Skip to content

Commit f58f8b5

Browse files
committed
optimistically update star count
1 parent 5582c9c commit f58f8b5

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

app/composables/github/useGitHubStar.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function useGitHubStar(repoRef: Ref<RepoRef | null>) {
3838

3939
const isStarred = computed(() => starStatus.value?.starred ?? false)
4040
const isStarActionPending = shallowRef(false)
41+
const starChange = shallowRef(0)
4142

4243
async function toggleStar() {
4344
if (!shouldFetch.value || isStarActionPending.value) return
@@ -50,6 +51,7 @@ export function useGitHubStar(repoRef: Ref<RepoRef | null>) {
5051
connected: true,
5152
}
5253

54+
starChange.value = currentlyStarred ? 0 : 1
5355
isStarActionPending.value = true
5456

5557
try {
@@ -65,6 +67,8 @@ export function useGitHubStar(repoRef: Ref<RepoRef | null>) {
6567
starred: currentlyStarred,
6668
connected: true,
6769
}
70+
71+
starChange.value = 0
6872
} finally {
6973
isStarActionPending.value = false
7074
}
@@ -74,6 +78,7 @@ export function useGitHubStar(repoRef: Ref<RepoRef | null>) {
7478
isStarred,
7579
isStarActionPending,
7680
isGitHubRepo,
81+
starChange,
7782
toggleStar,
7883
}
7984
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,10 @@ const repositoryUrl = computed(() => {
400400
const { meta: repoMeta, repoRef, stars, starsLink, forks, forksLink } = useRepoMeta(repositoryUrl)
401401
402402
const { isConnected: isGitHubConnected } = useGitHub()
403-
const { isStarred, isStarActionPending, isGitHubRepo, toggleStar } = useGitHubStar(repoRef)
403+
const { isStarred, isStarActionPending, isGitHubRepo, starChange, toggleStar } =
404+
useGitHubStar(repoRef)
405+
406+
const displayStars = computed(() => Math.max(0, stars.value + starChange.value))
404407
405408
const PROVIDER_ICONS: Record<string, IconClass> = {
406409
github: 'i-simple-icons:github',
@@ -911,7 +914,7 @@ const showSkeleton = shallowRef(false)
911914
</li>
912915
<li v-if="repositoryUrl && repoMeta && starsLink">
913916
<LinkBase :to="starsLink" classicon="i-lucide:star">
914-
{{ compactNumberFormatter.format(stars) }}
917+
{{ compactNumberFormatter.format(displayStars) }}
915918
</LinkBase>
916919
</li>
917920
<li v-if="forks && forksLink">

0 commit comments

Comments
 (0)