feat: add DeepChat deeplink support (#4668)
This commit is contained in:
+7
-28
@@ -1,14 +1,15 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Link, createFileRoute, redirect } from '@tanstack/react-router'
|
||||
import { Loader2, MessageCircleWarning } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { useActiveChatKey } from '@/features/chat/hooks/use-active-chat-key'
|
||||
import { useChatPresets } from '@/features/chat/hooks/use-chat-presets'
|
||||
import { resolveChatUrl } from '@/features/chat/lib/chat-links'
|
||||
import { getApiKeys } from '@/features/keys/api'
|
||||
import { API_KEY_STATUS } from '@/features/keys/constants'
|
||||
import {
|
||||
chatLinkRequiresApiKey,
|
||||
resolveChatUrl,
|
||||
} from '@/features/chat/lib/chat-links'
|
||||
|
||||
export const Route = createFileRoute('/_authenticated/chat/$chatId')({
|
||||
loader: async ({ params }) => {
|
||||
@@ -33,8 +34,7 @@ function ChatRouteComponent() {
|
||||
|
||||
const requiresActiveKey = useMemo(() => {
|
||||
if (!preset || !isWebLink) return false
|
||||
const url = preset.url ?? ''
|
||||
return url.includes('{key}') || url.includes('{cherryConfig}')
|
||||
return chatLinkRequiresApiKey(preset.url ?? '')
|
||||
}, [isWebLink, preset])
|
||||
|
||||
const {
|
||||
@@ -42,28 +42,7 @@ function ChatRouteComponent() {
|
||||
isPending,
|
||||
isError,
|
||||
error,
|
||||
} = useQuery({
|
||||
queryKey: ['chat-active-key'],
|
||||
queryFn: async () => {
|
||||
const result = await getApiKeys({ p: 1, size: 50 })
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || 'Failed to load API keys')
|
||||
}
|
||||
const items = result.data?.items ?? []
|
||||
const active = items.find(
|
||||
(item) => item.status === API_KEY_STATUS.ENABLED
|
||||
)
|
||||
if (!active) {
|
||||
throw new Error(
|
||||
'No enabled API key available. Please enable an API key first.'
|
||||
)
|
||||
}
|
||||
return active.key
|
||||
},
|
||||
enabled: Boolean(preset && requiresActiveKey),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
gcTime: 10 * 60 * 1000,
|
||||
})
|
||||
} = useActiveChatKey(Boolean(preset && requiresActiveKey))
|
||||
|
||||
const iframeSrc = useMemo(() => {
|
||||
if (!preset || !isWebLink) return ''
|
||||
|
||||
+12
-19
@@ -1,13 +1,11 @@
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { toast } from 'sonner'
|
||||
import { useActiveChatKey } from '@/features/chat/hooks/use-active-chat-key'
|
||||
import { useChatPresets } from '@/features/chat/hooks/use-chat-presets'
|
||||
import { resolveChatUrl } from '@/features/chat/lib/chat-links'
|
||||
import { getApiKeys } from '@/features/keys/api'
|
||||
import { API_KEY_STATUS } from '@/features/keys/constants'
|
||||
|
||||
export const Route = createFileRoute('/_authenticated/chat2link')({
|
||||
component: Chat2LinkPage,
|
||||
@@ -23,19 +21,9 @@ function Chat2LinkPage() {
|
||||
[chatPresets]
|
||||
)
|
||||
|
||||
const { data: activeKey } = useQuery({
|
||||
queryKey: ['chat2link-active-key'],
|
||||
queryFn: async () => {
|
||||
const result = await getApiKeys({ p: 1, size: 50 })
|
||||
if (!result.success) throw new Error(result.message)
|
||||
const items = result.data?.items ?? []
|
||||
const active = items.find(
|
||||
(item) => item.status === API_KEY_STATUS.ENABLED
|
||||
)
|
||||
return active?.key ?? null
|
||||
},
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
const { data: activeKey, error: keyError } = useActiveChatKey(
|
||||
Boolean(firstWebPreset)
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!firstWebPreset) {
|
||||
@@ -45,10 +33,14 @@ function Chat2LinkPage() {
|
||||
return
|
||||
}
|
||||
|
||||
if (activeKey === undefined) return
|
||||
if (activeKey === undefined && !keyError) return
|
||||
|
||||
if (!activeKey) {
|
||||
toast.error(t('No enabled tokens available'))
|
||||
if (keyError || !activeKey) {
|
||||
const message =
|
||||
keyError instanceof Error
|
||||
? keyError.message
|
||||
: t('No enabled tokens available')
|
||||
toast.error(message)
|
||||
navigate({ to: '/keys' })
|
||||
return
|
||||
}
|
||||
@@ -65,6 +57,7 @@ function Chat2LinkPage() {
|
||||
}, [
|
||||
firstWebPreset,
|
||||
activeKey,
|
||||
keyError,
|
||||
serverAddress,
|
||||
chatPresets.length,
|
||||
navigate,
|
||||
|
||||
Reference in New Issue
Block a user