Skip to content

Commit fbd62c6

Browse files
committed
fix: update Contributors component to use Vue's ref and onMounted for better state management
1 parent 9bdf746 commit fbd62c6

3 files changed

Lines changed: 29 additions & 27 deletions

File tree

docs/.vitepress/theme/MyLayout.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup>
2-
import DefaultTheme from 'vitepress/theme'
2+
import Theme from 'vitepress/theme'
33
import Contributors from "./components/Contributors.vue";
44
5-
const { Layout } = DefaultTheme
5+
const { Layout } = Theme
66
</script>
77

88
<template>

docs/.vitepress/theme/components/Contributors.vue

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts" setup>
2-
import { useStorage } from '@vueuse/core'
2+
import { ref, onMounted } from 'vue'
33
4-
const contributors = useStorage<any[]>('contributors', [])
4+
const contributors = ref<any[]>([])
55
66
const fromRepo = (repo: string) =>
77
fetch(`https://api.github.com/repos/tweakphp/${repo}/contributors`)
@@ -16,30 +16,29 @@ const getContributors = async () => {
1616
fromRepo('.github'),
1717
])
1818
19-
contributors.value = users.reduce((acc, data = []) => {
20-
if (!Array.isArray(data)) {
21-
return acc
22-
}
23-
24-
return [...acc, ...data.filter(i => i.login)]
25-
}, []).reduce((acc, user) => {
26-
const existingUser = acc.find(u => u.id === user.id)
27-
28-
if (existingUser) {
29-
existingUser.contributions += user.contributions
30-
return acc
31-
}
32-
33-
return [...acc, {
34-
id: user.id,
35-
username: user.login,
36-
contributions: user.contributions,
37-
avatar_url: user.avatar_url
38-
}]
39-
}, [])
19+
contributors.value = users
20+
.reduce((acc, data = []) => {
21+
if (!Array.isArray(data)) return acc
22+
return [...acc, ...data.filter(i => i.login)]
23+
}, [])
24+
.reduce((acc, user) => {
25+
const existingUser = acc.find(u => u.id === user.id)
26+
if (existingUser) {
27+
existingUser.contributions += user.contributions
28+
return acc
29+
}
30+
return [...acc, {
31+
id: user.id,
32+
username: user.login,
33+
contributions: user.contributions,
34+
avatar_url: user.avatar_url
35+
}]
36+
}, [])
4037
}
4138
42-
getContributors()
39+
onMounted(() => {
40+
getContributors()
41+
})
4342
</script>
4443

4544
<template>

docs/.vitepress/theme/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// https://vitepress.dev/guide/custom-theme
22
import Theme from 'vitepress/theme'
3-
import Contributors from './components/Contributors.vue'
43
import './style.css'
54
import MyLayout from "./MyLayout.vue";
5+
import Contributors from "./components/Contributors.vue";
66

77
export default {
88
extends: Theme,
99
Layout: MyLayout,
10+
enhanceApp({ app }) {
11+
app.component('Contributors', Contributors);
12+
}
1013
}

0 commit comments

Comments
 (0)