Files
bilibili-subtitle/src/hooks/useMessageService.ts
2024-10-10 09:23:42 +08:00

32 lines
1.1 KiB
TypeScript

import { setAuthor, setCtime, setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer'
import { useMemo } from 'react'
import { useAppDispatch } from './redux'
import useMessagingService from '@/messaging/layer2/useMessagingService'
const useMessageService = () => {
const dispatch = useAppDispatch()
//methods
const methods: {
[K in AllAPPMessages['method']]: (params: Extract<AllAPPMessages, { method: K }>['params'], context: MethodContext) => Promise<any>
} = useMemo(() => ({
SET_INFOS: async (params, context: MethodContext) => {
dispatch(setInfos(params.infos))
dispatch(setCurInfo(undefined))
dispatch(setCurFetched(false))
dispatch(setData(undefined))
},
SET_VIDEO_INFO: async (params, context: MethodContext) => {
dispatch(setInfos(params.infos))
dispatch(setUrl(params.url))
dispatch(setTitle(params.title))
dispatch(setCtime(params.ctime))
dispatch(setAuthor(params.author))
console.debug('video title: ', params.title)
},
}), [dispatch])
useMessagingService(methods)
}
export default useMessageService