fix: deeplink when app not open on linux (#2893)

Co-authored-by: James <james@jan.ai>
This commit is contained in:
NamH 2024-05-13 16:47:27 +07:00 committed by GitHub
parent 1e0d4f3753
commit 08d15e5dc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 40 deletions

View File

@ -29,7 +29,7 @@ class WindowManager {
}, },
}) })
if (process.platform === 'win32') { if (process.platform === 'win32' || process.platform === 'linux') {
/// This is work around for windows deeplink. /// This is work around for windows deeplink.
/// second-instance event is not fired when app is not open, so the app /// second-instance event is not fired when app is not open, so the app
/// does not received the deeplink. /// does not received the deeplink.

View File

@ -6,12 +6,10 @@ export type LoadingInfo = {
message: string message: string
} }
export const loadingModalVisibilityAtom = atom<LoadingInfo | undefined>( export const loadingModalInfoAtom = atom<LoadingInfo | undefined>(undefined)
undefined
)
const ResettingModal: React.FC = () => { const ResettingModal: React.FC = () => {
const loadingInfo = useAtomValue(loadingModalVisibilityAtom) const loadingInfo = useAtomValue(loadingModalInfoAtom)
return ( return (
<Modal open={loadingInfo != null}> <Modal open={loadingInfo != null}>

View File

@ -6,7 +6,7 @@ import { useDebouncedCallback } from 'use-debounce'
import { useGetHFRepoData } from '@/hooks/useGetHFRepoData' import { useGetHFRepoData } from '@/hooks/useGetHFRepoData'
import { loadingModalVisibilityAtom as loadingModalInfoAtom } from '../LoadingModal' import { loadingModalInfoAtom } from '../LoadingModal'
import { toaster } from '../Toast' import { toaster } from '../Toast'
import { import {
@ -27,46 +27,75 @@ const DeepLinkListener: React.FC<Props> = ({ children }) => {
importHuggingFaceModelStageAtom importHuggingFaceModelStageAtom
) )
const debounced = useDebouncedCallback(async (searchText) => { const handleDeepLinkAction = useDebouncedCallback(
if (searchText.indexOf('/') === -1) { async (deepLinkAction: DeepLinkAction) => {
toaster({ if (
title: 'Failed to get Hugging Face models', deepLinkAction.action !== 'models' ||
description: 'Invalid Hugging Face model URL', deepLinkAction.provider !== 'huggingface'
type: 'error', ) {
}) console.error(
return `Invalid deeplink action (${deepLinkAction.action}) or provider (${deepLinkAction.provider})`
} )
return
try {
setLoadingInfo({
title: 'Getting Hugging Face models',
message: 'Please wait..',
})
const data = await getHfRepoData(searchText)
setImportingHuggingFaceRepoData(data)
setImportHuggingFaceModelStage('REPO_DETAIL')
setLoadingInfo(undefined)
} catch (err) {
setLoadingInfo(undefined)
let errMessage = 'Unexpected Error'
if (err instanceof Error) {
errMessage = err.message
} }
toaster({
title: 'Failed to get Hugging Face models', try {
description: errMessage, setLoadingInfo({
type: 'error', title: 'Getting Hugging Face models',
}) message: 'Please wait..',
console.error(err) })
} const data = await getHfRepoData(deepLinkAction.resource)
}, 300) setImportingHuggingFaceRepoData(data)
setImportHuggingFaceModelStage('REPO_DETAIL')
setLoadingInfo(undefined)
} catch (err) {
setLoadingInfo(undefined)
toaster({
title: 'Failed to get Hugging Face models',
description: err instanceof Error ? err.message : 'Unexpected Error',
type: 'error',
})
console.error(err)
}
},
300
)
window.electronAPI?.onDeepLink((_event: string, input: string) => { window.electronAPI?.onDeepLink((_event: string, input: string) => {
window.core?.api?.ackDeepLink() window.core?.api?.ackDeepLink()
const url = input.replaceAll('jan://', '')
debounced(url) const action = deeplinkParser(input)
if (!action) return
handleDeepLinkAction(action)
}) })
return <Fragment>{children}</Fragment> return <Fragment>{children}</Fragment>
} }
type DeepLinkAction = {
action: string
provider: string
resource: string
}
const deeplinkParser = (
deepLink: string | undefined
): DeepLinkAction | undefined => {
if (!deepLink) return undefined
try {
const url = new URL(deepLink)
const params = url.pathname.split('/').filter((str) => str.length > 0)
if (params.length < 3) return undefined
const action = params[0]
const provider = params[1]
const resource = params.slice(2).join('/')
return { action, provider, resource }
} catch (err) {
console.error(err)
return undefined
}
}
export default DeepLinkListener export default DeepLinkListener