Skip to content

Commit

Permalink
fix: replaced user path from app log
Browse files Browse the repository at this point in the history
Signed-off-by: James <[email protected]>
  • Loading branch information
James committed Mar 5, 2024
1 parent e6c1020 commit aa0a414
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
47 changes: 29 additions & 18 deletions web/hooks/useLogs.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import {
fs,
joinPath,
openFileExplorer,
getJanDataFolderPath,
} from '@janhq/core'
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 getLogs = async (file: string) => {
const path = await joinPath(['file:https://logs', `${file}.log`])
if (!(await fs.existsSync(path))) return {}
const logs = await fs.readFileSync(path, 'utf-8')

return logs
}
const openServerLog = async () => {
const janDataFolderPath = await getJanDataFolderPath()
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)

const getLogs = useCallback(
async (file: string): Promise<string> => {
const path = await joinPath(['file:https://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', 'server.log'])
return openFileExplorer(fullPath)
}
}, [janDataFolderPath])

const clearServerLog = async () => {
const clearServerLog = useCallback(async () => {
await fs.writeFileSync(await joinPath(['file:https://logs', 'server.log']), '')
}
}, [])

return { getLogs, openServerLog, clearServerLog }
}
21 changes: 7 additions & 14 deletions web/hooks/usePath.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import {
openFileExplorer,
joinPath,
getJanDataFolderPath,
baseName,
} from '@janhq/core'
import { openFileExplorer, joinPath, baseName } from '@janhq/core'
import { useAtomValue } from 'jotai'

import { selectedModelAtom } from '@/containers/DropdownListSidebar'

import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'

export const usePath = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const activeThread = useAtomValue(activeThreadAtom)
const selectedModel = useAtomValue(selectedModelAtom)

const onRevealInFinder = async (type: string) => {
// TODO: this logic should be refactored.
if (type !== 'Model' && !activeThread) return

const userSpace = await getJanDataFolderPath()
let filePath = undefined
const assistantId = activeThread?.assistants[0]?.assistant_id
switch (type) {
Expand All @@ -40,15 +36,14 @@ export const usePath = () => {
}

if (!filePath) return
const fullPath = await joinPath([userSpace, filePath])
const fullPath = await joinPath([janDataFolderPath, filePath])
openFileExplorer(fullPath)
}

const onViewJson = async (type: string) => {
// TODO: this logic should be refactored.
if (type !== 'Model' && !activeThread) return

const userSpace = await getJanDataFolderPath()
let filePath = undefined
const assistantId = activeThread?.assistants[0]?.assistant_id
switch (type) {
Expand All @@ -74,31 +69,29 @@ export const usePath = () => {
}

if (!filePath) return
const fullPath = await joinPath([userSpace, filePath])
const fullPath = await joinPath([janDataFolderPath, filePath])
openFileExplorer(fullPath)
}

const onViewFile = async (id: string) => {
if (!activeThread) return

const userSpace = await getJanDataFolderPath()
let filePath = undefined

id = await baseName(id)
filePath = await joinPath(['threads', `${activeThread.id}/files`, `${id}`])
if (!filePath) return
const fullPath = await joinPath([userSpace, filePath])
const fullPath = await joinPath([janDataFolderPath, filePath])
openFileExplorer(fullPath)
}

const onViewFileContainer = async () => {
if (!activeThread) return

const userSpace = await getJanDataFolderPath()
let filePath = undefined
filePath = await joinPath(['threads', `${activeThread.id}/files`])
if (!filePath) return
const fullPath = await joinPath([userSpace, filePath])
const fullPath = await joinPath([janDataFolderPath, filePath])
openFileExplorer(fullPath)
}

Expand Down

0 comments on commit aa0a414

Please sign in to comment.