mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Move data loading to separate hooks
This commit is contained in:
parent
0e54b11c61
commit
4c757fea77
4 changed files with 81 additions and 55 deletions
|
@ -3,7 +3,6 @@ import type { AxiosRequestConfig } from 'axios';
|
||||||
import type { Api } from '@jellyfin/sdk';
|
import type { Api } from '@jellyfin/sdk';
|
||||||
import { getActivityLogApi } from '@jellyfin/sdk/lib/utils/api/activity-log-api';
|
import { getActivityLogApi } from '@jellyfin/sdk/lib/utils/api/activity-log-api';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { useApi } from 'hooks/useApi';
|
import { useApi } from 'hooks/useApi';
|
||||||
|
|
||||||
const fetchLogEntries = async (
|
const fetchLogEntries = async (
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
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';
|
||||||
|
|
||||||
|
const fetchLogEntries = async (api?: Api) => {
|
||||||
|
if (!api) {
|
||||||
|
console.error('[useLogEntries] No API instance available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await getSystemApi(api).getServerLogs();
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useLogEntries = () => {
|
||||||
|
const { api } = useApi();
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [ 'LogEntries' ],
|
||||||
|
queryFn: () => fetchLogEntries(api),
|
||||||
|
enabled: !!api
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,25 @@
|
||||||
|
import { Api } from '@jellyfin/sdk';
|
||||||
|
import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { useApi } from 'hooks/useApi';
|
||||||
|
|
||||||
|
export const fetchLogOptions = async (api?: Api) => {
|
||||||
|
if (!api) {
|
||||||
|
console.error('[useLogOptions] No API instance available');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await getConfigurationApi(api).getConfiguration();
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useLogOptions = () => {
|
||||||
|
const { api } = useApi();
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ['LogOptions'],
|
||||||
|
queryFn: () => fetchLogOptions(api),
|
||||||
|
enabled: !!api
|
||||||
|
});
|
||||||
|
};
|
|
@ -1,15 +1,15 @@
|
||||||
import React, { ChangeEvent, useCallback, useEffect, useState } from 'react';
|
import React, { ChangeEvent, useCallback, useEffect, useState } from 'react';
|
||||||
import type { LogFile } from '@jellyfin/sdk/lib/generated-client/models/log-file';
|
|
||||||
import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api';
|
import { getConfigurationApi } from '@jellyfin/sdk/lib/utils/api/configuration-api';
|
||||||
import { getSystemApi } from '@jellyfin/sdk/lib/utils/api/system-api';
|
|
||||||
import LogItem from 'components/dashboard/logs/LogItem';
|
import LogItem from 'components/dashboard/logs/LogItem';
|
||||||
import Loading from 'components/loading/LoadingComponent';
|
import Loading from 'components/loading/LoadingComponent';
|
||||||
import Page from 'components/Page';
|
import Page from 'components/Page';
|
||||||
import { useApi } from 'hooks/useApi';
|
|
||||||
import globalize from 'lib/globalize';
|
import globalize from 'lib/globalize';
|
||||||
import { Alert, Box, Button, FormControlLabel, Stack, Switch, TextField, Typography } from '@mui/material';
|
import { Alert, Box, Button, FormControlLabel, Stack, Switch, TextField, Typography } from '@mui/material';
|
||||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, Form, useActionData } from 'react-router-dom';
|
import { type ActionFunctionArgs, Form, useActionData } from 'react-router-dom';
|
||||||
import ServerConnections from 'components/ServerConnections';
|
import ServerConnections from 'components/ServerConnections';
|
||||||
|
import { useLogEntries } from 'apps/dashboard/features/logs/api/useLogEntries';
|
||||||
|
import { useLogOptions } from 'apps/dashboard/features/logs/api/useLogOptions';
|
||||||
|
import type { ServerConfiguration } from '@jellyfin/sdk/lib/generated-client/models/server-configuration';
|
||||||
|
|
||||||
interface ActionData {
|
interface ActionData {
|
||||||
isSaved: boolean;
|
isSaved: boolean;
|
||||||
|
@ -37,64 +37,41 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Logs = () => {
|
const Logs = () => {
|
||||||
const actionData = useActionData() as ActionData | undefined;
|
const actionData = useActionData() as ActionData | undefined;
|
||||||
const { api } = useApi();
|
|
||||||
const [ logs, setLogs ] = useState<LogFile[]>([]);
|
|
||||||
const [ logsLoading, setLogsLoading ] = useState<boolean>(true);
|
|
||||||
const [ configLoading, setConfigLoading ] = useState<boolean>(true);
|
|
||||||
const [ logWarningMessageChecked, setLogWarningMessageChecked ] = useState<boolean>(false);
|
|
||||||
const [ slowResponseTime, setSlowResponseTime ] = useState<string>('');
|
|
||||||
const [ isSubmitting, setIsSubmitting ] = useState(false);
|
const [ isSubmitting, setIsSubmitting ] = useState(false);
|
||||||
|
|
||||||
|
const { isPending: isLogEntriesPending, data: logs } = useLogEntries();
|
||||||
|
const { isPending: isLogOptionsPending, data: defaultLogOptions } = useLogOptions();
|
||||||
|
const [ loading, setLoading ] = useState(true);
|
||||||
|
const [ logOptions, setLogOptions ] = useState<ServerConfiguration>( {} );
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isLogOptionsPending && defaultLogOptions) {
|
||||||
|
setLogOptions(defaultLogOptions);
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [isLogOptionsPending, defaultLogOptions]);
|
||||||
|
|
||||||
const setLogWarningMessage = useCallback((_: ChangeEvent<HTMLInputElement>, checked: boolean) => {
|
const setLogWarningMessage = useCallback((_: ChangeEvent<HTMLInputElement>, checked: boolean) => {
|
||||||
setLogWarningMessageChecked(checked);
|
setLogOptions({
|
||||||
}, []);
|
...logOptions,
|
||||||
|
EnableSlowResponseWarning: checked
|
||||||
|
});
|
||||||
|
}, [logOptions]);
|
||||||
|
|
||||||
const onResponseTimeChange = useCallback((event: ChangeEvent<HTMLTextAreaElement>) => {
|
const onResponseTimeChange = useCallback((event: ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
setSlowResponseTime(event.target.value);
|
setLogOptions({
|
||||||
}, []);
|
...logOptions,
|
||||||
|
SlowResponseThresholdMs: parseInt(event.target.value, 10)
|
||||||
const loadLogs = useCallback(() => {
|
|
||||||
if (!api) return;
|
|
||||||
|
|
||||||
return getSystemApi(api)
|
|
||||||
.getServerLogs()
|
|
||||||
.then(({ data }) => {
|
|
||||||
setLogs(data);
|
|
||||||
});
|
});
|
||||||
}, [api]);
|
}, [logOptions]);
|
||||||
|
|
||||||
const onSubmit = useCallback(() => {
|
const onSubmit = useCallback(() => {
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
if (isLogEntriesPending || isLogOptionsPending || loading) {
|
||||||
if (!api) return;
|
|
||||||
|
|
||||||
loadLogs()?.then(() => {
|
|
||||||
setLogsLoading(false);
|
|
||||||
}).catch(err => {
|
|
||||||
console.error('[logs] An error occurred while fetching logs', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
getConfigurationApi(api)
|
|
||||||
.getConfiguration()
|
|
||||||
.then(({ data: config }) => {
|
|
||||||
if (config.EnableSlowResponseWarning) {
|
|
||||||
setLogWarningMessageChecked(config.EnableSlowResponseWarning);
|
|
||||||
}
|
|
||||||
if (config.SlowResponseThresholdMs != null) {
|
|
||||||
setSlowResponseTime(String(config.SlowResponseThresholdMs));
|
|
||||||
}
|
|
||||||
setConfigLoading(false);
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error('[logs] An error occurred while fetching system config', err);
|
|
||||||
});
|
|
||||||
}, [logsLoading, configLoading, api, loadLogs]);
|
|
||||||
|
|
||||||
if (logsLoading || configLoading) {
|
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +97,7 @@ export const Logs = () => {
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
control={
|
control={
|
||||||
<Switch
|
<Switch
|
||||||
checked={logWarningMessageChecked}
|
checked={logOptions?.EnableSlowResponseWarning}
|
||||||
onChange={setLogWarningMessage}
|
onChange={setLogWarningMessage}
|
||||||
name={'EnableWarningMessage'}
|
name={'EnableWarningMessage'}
|
||||||
/>
|
/>
|
||||||
|
@ -133,7 +110,7 @@ export const Logs = () => {
|
||||||
type='number'
|
type='number'
|
||||||
name={'SlowResponseTime'}
|
name={'SlowResponseTime'}
|
||||||
label={globalize.translate('LabelSlowResponseTime')}
|
label={globalize.translate('LabelSlowResponseTime')}
|
||||||
value={slowResponseTime}
|
value={logOptions?.SlowResponseThresholdMs}
|
||||||
onChange={onResponseTimeChange}
|
onChange={onResponseTimeChange}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
@ -147,7 +124,7 @@ export const Logs = () => {
|
||||||
</Form>
|
</Form>
|
||||||
<div className='serverLogs readOnlyContent'>
|
<div className='serverLogs readOnlyContent'>
|
||||||
<div className='paperList'>
|
<div className='paperList'>
|
||||||
{logs.map(log => {
|
{logs?.map(log => {
|
||||||
return <LogItem
|
return <LogItem
|
||||||
key={log.Name}
|
key={log.Name}
|
||||||
logFile={log}
|
logFile={log}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue