forked from afadil/wealthfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtauri.ts
91 lines (76 loc) · 2.51 KB
/
tauri.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { invoke } from '@tauri-apps/api/core';
import { open, save } from '@tauri-apps/plugin-dialog';
import { listen } from '@tauri-apps/api/event';
import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
import { error, info, warn, trace, debug } from '@tauri-apps/plugin-log';
import type { EventCallback, UnlistenFn } from '@tauri-apps/api/event';
export type { EventCallback, UnlistenFn };
export const invokeTauri = async <T>(command: string, payload?: Record<string, unknown>) => {
return await invoke<T>(command, payload);
};
export const openCsvFileDialogTauri = async (): Promise<null | string | string[]> => {
return open({ filters: [{ name: 'CSV', extensions: ['csv'] }] });
};
export const listenFileDropHoverTauri = async <T>(
handler: EventCallback<T>,
): Promise<UnlistenFn> => {
return listen<T>('tauri://file-drop-hover', handler);
};
export const listenFileDropTauri = async <T>(handler: EventCallback<T>): Promise<UnlistenFn> => {
return listen<T>('tauri://file-drop', handler);
};
export const listenFileDropCancelledTauri = async <T>(
handler: EventCallback<T>,
): Promise<UnlistenFn> => {
return listen<T>('tauri://file-drop-cancelled', handler);
};
export const listenQuotesSyncStartTauri = async <T>(
handler: EventCallback<T>,
): Promise<UnlistenFn> => {
return listen<T>('PORTFOLIO_UPDATE_START', handler);
};
export const listenQuotesSyncCompleteTauri = async <T>(
handler: EventCallback<T>,
): Promise<UnlistenFn> => {
return listen<T>('PORTFOLIO_UPDATE_COMPLETE', handler);
};
export const listenQuotesSyncErrorTauri = async <T>(
handler: EventCallback<T>,
): Promise<UnlistenFn> => {
return listen<T>('PORTFOLIO_UPDATE_ERROR', handler);
};
export const openFileSaveDialogTauri = async (
fileContent: string | Blob | Uint8Array,
fileName: string,
) => {
const filePath = await save({
defaultPath: fileName,
filters: [
{
name: fileName,
extensions: [fileName.split('.').pop() ?? ''],
},
],
});
if (filePath === null) {
return false;
}
let contentToSave: Uint8Array;
if (typeof fileContent === 'string') {
contentToSave = new TextEncoder().encode(fileContent);
} else if (fileContent instanceof Blob) {
const arrayBuffer = await fileContent.arrayBuffer();
contentToSave = new Uint8Array(arrayBuffer);
} else {
contentToSave = fileContent;
}
await writeFile(filePath, contentToSave, { baseDir: BaseDirectory.Document });
return true;
};
export const logger = {
error,
info,
warn,
trace,
debug,
};