Skip to content

Commit

Permalink
Connect Hdhr
Browse files Browse the repository at this point in the history
  • Loading branch information
markdavella committed Jan 23, 2024
1 parent b2b7660 commit 5d9e498
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
2 changes: 2 additions & 0 deletions types/src/HdhrSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import z from 'zod';
import { HdhrSettingsSchema } from './schemas/settingsSchemas.js';

export type HdhrSettings = z.infer<typeof HdhrSettingsSchema>;

export const defaultHdhrSettings = HdhrSettingsSchema.parse({});
5 changes: 3 additions & 2 deletions web2/src/pages/settings/FfmpegSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ export default function FfmpegSettingsPage() {
const [showFormError, setShowFormError] = React.useState<boolean>(false);

useEffect(() => {
console.log(data);
setFfmpegExecutablePath(
data?.ffmpegExecutablePath || defaultFfmpegSettings.ffmpegExecutablePath,
);

setNumThreads(
data?.numThreads.toString() ||
data?.numThreads?.toString() ||
defaultFfmpegSettings.numThreads.toString(),
);

Expand All @@ -159,7 +160,7 @@ export default function FfmpegSettingsPage() {
);

setVideoBufferSize(
data?.videoBufferSize.toString() ||
data?.videoBufferSize?.toString() ||
defaultFfmpegSettings.videoBufferSize.toString(),
);

Expand Down
96 changes: 86 additions & 10 deletions web2/src/pages/settings/HdhrSettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useEffect } from 'react';
import {
Button,
Checkbox,
Expand All @@ -6,42 +7,117 @@ import {
FormHelperText,
Stack,
TextField,
Snackbar,
} from '@mui/material';
import { useHdhrSettings } from '../../hooks/settingsHooks.ts';
import { defaultHdhrSettings, HdhrSettings } from 'dizquetv-types';
import { useMutation, useQueryClient } from '@tanstack/react-query';

export default function HdhrSettingsPage() {
const { data, isPending, error } = useHdhrSettings();
const [snackStatus, setSnackStatus] = React.useState<boolean>(false);
const [enableSsdpServer, setEnableSsdpServer] = React.useState<boolean>(
defaultHdhrSettings.enableSsdpServer,
);
const [tunerCount, setTunerCount] = React.useState<string>(
defaultHdhrSettings.tunerCount.toString(),
);

const queryClient = useQueryClient();

const updateHdhrSettingsMutation = useMutation({
mutationFn: (updateSettings: HdhrSettings) => {
return fetch('http://localhost:8000/api/hdhr-settings', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updateSettings),
});
},
onSuccess: () => {
setSnackStatus(true);
return queryClient.invalidateQueries({
queryKey: ['settings', 'hdhr-settings'],
});
},
});

const updateHdhrSettings = () => {
updateHdhrSettingsMutation.mutate({
enableSsdpServer,
tunerCount: Number(tunerCount),
});
};

const handleResetOptions = () => {
updateHdhrSettingsMutation.mutate({
...defaultHdhrSettings,
});
setEnableSsdpServer(defaultHdhrSettings.enableSsdpServer);
setTunerCount(defaultHdhrSettings.tunerCount.toString());
};

const handleEnableSsdpServer = () => {
setEnableSsdpServer(!enableSsdpServer);
};

const handleTunerCount = (value: string) => {
setTunerCount(value);
};

const handleSnackClose = () => {
setSnackStatus(false);
};

useEffect(() => {
setEnableSsdpServer(
data?.enableSsdpServer || defaultHdhrSettings.enableSsdpServer,
);
setTunerCount(
data?.tunerCount.toString() || defaultHdhrSettings.tunerCount.toString(),
);
}, [data]);

if (isPending) {
return <h1>XML: Loading...</h1>;
} else if (error) {
return <h1>XML: {error.message}</h1>;
}

const defaultHdhrSettings = {
enableSsdrServer: false,
tuners: 2,
};

return (
<>
<Snackbar
open={snackStatus}
autoHideDuration={6000}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
onClose={handleSnackClose}
message="Settings Saved!"
/>
<FormControl>
<Checkbox />
<Checkbox
value={enableSsdpServer}
onChange={() => handleEnableSsdpServer()}
/>
<FormLabel>Enable SSDP server</FormLabel>
<FormHelperText>* Restart required</FormHelperText>
</FormControl>
<TextField
fullWidth
id="output-path"
label="Tuner Count"
defaultValue={'2'}
InputProps={{ readOnly: true }}
value={tunerCount}
onChange={(event) => handleTunerCount(event.target.value)}
variant="filled"
sx={{ mt: 2, mb: 2 }}
/>
<Stack spacing={2} direction="row" justifyContent="right" sx={{ mt: 2 }}>
<Button variant="outlined">Reset Options</Button>
<Button variant="contained">Save</Button>
<Button variant="outlined" onClick={() => handleResetOptions()}>
Reset Options
</Button>
<Button variant="contained" onClick={() => updateHdhrSettings()}>
Save
</Button>
</Stack>
</>
);
Expand Down

0 comments on commit 5d9e498

Please sign in to comment.