fix: deeplink when app not open on linux (#2893)
Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
1e0d4f3753
commit
08d15e5dc7
@ -29,7 +29,7 @@ class WindowManager {
|
||||
},
|
||||
})
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
if (process.platform === 'win32' || process.platform === 'linux') {
|
||||
/// This is work around for windows deeplink.
|
||||
/// second-instance event is not fired when app is not open, so the app
|
||||
/// does not received the deeplink.
|
||||
|
||||
@ -6,12 +6,10 @@ export type LoadingInfo = {
|
||||
message: string
|
||||
}
|
||||
|
||||
export const loadingModalVisibilityAtom = atom<LoadingInfo | undefined>(
|
||||
undefined
|
||||
)
|
||||
export const loadingModalInfoAtom = atom<LoadingInfo | undefined>(undefined)
|
||||
|
||||
const ResettingModal: React.FC = () => {
|
||||
const loadingInfo = useAtomValue(loadingModalVisibilityAtom)
|
||||
const loadingInfo = useAtomValue(loadingModalInfoAtom)
|
||||
|
||||
return (
|
||||
<Modal open={loadingInfo != null}>
|
||||
|
||||
@ -6,7 +6,7 @@ import { useDebouncedCallback } from 'use-debounce'
|
||||
|
||||
import { useGetHFRepoData } from '@/hooks/useGetHFRepoData'
|
||||
|
||||
import { loadingModalVisibilityAtom as loadingModalInfoAtom } from '../LoadingModal'
|
||||
import { loadingModalInfoAtom } from '../LoadingModal'
|
||||
import { toaster } from '../Toast'
|
||||
|
||||
import {
|
||||
@ -27,13 +27,15 @@ const DeepLinkListener: React.FC<Props> = ({ children }) => {
|
||||
importHuggingFaceModelStageAtom
|
||||
)
|
||||
|
||||
const debounced = useDebouncedCallback(async (searchText) => {
|
||||
if (searchText.indexOf('/') === -1) {
|
||||
toaster({
|
||||
title: 'Failed to get Hugging Face models',
|
||||
description: 'Invalid Hugging Face model URL',
|
||||
type: 'error',
|
||||
})
|
||||
const handleDeepLinkAction = useDebouncedCallback(
|
||||
async (deepLinkAction: DeepLinkAction) => {
|
||||
if (
|
||||
deepLinkAction.action !== 'models' ||
|
||||
deepLinkAction.provider !== 'huggingface'
|
||||
) {
|
||||
console.error(
|
||||
`Invalid deeplink action (${deepLinkAction.action}) or provider (${deepLinkAction.provider})`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
@ -42,31 +44,58 @@ const DeepLinkListener: React.FC<Props> = ({ children }) => {
|
||||
title: 'Getting Hugging Face models',
|
||||
message: 'Please wait..',
|
||||
})
|
||||
const data = await getHfRepoData(searchText)
|
||||
const data = await getHfRepoData(deepLinkAction.resource)
|
||||
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',
|
||||
description: errMessage,
|
||||
description: err instanceof Error ? err.message : 'Unexpected Error',
|
||||
type: 'error',
|
||||
})
|
||||
console.error(err)
|
||||
}
|
||||
}, 300)
|
||||
},
|
||||
300
|
||||
)
|
||||
|
||||
window.electronAPI?.onDeepLink((_event: string, input: string) => {
|
||||
window.core?.api?.ackDeepLink()
|
||||
const url = input.replaceAll('jan://', '')
|
||||
debounced(url)
|
||||
|
||||
const action = deeplinkParser(input)
|
||||
if (!action) return
|
||||
handleDeepLinkAction(action)
|
||||
})
|
||||
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user