From 6335e380bb93109fd2cd829f59e292f39daab326 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 1 Dec 2024 23:09:43 +0800 Subject: [PATCH] fix: if there is `null` in a field, it will cause TW silent fail when booting --- src/components/WikiUpdateList.tsx | 16 ++++++++++------ src/services/NativeService/hooks.tsx | 9 ++++----- src/services/WikiStorageService/index.ts | 12 ++++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/components/WikiUpdateList.tsx b/src/components/WikiUpdateList.tsx index c26d549..6bb0d96 100644 --- a/src/components/WikiUpdateList.tsx +++ b/src/components/WikiUpdateList.tsx @@ -93,12 +93,16 @@ export const WikiUpdateList: React.FC = ({ onLongPress, wiki, las {selectedChange ? ( - {Object.entries(selectedChange.fields ?? {}).map(([key, value]) => ( - - {key}: - {String(value)} - - ))} + {Object.entries(selectedChange.fields ?? {}).map(([key, value]) => { + const displayValue = String(value); + const truncatedValue = displayValue.length > 1024 ? `${displayValue.slice(0, 1024)}......` : displayValue; + return ( + + {key}: + {truncatedValue} + + ); + })} ) : No details available} diff --git a/src/services/NativeService/hooks.tsx b/src/services/NativeService/hooks.tsx index 0c64373..3e748c2 100644 --- a/src/services/NativeService/hooks.tsx +++ b/src/services/NativeService/hooks.tsx @@ -89,11 +89,12 @@ export function useRegisterReceivingShareIntent() { creator: i18n.t('Share.TidGiMobileShare'), tags: newTagForSharedContent, }; + if (shareIntent.webUrl) fields = { ...fields, url: shareIntent.webUrl }; switch (shareIntent.type) { case 'text': case 'weburl': { if (shareIntent.text) fields = { ...fields, text: shareIntent.text }; - if (shareIntent.webUrl) fields = { ...fields, url: shareIntent.webUrl }; + await storageOfDefaultWorkspace.saveTiddler(shareIntent.meta?.title ?? randomTitle, fields); break; } case 'media': @@ -101,22 +102,20 @@ export function useRegisterReceivingShareIntent() { if (shareIntent.files) { for (const file of shareIntent.files) { const fileContent = await fs.readAsStringAsync(file.path, { encoding: fs.EncodingType.Base64 }); - fields = { + const fileFields = { ...fields, type: file.mimeType, - size: file.size, width: file.width, height: file.height, duration: file.duration, text: fileContent, }; - await storageOfDefaultWorkspace.saveTiddler(file.fileName, fields); + await storageOfDefaultWorkspace.saveTiddler(file.fileName || randomTitle, fileFields); } } break; } } - await storageOfDefaultWorkspace.saveTiddler(shareIntent.meta?.title ?? randomTitle, fields); setImportSuccessSnackBarVisible(true); } catch (error) { console.log( diff --git a/src/services/WikiStorageService/index.ts b/src/services/WikiStorageService/index.ts index 88ed9c2..85acc33 100644 --- a/src/services/WikiStorageService/index.ts +++ b/src/services/WikiStorageService/index.ts @@ -6,7 +6,6 @@ import { Observable } from 'rxjs'; import type { IChangedTiddlers, ITiddlerFieldsParam } from 'tiddlywiki'; import { getWikiTiddlerPathByTitle } from '../../constants/paths'; import { useConfigStore } from '../../store/config'; -import { useServerStore } from '../../store/server'; import { IWikiWorkspace } from '../../store/workspace'; import { backgroundSyncService } from '../BackgroundSyncService'; import { sqliteServiceService } from '../SQLiteService'; @@ -44,13 +43,22 @@ export class WikiStorageService { } /** - * Return the e-tag + * Save tiddler. Return the e-tag. + * `tags` field should be string, and fields can't contain `null`, otherwise TW won't boot. */ async saveTiddler(title: string, fields: ITiddlerFieldsParam): Promise { try { /** Tiddlers that should save to SQLite as full tiddlers. Like plugins that starts with `$:/` */ const saveFullTiddler = getFullSaveTiddlers(title).includes(title); const { text, title: _, ...fieldsObjectToSave } = fields as (ITiddlerFieldsParam & { text?: string; title: string }); + // if there is `null` in a field, it will cause TW silent fail when booting + (Object.keys(fieldsObjectToSave) as Array).forEach(key => { + if (fieldsObjectToSave[key] === null || fieldsObjectToSave[key] === undefined) { + // @ts-expect-error Index signature in type '{ readonly [x: string]: unknown; readonly [x: number]: unknown; created?: string | undefined; modified?: string | undefined; }' only permits reading.ts(2542) + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete fieldsObjectToSave[key]; + } + }); const changeCount = '0'; // this.wikiInstance.wiki.getChangeCount(title).toString(); const Etag = `"default/${encodeURIComponent(title)}/${changeCount}:"`;