jan/web/hooks/useLogs.ts
NamH 02478b3242
feat: add input actions for setting item (#2978)
Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
2024-06-02 22:41:27 +07:00

38 lines
1.1 KiB
TypeScript

import { useCallback } from 'react'
import { fs, joinPath, openFileExplorer } from '@janhq/core'
import { useAtomValue } from 'jotai'
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
export const useLogs = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const getLogs = useCallback(
async (file: string): Promise<string> => {
const path = await joinPath(['file://logs', `${file}.log`])
if (!(await fs.existsSync(path))) return ''
const logs = await fs.readFileSync(path, 'utf-8')
const sanitizedLogs = logs.replace(
new RegExp(`${janDataFolderPath}\\/`, 'g'),
'jan-data-folder/'
)
return sanitizedLogs
},
[janDataFolderPath]
)
const openServerLog = useCallback(async () => {
const fullPath = await joinPath([janDataFolderPath, 'logs', 'app.log'])
return openFileExplorer(fullPath)
}, [janDataFolderPath])
const clearServerLog = useCallback(async () => {
await fs.writeFileSync(await joinPath(['file://logs', 'app.log']), '')
}, [])
return { getLogs, openServerLog, clearServerLog }
}