|
| 1 | +<script lang="ts" setup> |
| 2 | +import { BookmarkSquareIcon } from "@heroicons/vue/24/outline" |
| 3 | +
|
| 4 | +import { Ref, onBeforeMount, toRefs } from "vue" |
| 5 | +import { useStorage } from "@vueuse/core" |
| 6 | +import { useThemeLocaleData } from "@vuepress/plugin-theme-data/client" |
| 7 | +
|
| 8 | +import { onRouteChange } from "../composables/usePageHook" |
| 9 | +
|
| 10 | +import type { DefaultThemeLocaleData } from "../../shared" |
| 11 | +const $props = withDefaults( |
| 12 | + defineProps<{ |
| 13 | + owner?: string |
| 14 | + repo?: string |
| 15 | + }>(), |
| 16 | + {} |
| 17 | +) |
| 18 | +const { owner, repo } = toRefs($props) |
| 19 | +const themeLocale: Ref<DefaultThemeLocaleData> = useThemeLocaleData() |
| 20 | +
|
| 21 | +let repoInfo = useStorage<{ |
| 22 | + data: any |
| 23 | + timestamp: number |
| 24 | +}>(`v-nc-github-repo[${owner?.value}/${repo?.value}]`, { |
| 25 | + data: {}, |
| 26 | + timestamp: -1, |
| 27 | +}) |
| 28 | +onRouteChange(() => { |
| 29 | + repoInfo = useStorage<{ |
| 30 | + data: any |
| 31 | + timestamp: number |
| 32 | + }>(`v-nc-github-repo[${owner?.value}/${repo?.value}]`, { |
| 33 | + data: {}, |
| 34 | + timestamp: -1, |
| 35 | + }) |
| 36 | +}) |
| 37 | +
|
| 38 | +onBeforeMount(() => { |
| 39 | + const updateRepoInfo = async () => |
| 40 | + await fetch(`${themeLocale.value.github?.accessServer}/repo`, { |
| 41 | + method: "POST", |
| 42 | + body: JSON.stringify({ username: owner.value, repo: repo.value }), |
| 43 | + headers: { |
| 44 | + "Content-Type": "application/json", |
| 45 | + Accept: "application/json", |
| 46 | + Authorization: `Bearer ${themeLocale.value.github?.accessToken}`, |
| 47 | + }, |
| 48 | + }) |
| 49 | + .then((res) => res.json()) |
| 50 | + .then((res) => { |
| 51 | + repoInfo.value.data = res |
| 52 | + repoInfo.value.timestamp = Date.now() |
| 53 | + }) |
| 54 | + if (repoInfo.value.timestamp == -1) { |
| 55 | + if (owner && repo && themeLocale.value.github?.accessServer) |
| 56 | + updateRepoInfo() |
| 57 | + } else if (repoInfo.value.timestamp + 1000 * 60 * 60 * 24 < Date.now()) { |
| 58 | + if (owner && repo && themeLocale.value.github?.accessServer) |
| 59 | + updateRepoInfo() |
| 60 | + } |
| 61 | +}) |
| 62 | +</script> |
| 63 | + |
| 64 | +<template> |
| 65 | + <Transition name="fade" mode="out-in" appear> |
| 66 | + <div class="github-repo-card" v-if="owner && repo"> |
| 67 | + <span class="prefix">Linked Github Repo</span> |
| 68 | + <a :href="repoInfo?.html_url" class="card-main" target="_blank"> |
| 69 | + <span class="repo-title"> |
| 70 | + <BookmarkSquareIcon class="icon" /> |
| 71 | + <span class="repo-name"> |
| 72 | + <span class="repo">{{ repo }}</span> |
| 73 | + <span class="owner">{{ owner }}</span> |
| 74 | + </span> |
| 75 | + </span> |
| 76 | + <span class="repo-description" v-if="repoInfo?.data?.description">{{ |
| 77 | + repoInfo?.data?.description |
| 78 | + }}</span> |
| 79 | + </a> |
| 80 | + </div> |
| 81 | + </Transition> |
| 82 | +</template> |
| 83 | + |
| 84 | +<style lang="postcss" scoped> |
| 85 | +.github-repo-card { |
| 86 | + @apply relative flex flex-col gap-y-2 px-1.5 my-2; |
| 87 | +} |
| 88 | +
|
| 89 | +.github-repo-card .prefix { |
| 90 | + @apply text-sm font-semibold text-green-600; |
| 91 | +} |
| 92 | +
|
| 93 | +.github-repo-card .card-main { |
| 94 | + @apply w-fit max-w-48 flex flex-col justify-center py-1 px-2 gap-1 |
| 95 | + border-2 rounded border-sky-300 dark:border-sky-600 |
| 96 | + |
| 97 | + transition-all ease-in-out duration-300 cursor-pointer; |
| 98 | +} |
| 99 | +
|
| 100 | +.card-main .repo-title { |
| 101 | + @apply inline-flex items-center gap-x-1; |
| 102 | +} |
| 103 | +.card-main .repo-name { |
| 104 | + @apply inline-flex flex-col |
| 105 | + text-sm text-blue-500; |
| 106 | +} |
| 107 | +
|
| 108 | +.repo-name span { |
| 109 | + @apply inline-block max-w-36 |
| 110 | + truncate; |
| 111 | +} |
| 112 | +
|
| 113 | +.repo-description { |
| 114 | + @apply text-sm opacity-50; |
| 115 | +} |
| 116 | +
|
| 117 | +.card-main .icon { |
| 118 | + @apply w-7 h-7 stroke-blue-500; |
| 119 | +} |
| 120 | +</style> |
0 commit comments