refactor: 优化eslint配置,提升代码质量 (#1341)
* feat: 统一并标准化eslint * lint: napcat.webui * lint: napcat.webui * lint: napcat.core * build: fix * lint: napcat.webui * refactor: 重构eslint * Update README.md
This commit is contained in:
651 files changed
+37978
-37584
No files matched your search
@@ -1,15 +1,15 @@
|
||||
import { Button } from '@heroui/button'
|
||||
import { Card, CardBody, CardHeader } from '@heroui/card'
|
||||
import { Select, SelectItem } from '@heroui/select'
|
||||
import type { Selection } from '@react-types/shared'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { Button } from '@heroui/button';
|
||||
import { Card, CardBody, CardHeader } from '@heroui/card';
|
||||
import { Select, SelectItem } from '@heroui/select';
|
||||
import type { Selection } from '@react-types/shared';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { colorizeLogLevel } from '@/utils/terminal'
|
||||
import { colorizeLogLevel } from '@/utils/terminal';
|
||||
|
||||
import PageLoading from '../page_loading'
|
||||
import XTerm from '../xterm'
|
||||
import type { XTermRef } from '../xterm'
|
||||
import LogLevelSelect from './log_level_select'
|
||||
import PageLoading from '../page_loading';
|
||||
import XTerm from '../xterm';
|
||||
import type { XTermRef } from '../xterm';
|
||||
import LogLevelSelect from './log_level_select';
|
||||
|
||||
export interface HistoryLogsProps {
|
||||
list: string[]
|
||||
@@ -32,80 +32,80 @@ const HistoryLogs: React.FC<HistoryLogsProps> = (props) => {
|
||||
listLoading,
|
||||
logContent,
|
||||
listError,
|
||||
logLoading
|
||||
} = props
|
||||
const Xterm = useRef<XTermRef>(null)
|
||||
logLoading,
|
||||
} = props;
|
||||
const Xterm = useRef<XTermRef>(null);
|
||||
|
||||
const [logLevel, setLogLevel] = useState<Selection>(
|
||||
new Set(['info', 'warn', 'error'])
|
||||
)
|
||||
);
|
||||
|
||||
const logToColored = (log: string) => {
|
||||
const logs = log
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
const colored = colorizeLogLevel(line)
|
||||
return colored
|
||||
const colored = colorizeLogLevel(line);
|
||||
return colored;
|
||||
})
|
||||
.filter((log) => {
|
||||
if (logLevel === 'all') {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return logLevel.has(log.level)
|
||||
return logLevel.has(log.level);
|
||||
})
|
||||
.map((log) => log.content)
|
||||
.join('\r\n')
|
||||
return logs
|
||||
}
|
||||
.join('\r\n');
|
||||
return logs;
|
||||
};
|
||||
|
||||
const onDownloadLog = () => {
|
||||
if (!logContent) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
const blob = new Blob([logContent], { type: 'text/plain' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${selectedLog}.log`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
const blob = new Blob([logContent], { type: 'text/plain' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${selectedLog}.log`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!Xterm.current || !logContent) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
Xterm.current.clear()
|
||||
const _logContent = logToColored(logContent)
|
||||
Xterm.current.write(_logContent + '\r\nnapcat@webui:~$ ')
|
||||
}, [logContent, logLevel])
|
||||
Xterm.current.clear();
|
||||
const _logContent = logToColored(logContent);
|
||||
Xterm.current.write(_logContent + '\r\nnapcat@webui:~$ ');
|
||||
}, [logContent, logLevel]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<title>历史日志 - NapCat WebUI</title>
|
||||
<Card className="max-w-full h-full bg-opacity-50 backdrop-blur-sm">
|
||||
<CardHeader className="flex-row justify-start gap-3">
|
||||
<Card className='max-w-full h-full bg-opacity-50 backdrop-blur-sm'>
|
||||
<CardHeader className='flex-row justify-start gap-3'>
|
||||
<Select
|
||||
label="选择日志"
|
||||
size="sm"
|
||||
label='选择日志'
|
||||
size='sm'
|
||||
isLoading={listLoading}
|
||||
errorMessage={listError?.message}
|
||||
classNames={{
|
||||
trigger:
|
||||
'hover:!bg-content3 bg-opacity-50 backdrop-blur-sm hover:!bg-opacity-60'
|
||||
'hover:!bg-content3 bg-opacity-50 backdrop-blur-sm hover:!bg-opacity-60',
|
||||
}}
|
||||
placeholder="选择日志"
|
||||
placeholder='选择日志'
|
||||
onChange={(e) => {
|
||||
const value = e.target.value
|
||||
const value = e.target.value;
|
||||
if (!value) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
onSelect(value)
|
||||
onSelect(value);
|
||||
}}
|
||||
selectedKeys={[selectedLog || '']}
|
||||
items={list.map((name) => ({
|
||||
value: name,
|
||||
label: name
|
||||
label: name,
|
||||
}))}
|
||||
>
|
||||
{(item) => (
|
||||
@@ -118,19 +118,19 @@ const HistoryLogs: React.FC<HistoryLogsProps> = (props) => {
|
||||
selectedKeys={logLevel}
|
||||
onSelectionChange={setLogLevel}
|
||||
/>
|
||||
<Button className="flex-shrink-0" onPress={onDownloadLog}>
|
||||
<Button className='flex-shrink-0' onPress={onDownloadLog}>
|
||||
下载日志
|
||||
</Button>
|
||||
<Button onPress={refreshList}>刷新列表</Button>
|
||||
<Button onPress={refreshLog}>刷新日志</Button>
|
||||
</CardHeader>
|
||||
<CardBody className="relative">
|
||||
<CardBody className='relative'>
|
||||
<PageLoading loading={logLoading} />
|
||||
<XTerm className="w-full h-full" ref={Xterm} />
|
||||
<XTerm className='w-full h-full' ref={Xterm} />
|
||||
</CardBody>
|
||||
</Card>
|
||||
</>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default HistoryLogs
|
||||
export default HistoryLogs;
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Chip } from '@heroui/chip'
|
||||
import { Select, SelectItem } from '@heroui/select'
|
||||
import { SharedSelection } from '@heroui/system'
|
||||
import type { Selection } from '@react-types/shared'
|
||||
import { Chip } from '@heroui/chip';
|
||||
import { Select, SelectItem } from '@heroui/select';
|
||||
import { SharedSelection } from '@heroui/system';
|
||||
import type { Selection } from '@react-types/shared';
|
||||
|
||||
import { LogLevel } from '@/const/enum'
|
||||
import { LogLevel } from '@/const/enum';
|
||||
|
||||
export interface LogLevelSelectProps {
|
||||
selectedKeys: Selection
|
||||
@@ -22,57 +22,57 @@ const logLevelColor: {
|
||||
[LogLevel.INFO]: 'primary',
|
||||
[LogLevel.WARN]: 'warning',
|
||||
[LogLevel.ERROR]: 'primary',
|
||||
[LogLevel.FATAL]: 'primary'
|
||||
}
|
||||
[LogLevel.FATAL]: 'primary',
|
||||
};
|
||||
const LogLevelSelect = (props: LogLevelSelectProps) => {
|
||||
const { selectedKeys, onSelectionChange } = props
|
||||
const { selectedKeys, onSelectionChange } = props;
|
||||
return (
|
||||
<Select
|
||||
selectedKeys={selectedKeys}
|
||||
onSelectionChange={(selectedKeys) => {
|
||||
if (selectedKeys !== 'all' && selectedKeys?.size === 0) {
|
||||
selectedKeys = 'all'
|
||||
selectedKeys = 'all';
|
||||
}
|
||||
onSelectionChange(selectedKeys)
|
||||
onSelectionChange(selectedKeys);
|
||||
}}
|
||||
label="日志级别"
|
||||
selectionMode="multiple"
|
||||
aria-label="Log Level"
|
||||
label='日志级别'
|
||||
selectionMode='multiple'
|
||||
aria-label='Log Level'
|
||||
classNames={{
|
||||
label: 'mb-2',
|
||||
trigger: 'bg-opacity-50 backdrop-blur-sm hover:!bg-opacity-60',
|
||||
popoverContent: 'bg-opacity-50 backdrop-blur-sm'
|
||||
popoverContent: 'bg-opacity-50 backdrop-blur-sm',
|
||||
}}
|
||||
size="sm"
|
||||
size='sm'
|
||||
items={[
|
||||
{ label: 'Debug', value: LogLevel.DEBUG },
|
||||
{ label: 'Info', value: LogLevel.INFO },
|
||||
{ label: 'Warn', value: LogLevel.WARN },
|
||||
{ label: 'Error', value: LogLevel.ERROR },
|
||||
{ label: 'Fatal', value: LogLevel.FATAL }
|
||||
{ label: 'Fatal', value: LogLevel.FATAL },
|
||||
]}
|
||||
renderValue={(value) => {
|
||||
if (value.length === 5) {
|
||||
return (
|
||||
<Chip size="sm" color="primary" variant="flat">
|
||||
<Chip size='sm' color='primary' variant='flat'>
|
||||
全部
|
||||
</Chip>
|
||||
)
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<div className='flex gap-2'>
|
||||
{value.map((v) => (
|
||||
<Chip
|
||||
size="sm"
|
||||
size='sm'
|
||||
key={v.key}
|
||||
color={logLevelColor[v.data?.value as LogLevel]}
|
||||
variant="flat"
|
||||
variant='flat'
|
||||
>
|
||||
{v.data?.label}
|
||||
</Chip>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}}
|
||||
>
|
||||
{(item) => (
|
||||
@@ -81,7 +81,7 @@ const LogLevelSelect = (props: LogLevelSelectProps) => {
|
||||
</SelectItem>
|
||||
)}
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default LogLevelSelect
|
||||
export default LogLevelSelect;
|
||||
@@ -1,114 +1,114 @@
|
||||
import { Button } from '@heroui/button'
|
||||
import type { Selection } from '@react-types/shared'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { IoDownloadOutline } from 'react-icons/io5'
|
||||
import { Button } from '@heroui/button';
|
||||
import type { Selection } from '@react-types/shared';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import toast from 'react-hot-toast';
|
||||
import { IoDownloadOutline } from 'react-icons/io5';
|
||||
|
||||
import { colorizeLogLevelWithTag } from '@/utils/terminal'
|
||||
import { colorizeLogLevelWithTag } from '@/utils/terminal';
|
||||
|
||||
import WebUIManager, { Log } from '@/controllers/webui_manager'
|
||||
import WebUIManager, { Log } from '@/controllers/webui_manager';
|
||||
|
||||
import type { XTermRef } from '../xterm'
|
||||
import XTerm from '../xterm'
|
||||
import LogLevelSelect from './log_level_select'
|
||||
import type { XTermRef } from '../xterm';
|
||||
import XTerm from '../xterm';
|
||||
import LogLevelSelect from './log_level_select';
|
||||
|
||||
const RealTimeLogs = () => {
|
||||
const Xterm = useRef<XTermRef>(null)
|
||||
const Xterm = useRef<XTermRef>(null);
|
||||
const [logLevel, setLogLevel] = useState<Selection>(
|
||||
new Set(['info', 'warn', 'error'])
|
||||
)
|
||||
const [dataArr, setDataArr] = useState<Log[]>([])
|
||||
);
|
||||
const [dataArr, setDataArr] = useState<Log[]>([]);
|
||||
|
||||
const onDownloadLog = () => {
|
||||
const logContent = dataArr
|
||||
.filter((log) => {
|
||||
if (logLevel === 'all') {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return logLevel.has(log.level)
|
||||
return logLevel.has(log.level);
|
||||
})
|
||||
.map((log) => colorizeLogLevelWithTag(log.message, log.level))
|
||||
.join('\r\n')
|
||||
const blob = new Blob([logContent], { type: 'text/plain' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = 'napcat.log'
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
.join('\r\n');
|
||||
const blob = new Blob([logContent], { type: 'text/plain' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'napcat.log';
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
const writeStream = () => {
|
||||
try {
|
||||
const _data = dataArr
|
||||
.filter((log) => {
|
||||
if (logLevel === 'all') {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return logLevel.has(log.level)
|
||||
return logLevel.has(log.level);
|
||||
})
|
||||
.map((log) => colorizeLogLevelWithTag(log.message, log.level))
|
||||
.join('\r\n')
|
||||
Xterm.current?.clear()
|
||||
Xterm.current?.write(_data)
|
||||
.join('\r\n');
|
||||
Xterm.current?.clear();
|
||||
Xterm.current?.write(_data);
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error('获取实时日志失败')
|
||||
console.error(error);
|
||||
toast.error('获取实时日志失败');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
writeStream()
|
||||
}, [logLevel, dataArr])
|
||||
writeStream();
|
||||
}, [logLevel, dataArr]);
|
||||
|
||||
useEffect(() => {
|
||||
const subscribeLogs = () => {
|
||||
try {
|
||||
const source = WebUIManager.getRealTimeLogs((data) => {
|
||||
setDataArr((prev) => {
|
||||
const newData = [...prev, ...data]
|
||||
const newData = [...prev, ...data];
|
||||
if (newData.length > 1000) {
|
||||
newData.splice(0, newData.length - 1000)
|
||||
newData.splice(0, newData.length - 1000);
|
||||
}
|
||||
return newData
|
||||
})
|
||||
})
|
||||
return newData;
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
source.close()
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error('获取实时日志失败')
|
||||
source.close();
|
||||
};
|
||||
} catch (_error) {
|
||||
toast.error('获取实时日志失败');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const close = subscribeLogs()
|
||||
const close = subscribeLogs();
|
||||
return () => {
|
||||
console.log('close')
|
||||
close?.()
|
||||
}
|
||||
}, [])
|
||||
console.log('close');
|
||||
close?.();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<title>实时日志 - NapCat WebUI</title>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className='flex items-center gap-2'>
|
||||
<LogLevelSelect
|
||||
selectedKeys={logLevel}
|
||||
onSelectionChange={setLogLevel}
|
||||
/>
|
||||
<Button
|
||||
className="flex-shrink-0"
|
||||
className='flex-shrink-0'
|
||||
onPress={onDownloadLog}
|
||||
startContent={<IoDownloadOutline className="text-lg" />}
|
||||
startContent={<IoDownloadOutline className='text-lg' />}
|
||||
>
|
||||
下载日志
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex-1 h-full overflow-hidden">
|
||||
<div className='flex-1 h-full overflow-hidden'>
|
||||
<XTerm ref={Xterm} />
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default RealTimeLogs
|
||||
export default RealTimeLogs;
|
||||
Reference in new issue
Block a user