store.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import type { TaskReport } from './tasks'
  2. import selfCheck, { ReportStatus } from '@/api/self_check'
  3. import { debounce } from 'lodash'
  4. import frontendTasks from './tasks/frontend'
  5. export const useSelfCheckStore = defineStore('selfCheck', () => {
  6. const data = ref<TaskReport[]>([])
  7. const loading = ref(false)
  8. async function __check() {
  9. if (loading.value)
  10. return
  11. loading.value = true
  12. try {
  13. const backendReports = (await selfCheck.run()).map(r => {
  14. return {
  15. key: r.key,
  16. name: () => $gettext(r.name.message, r.name.args),
  17. description: () => $gettext(r.description.message, r.description.args),
  18. type: 'backend' as const,
  19. status: r.status,
  20. fixable: r.fixable,
  21. err: r.err,
  22. }
  23. })
  24. const frontendReports = await Promise.all(
  25. Object.entries(frontendTasks).map(async ([key, task]) => {
  26. return {
  27. key,
  28. name: task.name,
  29. description: task.description,
  30. type: 'frontend' as const,
  31. status: await task.check(),
  32. fixable: false,
  33. }
  34. }),
  35. )
  36. data.value = [...backendReports, ...frontendReports]
  37. }
  38. catch (error) {
  39. console.error(error)
  40. }
  41. finally {
  42. loading.value = false
  43. }
  44. }
  45. const check = debounce(__check, 1000, {
  46. leading: true,
  47. trailing: false,
  48. })
  49. const fixing = ref<Record<string, boolean>>({})
  50. async function fix(taskName: string) {
  51. if (fixing.value[taskName])
  52. return
  53. fixing.value[taskName] = true
  54. try {
  55. await selfCheck.fix(taskName)
  56. await nextTick()
  57. setTimeout(() => {
  58. check()
  59. }, 1000)
  60. }
  61. finally {
  62. fixing.value[taskName] = false
  63. }
  64. }
  65. const hasError = computed(() => {
  66. return data.value?.some(item => item.status === ReportStatus.Error)
  67. })
  68. return { data, loading, fixing, hasError, check, fix }
  69. })