Files
Melo/src/renderer/core/useApp/useDeeplink/index.ts
T
2022-10-29 11:40:36 +08:00

77 lines
1.9 KiB
TypeScript

import { onBeforeUnmount } from '@common/utils/vueTools'
import { clearEnvParamsDeeplink, focusWindow, onDeeplink } from '@renderer/utils/ipc'
import { useDialog } from './utils'
import useMusicAction from './useMusicAction'
import useSonglistAction from './useSonglistAction'
export default () => {
let isInited = false
const showErrorDialog = useDialog()
const handleMusicAction = useMusicAction()
const handleSonglistAction = useSonglistAction()
const handleLinkAction = async(link: string) => {
// console.log(link)
const [url, search] = link.split('?')
const [type, action, ...paths] = url.replace('lxmusic://', '').split('/')
const params: {
paths: string[]
data?: any
[key: string]: any
} = {
paths: [],
}
if (search) {
for (const param of search.split('&')) {
const [key, value] = param.split('=')
params[key] = value
}
if (params.data) params.data = JSON.parse(decodeURIComponent(params.data))
}
params.paths = paths.map(p => decodeURIComponent(p))
console.log(params)
switch (type) {
case 'music':
handleMusicAction(action, params)
break
case 'songlist':
await handleSonglistAction(action, params)
break
default: throw new Error('Unknown type: ' + type)
}
}
const rDeeplink = onDeeplink(async({ params: link }) => {
console.log(link)
if (!isInited) return
clearEnvParamsDeeplink()
try {
await handleLinkAction(link)
} catch (err: any) {
showErrorDialog(err.message)
focusWindow()
}
})
onBeforeUnmount(() => {
rDeeplink()
})
return async(envParams: LX.EnvParams) => {
if (envParams.deeplink) {
clearEnvParamsDeeplink()
try {
await handleLinkAction(envParams.deeplink)
} catch (err: any) {
showErrorDialog(err.message)
focusWindow()
}
}
isInited = true
}
}