diff --git a/src/app/(main)/character/[id]/ChatButton.tsx b/src/app/(main)/character/[id]/ChatButton.tsx index 5656c80..ddc2956 100644 --- a/src/app/(main)/character/[id]/ChatButton.tsx +++ b/src/app/(main)/character/[id]/ChatButton.tsx @@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button'; import type React from 'react'; import { useAsyncFn } from '@/hooks/tools'; import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat'; +import { toast } from 'sonner'; export default function ChatButton({ id, @@ -13,9 +14,16 @@ export default function ChatButton({ const createChannel = useStreamChatStore((s) => s.createChannel); const { loading, run: createChannelAndPush } = useAsyncFn(async () => { - const channelId = await createChannel(id); - if (!channelId) return; - router.push(`/chat/${channelId}`); + const res = await createChannel(id); + if (res.result === 'error') { + if (res.error === 'need_auth') { + router.push(`/login?redirect=${encodeURIComponent(`/character/${id}`)}`); + } else { + toast.error(res.error); + } + return; + } + router.push(`/chat/${res.channelId}`); }); return ( diff --git a/src/app/(main)/chat/[id]/Drawer/Profile.tsx b/src/app/(main)/chat/[id]/Drawer/Profile.tsx index 38e7475..3e1c783 100644 --- a/src/app/(main)/chat/[id]/Drawer/Profile.tsx +++ b/src/app/(main)/chat/[id]/Drawer/Profile.tsx @@ -15,6 +15,7 @@ import { useStreamChatStore } from '@/app/(main)/chat/[id]/stream-chat'; import { useAsyncFn } from '@/hooks/tools'; import { useRouter } from 'next/navigation'; import { useModels } from '@/hooks/services/chat'; +import { toast } from 'sonner'; const genderMap = { 0: '/icons/male.svg', @@ -90,8 +91,11 @@ export default function Profile({ onActiveTab }: ProfileProps) { const deleteChannel = useStreamChatStore((s) => s.deleteChannel); const { loading: creating, run: createChannelAndPush } = useAsyncFn(async () => { - const channelId = await createChannel(characterId); - if (!channelId) return; + const { result, channelId, error } = await createChannel(characterId); + if (result === 'error') { + toast.error(error); + return; + } router.push(`/chat/${channelId}`); }); diff --git a/src/app/(main)/chat/[id]/stream-chat.ts b/src/app/(main)/chat/[id]/stream-chat.ts index 44b0bef..4a7c0f7 100644 --- a/src/app/(main)/chat/[id]/stream-chat.ts +++ b/src/app/(main)/chat/[id]/stream-chat.ts @@ -47,7 +47,9 @@ interface StreamChatStore { updateUserChatSetting: () => Promise; // 创建某个角色的聊天频道, 返回channelId - createChannel: (characterId: string) => Promise; + createChannel: ( + characterId: string + ) => Promise<{ result: 'ok' | 'error'; channelId?: string; error?: 'need_auth' | string }>; switchToChannel: (id: string) => Promise; queryChannels: (filter: any) => Promise; deleteChannel: ( @@ -98,19 +100,21 @@ export const useStreamChatStore = create((set, get) => ({ const { user, client } = get(); const { switchToChannel, queryChannels } = get(); if (!client) { - return false; + return { result: 'error', error: 'need_auth' }; } - const { data } = await createChannel({ - userId: user.userId, - userName: user.userName, - characterId, - }); + const { data } = await protect(() => + createChannel({ + userId: user.userId, + userName: user.userName, + characterId, + }) + ); if (!data?.channelId) { - return false; + return { result: 'error', error: 'create_channel_failed' }; } await queryChannels({}); switchToChannel(data.channelId); - return data.channelId; + return { result: 'ok', channelId: data.channelId }; }, setChatSetting: (setting: any) => {