mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 05:42:44 -08:00
fix(tauri): save JSON exports through the native file dialog
This commit is contained in:
5 files changed
+42
-17
No files matched your search
@@ -17,7 +17,7 @@ import {
|
||||
MATRIX_UNSTABLE_COLORS,
|
||||
MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME,
|
||||
} from '$unstable/prefixes';
|
||||
import { downloadJsonFile } from '$app/utils/common';
|
||||
import { downloadJsonFile } from '$app/utils/download';
|
||||
import { selectFile } from '$app/utils/dom';
|
||||
import { ModalOverlay } from '$components/modal-overlay/ModalOverlay';
|
||||
import { AsyncError } from '$components/AsyncError';
|
||||
@@ -96,7 +96,7 @@ export function PerMessageProfileOverview({
|
||||
useCallback(async () => {
|
||||
const personas = await new ProfileCatalog(mx).list();
|
||||
const data = { personas };
|
||||
downloadJsonFile(JSON.stringify(data), 'persona');
|
||||
await downloadJsonFile(JSON.stringify(data), 'persona');
|
||||
}, [mx])
|
||||
);
|
||||
|
||||
|
||||
@@ -55,7 +55,6 @@ import { useSetting } from '$state/hooks/settings';
|
||||
import type { EditorButtonId } from '$state/settings';
|
||||
import { MessageLayout, RightSwipeAction, settingsAtom } from '$state/settings';
|
||||
import { SettingTile, SettingToggle } from '$components/setting-tile';
|
||||
import { downloadJsonFile } from '$utils/common';
|
||||
import { getDebugLogger } from '$utils/debugLogger';
|
||||
import { KeySymbol } from '$utils/key-symbol';
|
||||
import { isDesktopTauri, isMacOS, isMobileOrTablet, isMobileTauri } from '$utils/platform';
|
||||
@@ -66,7 +65,7 @@ import { settingsSyncLastSyncedAtom, settingsSyncStatusAtom } from '$hooks/useSe
|
||||
import { sanitizeDiagnosticsLogs } from '$utils/sentryScrubbers';
|
||||
import { diagnosticCaptureActiveAtom } from '$state/debugLogger';
|
||||
import { exportSettingsAsJson, importSettingsFromJson } from '$utils/settingsSync';
|
||||
import { saveFileToDevice } from '$utils/download';
|
||||
import { downloadJsonFile, saveFileToDevice } from '$utils/download';
|
||||
import { CallSoundSettings } from './CallSoundSettings';
|
||||
|
||||
type DateHintProps = {
|
||||
@@ -1353,7 +1352,7 @@ function DiagnosticsAndPrivacy() {
|
||||
setDiagnosticsState('error');
|
||||
return;
|
||||
}
|
||||
downloadJsonFile(sanitizedLogs, 'sable-web-diagnostics');
|
||||
await downloadJsonFile(sanitizedLogs, 'sable-web-diagnostics');
|
||||
}
|
||||
setDiagnosticsState('success');
|
||||
setCaptureCompleted(false);
|
||||
|
||||
@@ -135,17 +135,6 @@ export const suffixRename = (name: string, validator: (newName: string) => boole
|
||||
|
||||
export const replaceSpaceWithDash = (str: string): string => str.replace(/ /g, '-');
|
||||
|
||||
/** Trigger a browser download of a JSON file. */
|
||||
export const downloadJsonFile = (content: string, fileNamePrefix: string): void => {
|
||||
const blob = new Blob([content], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${fileNamePrefix}-${Date.now()}.json`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const splitWithSpace = (content: string): string[] => {
|
||||
const trimmedContent = content.trim();
|
||||
if (trimmedContent === '') return [];
|
||||
|
||||
@@ -3,7 +3,7 @@ import FileSaver from 'file-saver';
|
||||
import { invoke, isTauri } from '@tauri-apps/api/core';
|
||||
import { type as osType } from '@tauri-apps/plugin-os';
|
||||
import { showToast } from '$state/toast';
|
||||
import { saveFileToDevice, saveMediaToGallery } from './download';
|
||||
import { downloadJsonFile, saveFileToDevice, saveMediaToGallery } from './download';
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
androidFs: {
|
||||
@@ -123,6 +123,33 @@ describe('saveFileToDevice', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('downloadJsonFile', () => {
|
||||
it('saves through the native desktop command instead of an anchor click', async () => {
|
||||
vi.mocked(osType).mockReturnValue('linux');
|
||||
|
||||
const result = await downloadJsonFile('{"a":1}', 'persona');
|
||||
|
||||
expect(result).toBe('saved');
|
||||
expect(invoke).toHaveBeenCalledWith('save_download', {
|
||||
filename: expect.stringMatching(/^persona-\d+\.json$/),
|
||||
bytes: expect.any(Array),
|
||||
});
|
||||
expect(FileSaver.saveAs).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('routes Android exports to the public Downloads directory', async () => {
|
||||
const result = await downloadJsonFile('{"a":1}', 'persona');
|
||||
|
||||
expect(result).toBe('saved');
|
||||
expect(androidFs.createNewPublicFile).toHaveBeenCalledWith(
|
||||
'Download',
|
||||
expect.stringMatching(/^persona-\d+\.json$/),
|
||||
'application/json',
|
||||
{ isPending: true, requestPermission: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveMediaToGallery', () => {
|
||||
it('saves Android images to Pictures through the public image API', async () => {
|
||||
await saveMediaToGallery(new Blob(['data']), 'photo.png', 'image/png');
|
||||
|
||||
@@ -185,3 +185,13 @@ export async function saveFileToDevice(
|
||||
FileSaver.saveAs(input, filename);
|
||||
return 'saved';
|
||||
}
|
||||
|
||||
export const downloadJsonFile = (
|
||||
content: string,
|
||||
fileNamePrefix: string
|
||||
): Promise<'saved' | 'cancelled' | 'failed'> =>
|
||||
saveFileToDevice(
|
||||
new Blob([content], { type: 'application/json' }),
|
||||
`${fileNamePrefix}-${Date.now()}.json`,
|
||||
'application/json'
|
||||
);
|
||||
Reference in new issue
Block a user