useWallpaperActions.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { useQueryClient } from '@tanstack/react-query'
  2. import type { ScreenTarget } from '@shared/types'
  3. import { cardPlayNeedsScreenChoice } from '@shared/playScreen'
  4. import { useToast } from '../components/common/Toast'
  5. import { askPlayScreen } from '../components/common/PlayScreenPrompt'
  6. import { forEachIgnoringErrors } from '../utils/async'
  7. import { t } from '../i18n'
  8. export function useApplyWallpaper(): (
  9. id: string,
  10. screen?: ScreenTarget,
  11. onError?: (message: string) => void
  12. ) => Promise<void> {
  13. const queryClient = useQueryClient()
  14. const { showToast } = useToast()
  15. return async (id, screen, onError = showToast) => {
  16. try {
  17. let chosen = screen
  18. if (chosen === undefined) {
  19. const targets = await queryClient.fetchQuery({
  20. queryKey: ['display-targets'],
  21. queryFn: () => window.electronAPI.wallpaper.getTargets(),
  22. staleTime: 30_000
  23. })
  24. if (cardPlayNeedsScreenChoice(targets)) {
  25. const picked = await askPlayScreen(targets.filter((name) => name !== '*'))
  26. if (picked === null) return
  27. chosen = picked
  28. }
  29. }
  30. await window.electronAPI.wallpaper.apply({ wallpaperId: id, screen: chosen })
  31. void queryClient.invalidateQueries({ queryKey: ['library'] })
  32. } catch (err) {
  33. onError((err as Error).message)
  34. }
  35. }
  36. }
  37. export function useSubscribe(): (id: string) => Promise<void> {
  38. const queryClient = useQueryClient()
  39. return async (id) => {
  40. await window.electronAPI.steam.subscribe(id)
  41. void queryClient.invalidateQueries({ queryKey: ['library'] })
  42. }
  43. }
  44. // unsubscribing can drop a wallpaper from the library, and with it from its folder
  45. export function useUnsubscribe(): (ids: string[]) => Promise<void> {
  46. const queryClient = useQueryClient()
  47. return async (ids) => {
  48. await forEachIgnoringErrors(ids, (id) => window.electronAPI.steam.unsubscribe(id))
  49. void queryClient.invalidateQueries({ queryKey: ['library'] })
  50. void queryClient.invalidateQueries({ queryKey: ['folders'] })
  51. }
  52. }
  53. function useRepairFiles(): (ids: string[], onStatus: (text: string) => void) => Promise<string> {
  54. const queryClient = useQueryClient()
  55. return async (ids, onStatus) => {
  56. onStatus(t('verify.starting', { count: ids.length }))
  57. void queryClient.invalidateQueries({ queryKey: ['library'] })
  58. let latest: { done: number; total: number } | null = null
  59. const off = window.electronAPI.on.repairProgress(({ done, total }) => {
  60. latest = { done, total }
  61. if (done >= total) {
  62. off()
  63. onStatus(t('verify.repairFinished', { done, total }))
  64. } else {
  65. onStatus(t('verify.repairProgress', { done, total }))
  66. }
  67. })
  68. try {
  69. const result = await window.electronAPI.steam.repair(ids)
  70. if (result.started === 0) off()
  71. const progress = latest as { done: number; total: number } | null
  72. let summary = progress
  73. ? t(progress.done >= progress.total ? 'verify.repairFinished' : 'verify.repairProgress', { done: progress.done, total: progress.total })
  74. : t('verify.repairing', { count: result.started })
  75. if (result.failed > 0) summary += ', ' + t('common.failedCount', { count: result.failed })
  76. return summary
  77. } catch (err) {
  78. off()
  79. return (err as Error).message
  80. } finally {
  81. void queryClient.invalidateQueries({ queryKey: ['library'] })
  82. }
  83. }
  84. }
  85. // ids undefined = all workshop wallpapers; damaged ones redownload after a confirm
  86. export function useVerifyFiles(): (ids: string[] | undefined, onStatus: (text: string) => void) => Promise<string> {
  87. const queryClient = useQueryClient()
  88. const repairFiles = useRepairFiles()
  89. return async (ids, onStatus) => {
  90. const off = window.electronAPI.on.verifyProgress(({ done, total }) => { onStatus(t('verify.progress', { done, total })) })
  91. let result
  92. try {
  93. result = await window.electronAPI.steam.verify(ids)
  94. } catch (err) {
  95. return (err as Error).message
  96. } finally {
  97. off()
  98. void queryClient.invalidateQueries({ queryKey: ['library'] })
  99. }
  100. const { checked, skipped, damaged } = result
  101. const parts = [
  102. damaged.length === 0
  103. ? t('verify.noProblems', { count: checked })
  104. : t('verify.foundDamaged', { count: damaged.length })
  105. ]
  106. if (skipped > 0) parts.push(t('verify.skipped', { count: skipped }))
  107. if (damaged.length === 0 || !confirm(t('verify.repairConfirm', { count: damaged.length }))) return parts.join(', ')
  108. return repairFiles(damaged, onStatus)
  109. }
  110. }