@@ -21,7 +21,7 @@ const [owner, repoName] = repo.split('/')
2121 >
2222 <div
2323 id =' gh-avatar'
24- class =' gh-text me-2 size-7 flex-shrink-0 bg-cover sm:size-8'
24+ class =' load-block me-2 size-7 flex-shrink-0 bg-cover sm:size-8'
2525 style =' border-radius:999px'
2626 >
2727 </div >
@@ -39,9 +39,9 @@ const [owner, repoName] = repo.split('/')
3939 <Icon name =' github' />
4040 </div >
4141 </div >
42- <p id =' gh-description' class =' gh-text text-sm sm:text-base' >Waiting for api.github.com...</p >
42+ <p id =' gh-description' class =' load-block text-sm sm:text-base' >Waiting for api.github.com...</p >
4343 <div class =' flex items-center justify-between gap-x-2' >
44- <div class =' gh-text flex flex-wrap items-center gap-x-3 gap-y-1 sm:gap-x-5' >
44+ <div class =' load-block flex flex-wrap items-center gap-x-3 gap-y-1 sm:gap-x-5' >
4545 <div class =' flex items-center gap-x-1.5 sm:gap-x-2' >
4646 { /* mingcute:star-line */ }
4747 <!-- prettier-ignore -->
@@ -63,8 +63,7 @@ const [owner, repoName] = repo.split('/')
6363 <span id =' gh-license' class =' text-sm leading-tight sm:text-base' >???</span >
6464 </div >
6565 </div >
66-
67- <span id =' gh-language' class =' gh-text text-sm leading-tight sm:text-base' >?????</span >
66+ <span id =' gh-language' class =' load-block text-sm leading-tight sm:text-base' >?????</span >
6867 </div >
6968 </a >
7069</github-card>
@@ -81,19 +80,22 @@ const [owner, repoName] = repo.split('/')
8180 opacity: 1;
8281 }
8382 }
84- .loading .gh-text {
83+ .loading .load-block {
8584 color: transparent;
8685 border-radius: calc(var(--radius) - 3px);
8786 background-color: hsl(var(--primary-foreground) / var(--un-bg-opacity, 1));
8887 animation: pulsate 2s infinite linear;
8988 user-select: none;
9089 }
91- .loading .gh-text :nth-child(2) {
90+ .loading .load-block :nth-child(2) {
9291 animation-delay: 1s;
9392 }
9493 .no-license {
9594 opacity: 0.5;
9695 }
96+ :not(.loading) #gh-avatar {
97+ background-color: hsl(var(--primary-foreground) / var(--un-bg-opacity));
98+ }
9799</style >
98100
99101<script >
@@ -134,26 +136,22 @@ const [owner, repoName] = repo.split('/')
134136 try {
135137 const data = await this.fetchGithub(this.dataset.repo)
136138 if (!data) return
137- ;(this.querySelector('#gh-stars') as HTMLElement).textContent = this.numberFormat(
138- data.stargazers_count
139- )
140- ;(this.querySelector('#gh-forks') as HTMLElement).textContent = this.numberFormat(
141- data.forks
142- )
143- ;(this.querySelector('#gh-language') as HTMLElement).textContent = data.language || 'N/A'
144- ;(this.querySelector('#gh-description') as HTMLElement).textContent =
139+ // Update stats
140+ this.updateElement('#gh-stars', this.numberFormat(data.stargazers_count))
141+ this.updateElement('#gh-forks', this.numberFormat(data.forks))
142+ this.updateElement('#gh-language', data.language || 'N/A')
143+ // Update description
144+ const description =
145145 typeof data.description === 'string'
146146 ? data.description.replace(/:[a-zA-Z0-9_]+:/g, '')
147147 : 'Description not set'
148-
148+ this.updateElement('#gh-description', description)
149+ // Update license
150+ const licenseText = data.license?.spdx_id || 'N/A'
149151 const licenseEl = this.querySelector('#gh-license') as HTMLElement
150- if (data.license?.spdx_id) {
151- licenseEl.textContent = data.license.spdx_id
152- } else {
153- licenseEl.textContent = 'N/A'
154- licenseEl.classList.add('no-license')
155- }
156-
152+ licenseEl.textContent = licenseText
153+ licenseEl.classList.toggle('no-license', licenseText === 'N/A')
154+ // Update avatar
157155 const avatarEl = this.querySelector('#gh-avatar') as HTMLElement
158156 if (avatarEl) {
159157 avatarEl.style.backgroundImage = `url(${data.owner.avatar_url})`
@@ -162,7 +160,14 @@ const [owner, repoName] = repo.split('/')
162160 this.classList.remove('loading')
163161 } catch (e) {
164162 console.error('Error setting Github data:', e)
165- ;(this.querySelector('#gh-description') as HTMLElement).textContent = 'Failed to fetch data'
163+ this.updateElement('#gh-description', 'Failed to fetch data')
164+ }
165+ }
166+
167+ private updateElement(selector: string, text: string) {
168+ const element = this.querySelector(selector) as HTMLElement
169+ if (element) {
170+ element.textContent = text
166171 }
167172 }
168173 }
0 commit comments