Skip to content

Commit

Permalink
feat(app): add Github star count
Browse files Browse the repository at this point in the history
  • Loading branch information
matschik committed Jul 15, 2023
1 parent 04453e6 commit 9098d6a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
6 changes: 4 additions & 2 deletions content/1-reactivity/2-update-state/react/Name.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useEffect, useState } from "react";
export default function Name() {
const [name, setName] = useState("John");

useEffect(() => setName("Jane"), []);
useEffect(() => {
setName("Jane");
}, []);

return <h1>Hello {name}</h1>;
}
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
:root {
/* bg-gray-900 */
--bg-color: rgb(17 24 39);
color-scheme: dark;
}

html {
Expand Down
4 changes: 0 additions & 4 deletions src/app.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
:root {
color-scheme: dark;
}

@tailwind base;
@tailwind components;
@tailwind utilities;
Expand Down
65 changes: 65 additions & 0 deletions src/components/GithubStarButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<script>
import { onMount } from "svelte";
import createLocaleStorage from "../lib/createLocaleStorage";
import GithubIcon from "./GithubIcon.svelte";
const REPOSITORY_PATH = "matschik/component-party.dev";
const starCountStorage = createLocaleStorage("github-star-count");
let starCount = null;
async function getRepoStarCount() {
const starCountStorageData = starCountStorage.getJSON();
if (
starCountStorageData &&
starCountStorageData.fetchedAt > Date.now() - 1000 * 60 * 5
) {
starCount = starCountStorageData.value;
return;
}
const data = await fetch(
`https://api.github.com/repos/${REPOSITORY_PATH}`,
{
headers: {
Accept: "application/vnd.github.v3.star+json",
Authorization: "",
},
}
).then((r) => r.json());
starCount = data.stargazers_count;
starCountStorage.setJSON({
value: starCount,
fetchedAt: Date.now(),
});
}
onMount(() => {
getRepoStarCount();
});
function onButtonClick() {
starCountStorage.remove();
}
</script>

<a
class="bg-[#21262d] text-[#c9d1d9] border border-[#373b43] py-1 rounded flex items-center text-sm shadow-inner hover:opacity-90"
href={`https://github.com/${REPOSITORY_PATH}`}
target="_blank"
aria-label={`Star ${REPOSITORY_PATH} on GitHub`}
on:click={onButtonClick}
>
<span
class="space-x-2 flex items-center border-r border-[#373b43] font-medium px-2"
>
<GithubIcon class="w-[1.1rem] h-[1.1rem]" />
<span class="mt-px">Star</span>
</span>
{#if starCount !== null}
<div class="h-full flex justify-center items-center pl-3 pr-3 font-medium">
<span class="mt-px">{starCount}</span>
</div>
{/if}
</a>
15 changes: 2 additions & 13 deletions src/components/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import GithubIcon from "./GithubIcon.svelte";
import GithubStarButton from "./GithubStarButton.svelte";
</script>

<header class="backdrop-blur bg-gray-900/80 border-b border-gray-700">
Expand All @@ -9,18 +9,7 @@
<img src="/popper.svg" alt="logo" class="w-5 h-5" />
<span>Component party</span>
</a>

<div>
<a
href="https://github.com/matschik/component-party"
title="Contribute on Github"
target="_blank"
rel="noopener noreferrer"
>
<GithubIcon class="h-6 w-6" />
<span class="sr-only">github</span>
</a>
</div>
<GithubStarButton />
</div>
</div>
</header>

0 comments on commit 9098d6a

Please sign in to comment.