-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6393 from viown/react-logs
Migrate logs to React
- Loading branch information
Showing
12 changed files
with
256 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Api } from '@jellyfin/sdk'; | ||
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useApi } from 'hooks/useApi'; | ||
import type { AxiosRequestConfig } from 'axios'; | ||
|
||
const fetchServerLogs = async (api?: Api, options?: AxiosRequestConfig) => { | ||
if (!api) { | ||
console.error('[useServerLogs] No API instance available'); | ||
return; | ||
} | ||
|
||
const response = await getSystemApi(api).getServerLogs(options); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useServerLogs = () => { | ||
const { api } = useApi(); | ||
|
||
return useQuery({ | ||
queryKey: [ 'ServerLogs' ], | ||
queryFn: ({ signal }) => fetchServerLogs(api, { signal }), | ||
enabled: !!api | ||
}); | ||
}; |
56 changes: 56 additions & 0 deletions
56
src/apps/dashboard/features/logs/components/LogItemList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import type { LogFile } from '@jellyfin/sdk/lib/generated-client/models/log-file'; | ||
import List from '@mui/material/List'; | ||
import ListItem from '@mui/material/ListItem'; | ||
import ListItemButton from '@mui/material/ListItemButton'; | ||
import ListItemText from '@mui/material/ListItemText'; | ||
import OpenInNewIcon from '@mui/icons-material/OpenInNew'; | ||
import { useApi } from 'hooks/useApi'; | ||
import datetime from 'scripts/datetime'; | ||
|
||
type LogItemProps = { | ||
logs: LogFile[]; | ||
}; | ||
|
||
const LogItemList: FunctionComponent<LogItemProps> = ({ logs }: LogItemProps) => { | ||
const { api } = useApi(); | ||
|
||
// TODO: Use getUri from TS SDK once available. | ||
const getLogFileUrl = (logFile: LogFile) => { | ||
if (!api) return ''; | ||
|
||
let url = api.basePath + '/System/Logs/Log'; | ||
|
||
url += '?name=' + encodeURIComponent(String(logFile.Name)); | ||
url += '&api_key=' + encodeURIComponent(api.accessToken); | ||
|
||
return url; | ||
}; | ||
|
||
const getDate = (logFile: LogFile) => { | ||
const date = datetime.parseISO8601Date(logFile.DateModified, true); | ||
return datetime.toLocaleDateString(date) + ' ' + datetime.getDisplayTime(date); | ||
}; | ||
|
||
return ( | ||
<List sx={{ bgcolor: 'background.paper' }}> | ||
{logs.map(log => { | ||
return ( | ||
<ListItem key={log.Name} disablePadding> | ||
<ListItemButton href={getLogFileUrl(log)} target='_blank'> | ||
<ListItemText | ||
primary={log.Name} | ||
primaryTypographyProps={{ variant: 'h3' }} | ||
secondary={getDate(log)} | ||
secondaryTypographyProps={{ variant: 'body1' }} | ||
/> | ||
<OpenInNewIcon /> | ||
</ListItemButton> | ||
</ListItem> | ||
); | ||
})} | ||
</List> | ||
); | ||
}; | ||
|
||
export default LogItemList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React, { ChangeEvent, useCallback, useEffect, useState } from 'react'; | ||
import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api'; | ||
import Loading from 'components/loading/LoadingComponent'; | ||
import Page from 'components/Page'; | ||
import globalize from 'lib/globalize'; | ||
import Alert from '@mui/material/Alert'; | ||
import Box from '@mui/material/Box'; | ||
import Button from '@mui/material/Button'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import Stack from '@mui/material/Stack'; | ||
import Switch from '@mui/material/Switch'; | ||
import TextField from '@mui/material/TextField'; | ||
import Typography from '@mui/material/Typography'; | ||
import { type ActionFunctionArgs, Form, useActionData } from 'react-router-dom'; | ||
import ServerConnections from 'components/ServerConnections'; | ||
import { useServerLogs } from 'apps/dashboard/features/logs/api/useServerLogs'; | ||
import { useConfiguration } from 'hooks/useConfiguration'; | ||
import type { ServerConfiguration } from '@jellyfin/sdk/lib/generated-client/models/server-configuration'; | ||
import { ActionData } from 'types/actionData'; | ||
import LogItemList from 'apps/dashboard/features/logs/components/LogItemList'; | ||
|
||
export const action = async ({ request }: ActionFunctionArgs) => { | ||
const api = ServerConnections.getCurrentApi(); | ||
if (!api) throw new Error('No Api instance available'); | ||
|
||
const formData = await request.formData(); | ||
const { data: config } = await getConfigurationApi(api).getConfiguration(); | ||
|
||
const enableWarningMessage = formData.get('EnableWarningMessage'); | ||
config.EnableSlowResponseWarning = enableWarningMessage === 'on'; | ||
|
||
const responseTime = formData.get('SlowResponseTime'); | ||
if (responseTime) { | ||
config.SlowResponseThresholdMs = parseInt(responseTime.toString(), 10); | ||
} | ||
|
||
await getConfigurationApi(api) | ||
.updateConfiguration({ serverConfiguration: config }); | ||
|
||
return { | ||
isSaved: true | ||
}; | ||
}; | ||
|
||
const Logs = () => { | ||
const actionData = useActionData() as ActionData | undefined; | ||
const [ isSubmitting, setIsSubmitting ] = useState(false); | ||
|
||
const { isPending: isLogEntriesPending, data: logs } = useServerLogs(); | ||
const { isPending: isConfigurationPending, data: defaultConfiguration } = useConfiguration(); | ||
const [ loading, setLoading ] = useState(true); | ||
const [ configuration, setConfiguration ] = useState<ServerConfiguration>( {} ); | ||
|
||
useEffect(() => { | ||
if (!isConfigurationPending && defaultConfiguration) { | ||
setConfiguration(defaultConfiguration); | ||
setLoading(false); | ||
} | ||
}, [isConfigurationPending, defaultConfiguration]); | ||
|
||
const setLogWarningMessage = useCallback((_: ChangeEvent<HTMLInputElement>, checked: boolean) => { | ||
setConfiguration({ | ||
...configuration, | ||
EnableSlowResponseWarning: checked | ||
}); | ||
}, [configuration]); | ||
|
||
const onResponseTimeChange = useCallback((event: ChangeEvent<HTMLTextAreaElement>) => { | ||
setConfiguration({ | ||
...configuration, | ||
SlowResponseThresholdMs: parseInt(event.target.value, 10) | ||
}); | ||
}, [configuration]); | ||
|
||
const onSubmit = useCallback(() => { | ||
setIsSubmitting(true); | ||
}, []); | ||
|
||
if (isLogEntriesPending || isConfigurationPending || loading || !logs) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<Page | ||
id='logPage' | ||
title={globalize.translate('TabLogs')} | ||
className='mainAnimatedPage type-interior' | ||
> | ||
<Box className='content-primary'> | ||
<Form method='POST' onSubmit={onSubmit}> | ||
<Stack spacing={3}> | ||
<Typography variant='h1'> | ||
{globalize.translate('TabLogs')} | ||
</Typography> | ||
|
||
{isSubmitting && actionData?.isSaved && ( | ||
<Alert severity='success'> | ||
{globalize.translate('SettingsSaved')} | ||
</Alert> | ||
)} | ||
|
||
<FormControlLabel | ||
control={ | ||
<Switch | ||
checked={configuration?.EnableSlowResponseWarning} | ||
onChange={setLogWarningMessage} | ||
name={'EnableWarningMessage'} | ||
/> | ||
} | ||
label={globalize.translate('LabelSlowResponseEnabled')} | ||
/> | ||
|
||
<TextField | ||
fullWidth | ||
type='number' | ||
name={'SlowResponseTime'} | ||
label={globalize.translate('LabelSlowResponseTime')} | ||
value={configuration?.SlowResponseThresholdMs} | ||
disabled={!configuration?.EnableSlowResponseWarning} | ||
onChange={onResponseTimeChange} | ||
/> | ||
|
||
<Button | ||
type='submit' | ||
size='large' | ||
> | ||
{globalize.translate('Save')} | ||
</Button> | ||
</Stack> | ||
</Form> | ||
<Box className='serverLogs readOnlyContent' sx={{ mt: 3 }}> | ||
<LogItemList logs={logs} /> | ||
</Box> | ||
</Box> | ||
</Page> | ||
); | ||
}; | ||
|
||
export default Logs; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.