fix: resolve state update loop infinitive rerendering (#2017)

* fix: resolve state update loop infinitive rerendering

* fix: thread creation issue
This commit is contained in:
Louis 2024-02-14 19:40:18 +07:00 committed by GitHub
parent 3b51f3d1aa
commit f2e31874e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import React from 'react'
@ -23,15 +23,34 @@ const ServerLogs = (props: ServerLogsProps) => {
const clipboard = useClipboard({ timeout: 1000 })
useEffect(() => {
getLogs('server').then((log) => {
if (typeof log?.split === 'function') {
setLogs(log.split(/\r?\n|\r|\n/g))
}
})
const updateLogs = useCallback(
() =>
getLogs('server').then((log) => {
if (typeof log?.split === 'function') {
setLogs(log.split(/\r?\n|\r|\n/g))
}
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [logs, serverEnabled])
[]
)
useEffect(() => {
if (serverEnabled) {
updateLogs()
}
}, [serverEnabled, updateLogs])
useEffect(() => {
updateLogs()
// Log polling interval
const intervalId = setInterval(() => {
updateLogs()
}, window.core?.api?.pollingInterval ?? 1000)
// clean up interval
return () => clearInterval(intervalId)
}, [updateLogs])
return (
<>

View File

@ -25,7 +25,6 @@ import useSetActiveThread from './useSetActiveThread'
import { extensionManager } from '@/extension'
import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
import {
threadsAtom,
threadStatesAtom,
@ -57,7 +56,6 @@ export const useCreateNewThread = () => {
const setFileUpload = useSetAtom(fileUploadAtom)
const setSelectedModel = useSetAtom(selectedModelAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
const messages = useAtomValue(getCurrentChatMessagesAtom)
const { experimentalFeature } = useContext(FeatureToggleContext)
const { recommendedModel, downloadedModels } = useRecommendedModel()
@ -71,9 +69,9 @@ export const useCreateNewThread = () => {
const defaultModel = model ?? recommendedModel ?? downloadedModels[0]
// check last thread message, if there empty last message use can not create thread
const lastMessage = threads[threads.length - 1]?.metadata?.lastMessage
const lastMessage = threads[0]?.metadata?.lastMessage
if (!lastMessage && threads.length && !messages.length) {
if (!lastMessage && threads.length) {
return null
}

View File

@ -7,12 +7,9 @@ import {
export const useLogs = () => {
const getLogs = async (file: string) => {
if (!(await fs.existsSync(await joinPath(['file://logs', `${file}.log`]))))
return {}
const logs = await fs.readFileSync(
await joinPath(['file://logs', `${file}.log`]),
'utf-8'
)
const path = await joinPath(['file://logs', `${file}.log`])
if (!(await fs.existsSync(path))) return {}
const logs = await fs.readFileSync(path, 'utf-8')
return logs
}

View File

@ -46,4 +46,5 @@ export const restAPI = {
openExternalUrl,
// Jan Server URL
baseApiUrl: API_BASE_URL,
pollingInterval: 5000,
}