|
| 1 | +<script> |
| 2 | + import { onMount } from "svelte"; |
| 3 | + import createLocaleStorage from "../lib/createLocaleStorage"; |
| 4 | + import GithubIcon from "./GithubIcon.svelte"; |
| 5 | +
|
| 6 | + const REPOSITORY_PATH = "matschik/component-party.dev"; |
| 7 | + const DURATION_2_MIN = 1000 * 60 * 2; |
| 8 | + const STAR_COUNT_DURATION_EXPIRATION = DURATION_2_MIN; |
| 9 | +
|
| 10 | + const starCountStorage = createLocaleStorage("github-star-count"); |
| 11 | +
|
| 12 | + let starCount = 0; |
| 13 | + let isFetchingStarCount = false; |
| 14 | +
|
| 15 | + async function getRepoStarCount() { |
| 16 | + const starCountStorageData = starCountStorage.getJSON(); |
| 17 | + if (starCountStorageData) { |
| 18 | + starCount = starCountStorageData.value; |
| 19 | + if ( |
| 20 | + starCountStorageData.fetchedAt > |
| 21 | + Date.now() - STAR_COUNT_DURATION_EXPIRATION |
| 22 | + ) { |
| 23 | + return; |
| 24 | + } |
| 25 | + } |
| 26 | +
|
| 27 | + isFetchingStarCount = true; |
| 28 | +
|
| 29 | + // Github public API rate limit: 60 requests per hour |
| 30 | + const data = await fetch( |
| 31 | + `https://api.github.com/repos/${REPOSITORY_PATH}`, |
| 32 | + { |
| 33 | + headers: { |
| 34 | + Accept: "application/vnd.github.v3.star+json", |
| 35 | + Authorization: "", |
| 36 | + }, |
| 37 | + } |
| 38 | + ) |
| 39 | + .finally(() => { |
| 40 | + isFetchingStarCount = false; |
| 41 | + }) |
| 42 | + .then((r) => r.json()); |
| 43 | +
|
| 44 | + if (data.stargazers_count) { |
| 45 | + starCount = data.stargazers_count; |
| 46 | + starCountStorage.setJSON({ |
| 47 | + value: starCount, |
| 48 | + fetchedAt: Date.now(), |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + onMount(() => { |
| 54 | + getRepoStarCount(); |
| 55 | + }); |
| 56 | +
|
| 57 | + function onButtonClick() { |
| 58 | + starCountStorage.remove(); |
| 59 | + } |
| 60 | +</script> |
| 61 | + |
| 62 | +<a |
| 63 | + class="bg-[#21262d] text-[#c9d1d9] border border-[#373b43] py-1 rounded flex items-center text-sm shadow-inner hover:opacity-90" |
| 64 | + href={`https://github.com/${REPOSITORY_PATH}`} |
| 65 | + target="_blank" |
| 66 | + aria-label={`Star ${REPOSITORY_PATH} on GitHub`} |
| 67 | + on:click={onButtonClick} |
| 68 | +> |
| 69 | + <span |
| 70 | + class="space-x-2 flex items-center border-r border-[#373b43] font-medium px-2" |
| 71 | + > |
| 72 | + <GithubIcon class="w-[1.1rem] h-[1.1rem]" /> |
| 73 | + <span class="mt-px">Star</span> |
| 74 | + </span> |
| 75 | + {#if isFetchingStarCount || starCount !== 0} |
| 76 | + <div class="h-full flex justify-center items-center pl-3 pr-3 font-medium"> |
| 77 | + {#if isFetchingStarCount && starCount === 0} |
| 78 | + <svg |
| 79 | + class="animate-spin h-4 w-4 mx-1" |
| 80 | + xmlns="http://www.w3.org/2000/svg" |
| 81 | + fill="none" |
| 82 | + viewBox="0 0 24 24" |
| 83 | + > |
| 84 | + <circle |
| 85 | + class="opacity-25" |
| 86 | + cx="12" |
| 87 | + cy="12" |
| 88 | + r="10" |
| 89 | + stroke="currentColor" |
| 90 | + stroke-width="4" |
| 91 | + /> |
| 92 | + <path |
| 93 | + class="opacity-75" |
| 94 | + fill="currentColor" |
| 95 | + d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" |
| 96 | + /> |
| 97 | + </svg> |
| 98 | + {:else} |
| 99 | + <span class="mt-px">{starCount}</span> |
| 100 | + {/if} |
| 101 | + </div> |
| 102 | + {/if} |
| 103 | +</a> |
0 commit comments