Skip to content

Commit 0a8b723

Browse files
committed
optimistically update star count
1 parent 4adb000 commit 0a8b723

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
@@ -476,7 +476,10 @@ const repositoryUrl = computed(() => {
476476
const { meta: repoMeta, repoRef, stars, starsLink, forks, forksLink } = useRepoMeta(repositoryUrl)
477477
478478
const { isConnected: isGitHubConnected } = useGitHub()
479-
const { isStarred, isStarActionPending, isGitHubRepo, toggleStar } = useGitHubStar(repoRef)
479+
const { isStarred, isStarActionPending, isGitHubRepo, starChange, toggleStar } =
480+
useGitHubStar(repoRef)
481+
482+
const displayStars = computed(() => Math.max(0, stars.value + starChange.value))
480483
481484
const PROVIDER_ICONS: Record<string, IconClass> = {
482485
github: 'i-simple-icons:github',
@@ -997,7 +1000,7 @@ const showSkeleton = shallowRef(false)
9971000
</li>
9981001
<li v-if="repositoryUrl && repoMeta && starsLink">
9991002
<LinkBase :to="starsLink" classicon="i-lucide:star">
1000-
{{ compactNumberFormatter.format(stars) }}
1003+
{{ compactNumberFormatter.format(displayStars) }}
10011004
</LinkBase>
10021005
</li>
10031006
<li v-if="forks && forksLink">

0 commit comments

Comments
 (0)