Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate logs to React #6393

Merged
merged 18 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/apps/dashboard/features/activity/api/useLogEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const fetchLogEntries = async (
return response.data;
};

export const useLogEntires = (
export const useLogEntries = (
requestParams: ActivityLogApiGetLogEntriesRequest
) => {
const { api } = useApi();
return useQuery({
queryKey: ['LogEntries', requestParams],
queryKey: ['ActivityLogEntries', requestParams],
queryFn: ({ signal }) =>
fetchLogEntries(api, requestParams, { signal }),
enabled: !!api
Expand Down
26 changes: 26 additions & 0 deletions src/apps/dashboard/features/logs/api/useServerLogs.ts
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 src/apps/dashboard/features/logs/components/LogItemList.tsx
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) => {
viown marked this conversation as resolved.
Show resolved Hide resolved
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' }}
/>
viown marked this conversation as resolved.
Show resolved Hide resolved
<OpenInNewIcon />
</ListItemButton>
</ListItem>
);
})}
</List>
);
};

export default LogItemList;
1 change: 1 addition & 0 deletions src/apps/dashboard/routes/_asyncRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AsyncRouteType, type AsyncRoute } from 'components/router/AsyncRoute';
export const ASYNC_ADMIN_ROUTES: AsyncRoute[] = [
{ path: 'activity', type: AsyncRouteType.Dashboard },
{ path: 'branding', type: AsyncRouteType.Dashboard },
{ path: 'logs', type: AsyncRouteType.Dashboard },
{ path: 'playback/trickplay', type: AsyncRouteType.Dashboard },
{ path: 'plugins/:pluginId', page: 'plugins/plugin', type: AsyncRouteType.Dashboard },
{ path: 'users', type: AsyncRouteType.Dashboard },
Expand Down
6 changes: 0 additions & 6 deletions src/apps/dashboard/routes/_legacyRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ export const LEGACY_ADMIN_ROUTES: LegacyRoute[] = [
controller: 'dashboard/encodingsettings',
view: 'dashboard/encodingsettings.html'
}
}, {
path: 'logs',
pageProps: {
controller: 'dashboard/logs',
view: 'dashboard/logs.html'
}
}, {
path: 'libraries/metadata',
pageProps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Typography from '@mui/material/Typography';
import { type MRT_ColumnDef, MaterialReactTable, useMaterialReactTable } from 'material-react-table';
import { useSearchParams } from 'react-router-dom';

import { useLogEntires } from 'apps/dashboard/features/activity/api/useLogEntries';
import { useLogEntries } from 'apps/dashboard/features/activity/api/useLogEntries';
import ActionsCell from 'apps/dashboard/features/activity/components/ActionsCell';
import LogLevelCell from 'apps/dashboard/features/activity/components/LogLevelCell';
import OverviewCell from 'apps/dashboard/features/activity/components/OverviewCell';
Expand Down Expand Up @@ -87,7 +87,7 @@ const Activity = () => {
hasUserId: activityView !== ActivityView.All ? activityView === ActivityView.User : undefined
}), [activityView, pagination.pageIndex, pagination.pageSize]);

const { data: logEntries, isLoading: isLogEntriesLoading } = useLogEntires(activityParams);
const { data: logEntries, isLoading: isLogEntriesLoading } = useLogEntries(activityParams);

const isLoading = isUsersLoading || isLogEntriesLoading;

Expand Down
5 changes: 1 addition & 4 deletions src/apps/dashboard/routes/branding/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import Page from 'components/Page';
import ServerConnections from 'components/ServerConnections';
import globalize from 'lib/globalize';
import { queryClient } from 'utils/query/queryClient';

interface ActionData {
isSaved: boolean
}
import { ActionData } from 'types/actionData';

const BRANDING_CONFIG_KEY = 'branding';
const BrandingOption = {
Expand Down
139 changes: 139 additions & 0 deletions src/apps/dashboard/routes/logs/index.tsx
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
viown marked this conversation as resolved.
Show resolved Hide resolved
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;
33 changes: 0 additions & 33 deletions src/controllers/dashboard/logs.html

This file was deleted.

63 changes: 0 additions & 63 deletions src/controllers/dashboard/logs.js

This file was deleted.

Loading
Loading