Skip to content

Commit 0f04aaf

Browse files
committed
🐞 fix(视频列表): 删除视频或UP时,刷新列表
1 parent 6330c83 commit 0f04aaf

File tree

4 files changed

+57
-56
lines changed

4 files changed

+57
-56
lines changed

src/components/BVListItem/index.tsx

+23-43
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44
* @author darcrand
55
*/
66

7-
import { hasSameKeys, pickVideoInfo } from '@/components/DownloadModal/utils'
8-
import { mediaService } from '@/services/media'
7+
import { ALL_BV_DATA_KEY } from '@/queries/useAllBVData'
98
import type { BVItemFromFile } from '@/types/global'
109
import UImage from '@/ui/UImage'
1110
import { cls } from '@/utils/cls'
1211
import { formatSeconds } from '@/utils/common'
13-
import { DatabaseOutlined, DeleteOutlined, FolderOpenOutlined, LinkOutlined, MoreOutlined } from '@ant-design/icons'
14-
import { useQuery, useQueryClient } from '@tanstack/react-query'
15-
import { removeDir, writeTextFile } from '@tauri-apps/api/fs'
12+
import { DeleteOutlined, FolderOpenOutlined, LinkOutlined, MoreOutlined } from '@ant-design/icons'
13+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
14+
import { removeDir } from '@tauri-apps/api/fs'
1615
import { open as openShell } from '@tauri-apps/api/shell'
1716
import { convertFileSrc } from '@tauri-apps/api/tauri'
18-
import { Button, Dropdown, Modal } from 'antd'
17+
import { App, Button, Dropdown, Modal } from 'antd'
1918
import dayjs from 'dayjs'
2019
import * as R from 'ramda'
2120
import { useMemo, useState } from 'react'
22-
import { NavLink, useNavigate } from 'react-router-dom'
21+
import { NavLink } from 'react-router-dom'
2322

2423
export type BVListItemProps = {
2524
data: BVItemFromFile
@@ -29,6 +28,7 @@ export type BVListItemProps = {
2928

3029
export default function BVListItem(props: BVListItemProps) {
3130
const { videoInfo, bvid, path: bvPath, children } = props.data
31+
const { message } = App.useApp()
3232

3333
const dateLabel = useMemo(() => {
3434
if (!videoInfo) return ''
@@ -68,34 +68,20 @@ export default function BVListItem(props: BVListItemProps) {
6868
await openShell(`https://www.bilibili.com/video/${bvid}`)
6969
}
7070

71-
const navigate = useNavigate()
7271
const queryClient = useQueryClient()
7372
const [openRemove, setOpenRemove] = useState(false)
7473

75-
const removeVideoFolder = async () => {
76-
await removeDir(bvPath, { recursive: true })
77-
setOpenRemove(false)
78-
queryClient.invalidateQueries({ queryKey: ['bv', 'all'] })
79-
navigate('/home/space')
80-
}
74+
const removeMutation = useMutation({
75+
mutationFn: () => removeDir(bvPath, { recursive: true }),
76+
onSuccess() {
77+
message.success('视频删除成功')
78+
setOpenRemove(false)
79+
queryClient.invalidateQueries({ queryKey: ALL_BV_DATA_KEY })
80+
}
81+
})
8182

8283
const pageCount = children?.filter((v) => v.name?.endsWith('.mp4')).length || 0
8384

84-
// 优化功能
85-
// 对应 videoInfo 进行压缩
86-
const infoCompressed = hasSameKeys(videoInfo)
87-
const compressInfo = async () => {
88-
// 重新获取最新的视频信息
89-
const latestInfo = await mediaService.info(bvid || '')
90-
91-
const compressed = pickVideoInfo(latestInfo)
92-
const jsonFilePath = children?.find((v) => v.name?.endsWith('.json'))?.path
93-
if (jsonFilePath) {
94-
await writeTextFile(jsonFilePath, JSON.stringify(compressed))
95-
queryClient.invalidateQueries({ queryKey: ['bv', 'all'] })
96-
}
97-
}
98-
9985
return (
10086
<>
10187
<article className={cls(props.className)}>
@@ -142,27 +128,21 @@ export default function BVListItem(props: BVListItemProps) {
142128
label: '从B站打开',
143129
onClick: openInBrowser
144130
},
145-
{ key: 'remove', icon: <DeleteOutlined />, label: '删除文件夹', onClick: () => setOpenRemove(true) },
146-
{
147-
key: 'compress',
148-
icon: <DatabaseOutlined />,
149-
disabled: infoCompressed,
150-
label: '压缩视频信息',
151-
onClick: compressInfo
152-
}
131+
{ key: 'remove', icon: <DeleteOutlined />, label: '删除文件夹', onClick: () => setOpenRemove(true) }
153132
]
154133
}}
155134
>
156-
<Button
157-
shape='circle'
158-
type='text'
159-
icon={<MoreOutlined className={cls(!infoCompressed && 'text-red-500')} />}
160-
/>
135+
<Button shape='circle' type='text' icon={<MoreOutlined />} />
161136
</Dropdown>
162137
</div>
163138
</article>
164139

165-
<Modal title='删除文件夹' open={openRemove} onCancel={() => setOpenRemove(false)} onOk={removeVideoFolder}>
140+
<Modal
141+
title='删除文件夹'
142+
open={openRemove}
143+
onCancel={() => setOpenRemove(false)}
144+
onOk={() => removeMutation.mutateAsync()}
145+
>
166146
<p>当前的 BV 视频文件夹中的所有文件都会被删除噢</p>
167147
<p>确定要删除文件夹吗?</p>
168148
</Modal>

src/main.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StyleProvider } from '@ant-design/cssinjs'
22
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
3-
import { ConfigProvider } from 'antd'
3+
import { App as AntdApp, ConfigProvider } from 'antd'
44
import zhCN from 'antd/locale/zh_CN'
55
import 'dayjs/locale/zh-cn'
66
import ReactDOM from 'react-dom/client'
@@ -23,7 +23,9 @@ ReactDOM.createRoot(root).render(
2323
<QueryClientProvider client={client}>
2424
<ConfigProvider locale={zhCN} theme={{ token: { colorPrimary: '#fb7299', colorLink: '#fb7299' } }}>
2525
<StyleProvider hashPriority='high'>
26-
<App />
26+
<AntdApp>
27+
<App />
28+
</AntdApp>
2729
</StyleProvider>
2830
</ConfigProvider>
2931
</QueryClientProvider>

src/pages/root/home/space/[id]/index.tsx

+26-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import BVListItem from '@/components/BVListItem'
88
import { useRootDirPath } from '@/hooks/use-root-dir-path'
9-
import { useAllBVData } from '@/queries/useAllBVData'
9+
import { ALL_BV_DATA_KEY, useAllBVData } from '@/queries/useAllBVData'
1010
import { fsService } from '@/services/fs'
1111
import { userService } from '@/services/user'
1212
import { useSetCachedUrl } from '@/stores/use-cached-url'
@@ -15,10 +15,10 @@ import UEmpty from '@/ui/UEmpty'
1515
import UImage from '@/ui/UImage'
1616
import useUrlState from '@ahooksjs/use-url-state'
1717
import { DeleteOutlined, FolderOpenOutlined, MoreOutlined } from '@ant-design/icons'
18-
import { useIsFetching, useQuery, useQueryClient } from '@tanstack/react-query'
18+
import { useIsFetching, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
1919
import { removeDir } from '@tauri-apps/api/fs'
2020
import { open as openShell } from '@tauri-apps/api/shell'
21-
import { Button, Dropdown, Input, Modal, Pagination } from 'antd'
21+
import { App, Button, Dropdown, Input, Modal, Pagination } from 'antd'
2222
import QueryString from 'qs'
2323
import * as R from 'ramda'
2424
import { useEffect, useState } from 'react'
@@ -27,6 +27,7 @@ import { useNavigate, useParams } from 'react-router-dom'
2727
const PAGE_SIZE = 24
2828

2929
export default function SpacePage() {
30+
const { message } = App.useApp()
3031
const rootDirPath = useRootDirPath()
3132
const { data: allData } = useAllBVData(rootDirPath)
3233
const isLoading = useIsFetching() > 0
@@ -96,16 +97,26 @@ export default function SpacePage() {
9697
const navigate = useNavigate()
9798
const queryClient = useQueryClient()
9899

99-
const removeOwnerFolder = async () => {
100-
const matched = ownerDirs?.find((v) => v.mid === id)
101-
if (matched?.path) {
102-
await removeDir(matched?.path, { recursive: true })
100+
const removeMutation = useMutation({
101+
mutationFn: async () => {
102+
const matched = ownerDirs?.find((v) => v.mid === id)
103+
if (matched?.path) {
104+
await removeDir(matched?.path, { recursive: true })
105+
} else {
106+
throw new Error('no path')
107+
}
108+
},
109+
onError() {
110+
message.error('删除失败')
111+
},
112+
onSuccess() {
113+
message.success('UP主删除成功')
103114
setOpenRemove(false)
104-
queryClient.invalidateQueries({ queryKey: ['bv', 'all-in-one'] })
115+
queryClient.invalidateQueries({ queryKey: ALL_BV_DATA_KEY })
105116
queryClient.invalidateQueries({ queryKey: ['bv', 'all'] })
106117
navigate('/home/space')
107118
}
108-
}
119+
})
109120

110121
return (
111122
<>
@@ -189,7 +200,12 @@ export default function SpacePage() {
189200
</footer>
190201
</div>
191202

192-
<Modal title='删除文件夹' open={openRemove} onCancel={() => setOpenRemove(false)} onOk={removeOwnerFolder}>
203+
<Modal
204+
title='删除文件夹'
205+
open={openRemove}
206+
onCancel={() => setOpenRemove(false)}
207+
onOk={() => removeMutation.mutateAsync()}
208+
>
193209
<p>确定要删除文件夹吗?</p>
194210
<p>这个 Up 所有的视频都会被删除哦</p>
195211
</Modal>

src/queries/useAllBVData.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { fsService } from '@/services/fs'
22
import { useQuery } from '@tanstack/react-query'
33

4+
// allBVDataKey to uppercase
5+
export const ALL_BV_DATA_KEY = ['bv', 'all-in-one']
6+
47
// 将 UP 主列表和 BV 列表整合在一起
58
export function useAllBVData(rootDirPath?: string) {
69
const { data } = useQuery({
710
enabled: !!rootDirPath,
8-
queryKey: ['bv', 'all-in-one', rootDirPath],
11+
queryKey: ALL_BV_DATA_KEY.concat(rootDirPath || ''),
912
queryFn: () => fsService.getAllBVData(rootDirPath || '')
1013
})
1114

0 commit comments

Comments
 (0)