Skip to content

Commit

Permalink
fix: if there is null in a field, it will cause TW silent fail when…
Browse files Browse the repository at this point in the history
… booting
  • Loading branch information
linonetwo committed Dec 1, 2024
1 parent 1563683 commit 6335e38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/components/WikiUpdateList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ export const WikiUpdateList: React.FC<WikiListProps> = ({ onLongPress, wiki, las
{selectedChange
? (
<ScrollableContent>
{Object.entries(selectedChange.fields ?? {}).map(([key, value]) => (
<FieldRow key={key}>
<FieldKey>{key}:</FieldKey>
<Text>{String(value)}</Text>
</FieldRow>
))}
{Object.entries(selectedChange.fields ?? {}).map(([key, value]) => {
const displayValue = String(value);
const truncatedValue = displayValue.length > 1024 ? `${displayValue.slice(0, 1024)}......` : displayValue;
return (
<FieldRow key={key}>
<FieldKey>{key}:</FieldKey>
<Text>{truncatedValue}</Text>
</FieldRow>
);
})}
</ScrollableContent>
)
: <Text>No details available</Text>}
Expand Down
9 changes: 4 additions & 5 deletions src/services/NativeService/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,33 @@ 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':
case 'file': {
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(
Expand Down
12 changes: 10 additions & 2 deletions src/services/WikiStorageService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string> {
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<keyof typeof fieldsObjectToSave>).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}:"`;

Expand Down

0 comments on commit 6335e38

Please sign in to comment.