Skip to content

Commit

Permalink
fix: resolve state update loop infinitive rerendering (janhq#2017)
Browse files Browse the repository at this point in the history
* fix: resolve state update loop infinitive rerendering

* fix: thread creation issue
  • Loading branch information
louis-jan authored Feb 14, 2024
1 parent 3b51f3d commit f2e3187
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
35 changes: 27 additions & 8 deletions web/containers/ServerLogs/index.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -23,15 +23,34 @@ const ServerLogs = (props: ServerLogsProps) => {

const clipboard = useClipboard({ timeout: 1000 })

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
[]
)

useEffect(() => {
getLogs('server').then((log) => {
if (typeof log?.split === 'function') {
setLogs(log.split(/\r?\n|\r|\n/g))
}
})
if (serverEnabled) {
updateLogs()
}
}, [serverEnabled, updateLogs])

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [logs, serverEnabled])
useEffect(() => {
updateLogs()

// Log polling interval
const intervalId = setInterval(() => {
updateLogs()
}, window.core?.api?.pollingInterval ?? 1000)

// clean up interval
return () => clearInterval(intervalId)
}, [updateLogs])

return (
<>
Expand Down
6 changes: 2 additions & 4 deletions web/hooks/useCreateNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import useSetActiveThread from './useSetActiveThread'

import { extensionManager } from '@/extension'

import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
import {
threadsAtom,
threadStatesAtom,
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
9 changes: 3 additions & 6 deletions web/hooks/useLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions web/services/restService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export const restAPI = {
openExternalUrl,
// Jan Server URL
baseApiUrl: API_BASE_URL,
pollingInterval: 5000,
}

0 comments on commit f2e3187

Please sign in to comment.