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 1 commit
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 @@ -469,6 +469,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 = Serial log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really correct? Why not "Text file" or "Plain text"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean sure? idk lol

settings-serial-save_logs = Save Logs
ImUrX marked this conversation as resolved.
Show resolved Hide resolved

## OSC router settings
settings-osc-router = OSC router
Expand Down
60 changes: 58 additions & 2 deletions 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 @@ -22,7 +22,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 { useBreakpoint } from '@/hooks/breakpoint';
import { useBreakpoint, 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 @@ -167,6 +172,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 @@ -242,6 +291,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>
{isMobile && (
<Dropdown
control={control}
Expand Down