Skip to content

Commit

Permalink
Fix TypeError Failed to parse URL
Browse files Browse the repository at this point in the history
  • Loading branch information
0xinhua committed Jun 18, 2024
1 parent c84791f commit 0958d9b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
8 changes: 4 additions & 4 deletions app/(chat)/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export default function ChatPage({ params }: ChatPageProps) {
}, [session?.user.id])

const [loading, setLoading] = useState(false)
const { chat, chatLoading, fetchChatById } = useChatStore()
const { chat, chatLoading, fetchChatById, fetchHistory } = useChatStore()

useEffect(() => {
const fetchChat = async () => {
setLoading(true)
fetchChatById(params.id)
setLoading(false)
}
fetchChat()
},
[])
fetchChat()
fetchHistory()
}, [])

return <div>
<Suspense>
Expand Down
12 changes: 12 additions & 0 deletions app/(chat)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
import { nanoid } from '@/lib/utils'
import { Chat } from '@/components/chat'
import useUserSettingStore from '@/store/useSettingStore'
import { useEffect } from 'react'
import useChatStore from '@/store/useChatStore'

export default function IndexPage() {

const {
systemPrompt,
fetchSystemPrompt
} = useUserSettingStore()

const {
fetchHistory,
} = useChatStore()

const id = nanoid()

useEffect(() => {
fetchHistory()
fetchSystemPrompt()
},[])

return <Chat
id={id}
initialMessages={[{
Expand Down
16 changes: 3 additions & 13 deletions store/useChatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ export interface ChatState {
const useChatStore = create<ChatState>()(
persist(
(set) => {
// init history data
(async () => {
if (isLocalMode) {
const localChatState = await localForage.get('chat-history') as { state: ChatState } || null
set({ chats: localChatState?.state?.chats || [] })
} else {
const response = await fetch('/api/chats')
const { data } = await response.json()
console.log('init history data', data)
set({ chats: data })
}
})()

return {
chats: [],
chat: null,
Expand All @@ -46,6 +33,9 @@ const useChatStore = create<ChatState>()(
const { data } = await response.json()
console.log('fetch history data', data)
set({ chats: data })
} else {
const localChatState = await localForage.get('chat-history') as { state: ChatState } || null
set({ chats: localChatState?.state?.chats || [] })
}
},
setChat: (chat: Chat) => set({chat}),
Expand Down
23 changes: 5 additions & 18 deletions store/useSettingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ const useUserSettingStore = create<UserSettingState>()(
persist(
(set, get) => {

(async () => {
if (isLocalMode) {
const localChatSetting = await localForage.get('user-setting') as { state: UserSettingState } || null
set({ systemPrompt: localChatSetting?.state?.systemPrompt || defaultSystemPrompt })
} else {
const response = await fetch('/api/user/settings/systemPrompt')
const { data } = await response.json()
set({ systemPrompt: data.prompt || defaultSystemPrompt })
}
})()

return {
isSettingsDialogOpen: false,
systemPrompt: defaultSystemPrompt,
Expand All @@ -44,20 +33,18 @@ const useUserSettingStore = create<UserSettingState>()(

fetchSystemPrompt: async () => {

if (!isLocalMode) {

if (isLocalMode) {
const localChatSetting = await localForage.get('user-setting') as { state: UserSettingState } || null
set({ systemPrompt: localChatSetting?.state?.systemPrompt || defaultSystemPrompt })
} else {
set({ loading: true, error: null })

try {
const response = await fetch('/api/user/settings/systemPrompt')

if (!response.ok) {
throw new Error('Failed to fetch system prompt')
}

const json = await response.json()

set({
set({
systemPrompt: json.data?.prompt ? json.data.prompt : defaultSystemPrompt,
loading: false
})
Expand Down

0 comments on commit 0958d9b

Please sign in to comment.