Skip to content

Commit

Permalink
refactor: convert comment plugin helper functions into hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 11, 2023
1 parent b9ecf2d commit af82e99
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 191 deletions.
26 changes: 6 additions & 20 deletions src/components/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,23 @@ import { useAppStore } from '@/stores/app'
import { useI18n } from 'vue-i18n'
import SvgIcon, { SvgTypes } from '@/components/SvgIcon/index.vue'
import beianImg from '@/assets/gongan-beian-40-40.png'
import { walinePageViewInit } from '@/utils/comments/waline-api'
import {
enabledCommentPlugin,
intiCommentPluginPageView
} from '@/utils/comments/helpers'
import { getDaysTillNow } from '@/utils'
import useCommentPlugin from '@/hooks/useCommentPlugin'
export default defineComponent({
name: 'ObFooter',
components: { SvgIcon },
setup() {
const appStore = useAppStore()
const { t } = useI18n()
const enabledPlugin = computed(
() => enabledCommentPlugin(appStore.themeConfig.plugins).plugin
)
const { enabledCommentPlugin, intiCommentPluginPageView } =
useCommentPlugin()
watch(
() => enabledPlugin.value,
() => enabledCommentPlugin.value.plugin,
(newValue, oldValue) => {
if (oldValue === '' && newValue) {
window.setTimeout(
() =>
intiCommentPluginPageView(
newValue,
'/',
appStore.themeConfig.plugins
),
50
)
window.setTimeout(() => intiCommentPluginPageView('/'), 50)
}
}
)
Expand Down Expand Up @@ -179,7 +165,7 @@ export default defineComponent({
return getDaysTillNow(`${appStore.themeConfig.site.started_date}`)
}),
intiCommentPluginPageView,
enabledPlugin,
enabledPlugin: computed(() => enabledCommentPlugin.value.plugin),
t
}
}
Expand Down
28 changes: 10 additions & 18 deletions src/components/Post/PostStats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,10 @@
</template>

<script lang="ts">
import { PropType, computed, defineComponent, ref, defineExpose } from 'vue'
import { PropType, computed, defineComponent, ref } from 'vue'
import SvgIcon from '@/components/SvgIcon/index.vue'
import {
enabledCommentPlugin,
initCommentPluginCommentCount,
intiCommentPluginPageView
} from '@/utils/comments/helpers'
import { PluginsData, ThemeConfig } from '@/models/ThemeConfig.class'
import useCommentPlugin from '@/hooks/useCommentPlugin'
export default defineComponent({
name: 'ObPostStats',
Expand Down Expand Up @@ -208,24 +204,20 @@ export default defineComponent({
},
setup(props, { expose }) {
const commentCount = ref<number | undefined>(undefined)
const enabledPlugin = computed(
() => enabledCommentPlugin(props.pluginConfigs).plugin
)
const {
enabledCommentPlugin,
initCommentPluginCommentCount,
intiCommentPluginPageView
} = useCommentPlugin()
const getCommentCount = async () => {
commentCount.value = await initCommentPluginCommentCount(
enabledPlugin.value,
props.currentPath,
props.pluginConfigs
props.currentPath
)
}
const getPostView = () => {
intiCommentPluginPageView(
enabledPlugin.value,
props.currentPath,
props.pluginConfigs
)
intiCommentPluginPageView(props.currentPath)
}
expose({
Expand All @@ -235,7 +227,7 @@ export default defineComponent({
return {
commentCount,
plugin: enabledPlugin
plugin: computed(() => enabledCommentPlugin.value.plugin)
}
}
})
Expand Down
100 changes: 11 additions & 89 deletions src/components/Sidebar/src/RecentComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,127 +98,49 @@
import { computed, defineComponent, ref, watch } from 'vue'
import { SubTitle } from '@/components/Title'
import SvgIcon from '@/components/SvgIcon/index.vue'
import { RecentComment } from '@/utils'
import { GithubComments } from '@/utils/comments/github-api'
import { LeanCloudComments } from '@/utils/comments/leancloud-api'
import { useAppStore } from '@/stores/app'
import { useI18n } from 'vue-i18n'
import { TwikooComments } from '@/utils/comments/twikoo-api'
import { WalineComments } from '@/utils/comments/waline-api'
import { SvgTypes } from '@/components/SvgIcon/index.vue'
import { enabledCommentPlugin } from '@/utils/comments/helpers'
import useCommentPlugin from '@/hooks/useCommentPlugin'
export default defineComponent({
name: 'ObRecentComment',
components: { SubTitle, SvgIcon },
setup() {
const appStore = useAppStore()
const { t } = useI18n()
let recentComments = ref<RecentComment[]>([])
let loading = ref<boolean>(true)
const enabledPlugin = computed<string | undefined>(() => {
const result = enabledCommentPlugin(appStore.themeConfig.plugins)
return result.plugin !== '' && !!result.recentComment
? result.plugin
: undefined
})
const initRecentComment = () => {
if (!appStore.configReady || enabledPlugin.value === undefined) {
loading.value = false
return
}
switch (enabledPlugin.value) {
case 'gitalk':
const githubComments = new GithubComments({
repo: appStore.themeConfig.plugins.gitalk.repo,
clientId: appStore.themeConfig.plugins.gitalk.clientID,
clientSecret: appStore.themeConfig.plugins.gitalk.clientSecret,
owner: appStore.themeConfig.plugins.gitalk.owner,
admin: appStore.themeConfig.plugins.gitalk.admin[0]
})
githubComments.getComments().then(response => {
recentComments.value = response
})
break
case 'valine':
const leadCloudComments = new LeanCloudComments({
appId: appStore.themeConfig.plugins.valine.app_id,
appKey: appStore.themeConfig.plugins.valine.app_key,
avatar: appStore.themeConfig.plugins.valine.avatar,
admin: appStore.themeConfig.plugins.valine.admin,
lang: appStore.themeConfig.plugins.valine.lang
})
leadCloudComments.getRecentComments(7).then(res => {
recentComments.value = res
loading.value = false
})
break
case 'twikoo':
const twikooComments = new TwikooComments({
envId: appStore.themeConfig.plugins.twikoo.envId,
lang: appStore.themeConfig.plugins.twikoo.lang
})
twikooComments.getRecentComments(7).then(res => {
recentComments.value = res
loading.value = false
})
break
case 'waline':
const walineComments = new WalineComments({
serverURL:
'https://' + appStore.themeConfig.plugins.waline.serverURL,
lang: appStore.locale ?? 'en'
})
walineComments.getRecentComments(7).then(res => {
recentComments.value = res
loading.value = false
})
break
default:
loading.value = false
}
}
const {
enabledCommentPlugin,
recentComments,
fetchRecentComment,
commentPluginLoading
} = useCommentPlugin()
/** Wait for config is ready */
watch(
() => appStore.configReady,
(newValue, oldValue) => {
if (!oldValue && newValue) {
initRecentComment()
fetchRecentComment()
}
}
)
return {
SvgTypes,
isLoading: computed(() => loading.value),
isLoading: computed(() => commentPluginLoading.value),
comments: computed(() => {
return recentComments.value
}),
isConfigReady: computed(() => appStore.configReady),
initRecentComment,
fetchRecentComment,
enabledCommentPlugin,
t
}
},
mounted() {
if (this.isConfigReady) {
this.initRecentComment()
this.fetchRecentComment()
}
}
})
Expand Down
Loading

0 comments on commit af82e99

Please sign in to comment.