fix: optimize types for infra/electron (#3574)

This commit is contained in:
Peng Xiao 2023-08-04 17:23:56 +08:00 committed by GitHub
parent 5795020403
commit 6415f0093b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 54 deletions

View File

@ -139,7 +139,6 @@ const SetDBLocationContent = ({
if (result?.filePath) { if (result?.filePath) {
onConfirmLocation(result.filePath); onConfirmLocation(result.filePath);
} else if (result?.error) { } else if (result?.error) {
// @ts-expect-error: result.error is dynamic so the type is unknown
toast(t[result.error]()); toast(t[result.error]());
} }
})().catch(err => { })().catch(err => {
@ -273,7 +272,6 @@ export const CreateWorkspaceModal = ({
setStep('set-syncing-mode'); setStep('set-syncing-mode');
} else if (result.error || result.canceled) { } else if (result.error || result.canceled) {
if (result.error) { if (result.error) {
// @ts-expect-error: result.error is dynamic so the type is unknown
toast(t[result.error]()); toast(t[result.error]());
} }
onClose(); onClose();

View File

@ -36,7 +36,6 @@ export const ExportPanel: FC<{
await syncBlobsToSqliteDb(workspace); await syncBlobsToSqliteDb(workspace);
const result = await window.apis?.dialog.saveDBFileAs(workspaceId); const result = await window.apis?.dialog.saveDBFileAs(workspaceId);
if (result?.error) { if (result?.error) {
// @ts-expect-error: result.error is dynamic
toast(t[result.error]()); toast(t[result.error]());
} else if (!result?.canceled) { } else if (!result?.canceled) {
toast(t['Export success']()); toast(t['Export success']());

View File

@ -55,7 +55,6 @@ export const StoragePanel: FC<{
if (!result?.error && !result?.canceled) { if (!result?.error && !result?.canceled) {
toast(t['Move folder success']()); toast(t['Move folder success']());
} else if (result?.error) { } else if (result?.error) {
// @ts-expect-error: result.error is dynamic
toast(t[result.error]()); toast(t[result.error]());
} }
}) })

View File

@ -1,6 +1,13 @@
import path from 'node:path'; import path from 'node:path';
import { ValidationResult } from '@affine/native'; import { ValidationResult } from '@affine/native';
import type {
FakeDialogResult,
LoadDBFileResult,
MoveDBFileResult,
SaveDBFileResult,
SelectDBFileLocationResult,
} from '@toeverything/infra/type';
import fs from 'fs-extra'; import fs from 'fs-extra';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
@ -28,13 +35,6 @@ export async function revealDBFile(workspaceId: string) {
await mainRPC.showItemInFolder(meta.secondaryDBPath ?? meta.mainDBPath); await mainRPC.showItemInFolder(meta.secondaryDBPath ?? meta.mainDBPath);
} }
// provide a backdoor to set dialog path for testing in playwright
export interface FakeDialogResult {
canceled?: boolean;
filePath?: string;
filePaths?: string[];
}
// result will be used in the next call to showOpenDialog // result will be used in the next call to showOpenDialog
// if it is being read once, it will be reset to undefined // if it is being read once, it will be reset to undefined
let fakeDialogResult: FakeDialogResult | undefined = undefined; let fakeDialogResult: FakeDialogResult | undefined = undefined;
@ -53,23 +53,6 @@ export function setFakeDialogResult(result: FakeDialogResult | undefined) {
} }
} }
const ErrorMessages = [
'DB_FILE_ALREADY_LOADED',
'DB_FILE_PATH_INVALID',
'DB_FILE_INVALID',
'DB_FILE_MIGRATION_FAILED',
'FILE_ALREADY_EXISTS',
'UNKNOWN_ERROR',
] as const;
type ErrorMessage = (typeof ErrorMessages)[number];
export interface SaveDBFileResult {
filePath?: string;
canceled?: boolean;
error?: ErrorMessage;
}
const extension = 'affine'; const extension = 'affine';
function getDefaultDBFileName(name: string, id: string) { function getDefaultDBFileName(name: string, id: string) {
@ -125,12 +108,6 @@ export async function saveDBFileAs(
} }
} }
export interface SelectDBFileLocationResult {
filePath?: string;
error?: ErrorMessage;
canceled?: boolean;
}
export async function selectDBFileLocation(): Promise<SelectDBFileLocationResult> { export async function selectDBFileLocation(): Promise<SelectDBFileLocationResult> {
try { try {
const ret = const ret =
@ -157,12 +134,6 @@ export async function selectDBFileLocation(): Promise<SelectDBFileLocationResult
} }
} }
export interface LoadDBFileResult {
workspaceId?: string;
error?: ErrorMessage;
canceled?: boolean;
}
/** /**
* This function is called when the user clicks the "Load" button in the "Load Workspace" dialog. * This function is called when the user clicks the "Load" button in the "Load Workspace" dialog.
* *
@ -255,12 +226,6 @@ export async function loadDBFile(): Promise<LoadDBFileResult> {
} }
} }
export interface MoveDBFileResult {
filePath?: string;
error?: ErrorMessage;
canceled?: boolean;
}
/** /**
* This function is called when the user clicks the "Move" button in the "Move Workspace Storage" setting. * This function is called when the user clicks the "Move" button in the "Move Workspace Storage" setting.
* *

View File

@ -105,9 +105,9 @@ export type DBHandlers = {
key: string, key: string,
data: Uint8Array data: Uint8Array
) => Promise<void>; ) => Promise<void>;
getBlob: (workspaceId: string, key: string) => Promise<any>; getBlob: (workspaceId: string, key: string) => Promise<Buffer | null>;
deleteBlob: (workspaceId: string, key: string) => Promise<void>; deleteBlob: (workspaceId: string, key: string) => Promise<void>;
getBlobKeys: (workspaceId: string) => Promise<any>; getBlobKeys: (workspaceId: string) => Promise<string[]>;
getDefaultStorageLocation: () => Promise<string>; getDefaultStorageLocation: () => Promise<string>;
}; };
@ -116,13 +116,55 @@ export type DebugHandlers = {
logFilePath: () => Promise<string>; logFilePath: () => Promise<string>;
}; };
export type ErrorMessage =
| 'DB_FILE_ALREADY_LOADED'
| 'DB_FILE_PATH_INVALID'
| 'DB_FILE_INVALID'
| 'DB_FILE_MIGRATION_FAILED'
| 'FILE_ALREADY_EXISTS'
| 'UNKNOWN_ERROR';
export interface LoadDBFileResult {
workspaceId?: string;
error?: ErrorMessage;
canceled?: boolean;
}
export interface SaveDBFileResult {
filePath?: string;
canceled?: boolean;
error?: ErrorMessage;
}
export interface SelectDBFileLocationResult {
filePath?: string;
error?: ErrorMessage;
canceled?: boolean;
}
export interface MoveDBFileResult {
filePath?: string;
error?: ErrorMessage;
canceled?: boolean;
}
// provide a backdoor to set dialog path for testing in playwright
export interface FakeDialogResult {
canceled?: boolean;
filePath?: string;
filePaths?: string[];
}
export type DialogHandlers = { export type DialogHandlers = {
revealDBFile: (workspaceId: string) => Promise<any>; revealDBFile: (workspaceId: string) => Promise<void>;
loadDBFile: () => Promise<any>; loadDBFile: () => Promise<LoadDBFileResult>;
saveDBFileAs: (workspaceId: string) => Promise<any>; saveDBFileAs: (workspaceId: string) => Promise<SaveDBFileResult>;
moveDBFile: (workspaceId: string, dbFileLocation?: string) => Promise<any>; moveDBFile: (
selectDBFileLocation: () => Promise<any>; workspaceId: string,
setFakeDialogResult: (result: any) => Promise<any>; dbFileLocation?: string
) => Promise<MoveDBFileResult>;
selectDBFileLocation: () => Promise<SelectDBFileLocationResult>;
setFakeDialogResult: (result: any) => Promise<FakeDialogResult>;
}; };
export type UIHandlers = { export type UIHandlers = {