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

Add a way to save serial logs as a file #1029

Merged
merged 8 commits into from
Jul 25, 2024
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
2 changes: 2 additions & 0 deletions gui/public/i18n/en/translation.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ settings-serial-get_infos = Get Infos
settings-serial-serial_select = Select a serial port
settings-serial-auto_dropdown_item = Auto
settings-serial-get_wifi_scan = Get WiFi Scan
settings-serial-file_type = Plain text
settings-serial-save_logs = Save To File

## OSC router settings
settings-osc-router = OSC router
Expand Down
59 changes: 58 additions & 1 deletion gui/src/components/settings/pages/Serial.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useLocation } from 'react-router-dom';
import {
Expand All @@ -21,6 +21,12 @@ import { Typography } from '@/components/commons/Typography';
import { Localized, useLocalization } from '@fluent/react';
import { BaseModal } from '@/components/commons/BaseModal';
import { WarningBox } from '@/components/commons/TipBox';
import { useIsTauri } from '@/hooks/breakpoint';
import { fileSave } from 'browser-fs-access';
import { save } from '@tauri-apps/plugin-dialog';
import { writeTextFile } from '@tauri-apps/plugin-fs';
import { error } from '@/utils/logging';
import { waitUntil } from '@/utils/a11y';

export interface SerialForm {
port: string;
Expand Down Expand Up @@ -156,6 +162,50 @@ export function Serial() {
);
};

const isTauri = useIsTauri();
const consoleContentRef = useRef(consoleContent);
useLayoutEffect(() => {
consoleContentRef.current = consoleContent;
}, [consoleContent]);

const saveLogToFile = async () => {
// Check if we have getInfos and fetch them if we don't
if (!consoleContentRef.current.includes('GET INFO')) {
getInfos();
await waitUntil(
() => consoleContentRef.current.includes('GET INFO'),
100,
10
);
}

if (isTauri) {
save({
filters: [
{
name: l10n.getString('settings-serial-file_type'),
extensions: ['txt'],
},
],
defaultPath: 'serial-logs.txt',
})
.then((path) =>
path ? writeTextFile(path, consoleContentRef.current) : undefined
)
.catch((err) => {
error(err);
});
} else {
const blob = new Blob([consoleContentRef.current], {
type: 'text/plain',
});
fileSave(blob, {
fileName: 'serial-logs.txt',
extensions: ['.txt'],
});
}
};

return (
<>
<BaseModal
Expand Down Expand Up @@ -226,6 +276,13 @@ export function Serial() {
<Button variant="quaternary" onClick={getWifiScan}>
{l10n.getString('settings-serial-get_wifi_scan')}
</Button>
<Button
variant="quaternary"
onClick={saveLogToFile}
disabled={!isSerialOpen || !consoleContent.trim()}
>
{l10n.getString('settings-serial-save_logs')}
</Button>
<div className="w-full mobile:col-span-2">
<Dropdown
control={control}
Expand Down
Loading