From b7a18bea344fc92a6dc8f47bac20cafe15afbed4 Mon Sep 17 00:00:00 2001 From: reya Date: Sun, 19 Nov 2023 14:50:59 +0700 Subject: [PATCH] respect user settings --- src/app/settings/general.tsx | 2 ++ src/libs/storage/instance.ts | 2 ++ src/libs/storage/provider.tsx | 15 ++++++++++++--- src/utils/hooks/useRichContent.tsx | 17 ++++++++++++----- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/app/settings/general.tsx b/src/app/settings/general.tsx index 8494906c..e4cd86e0 100644 --- a/src/app/settings/general.tsx +++ b/src/app/settings/general.tsx @@ -47,12 +47,14 @@ export function GeneralSettingScreen() { const toggleMedia = async () => { await db.createSetting('media', String(+!settings.media)); + db.settings.media = !settings.media; // update state setSettings((prev) => ({ ...prev, media: !settings.media })); }; const toggleHashtag = async () => { await db.createSetting('hashtag', String(+!settings.hashtag)); + db.settings.hashtag = !settings.hashtag; // update state setSettings((prev) => ({ ...prev, hashtag: !settings.hashtag })); }; diff --git a/src/libs/storage/instance.ts b/src/libs/storage/instance.ts index 37bc18c9..929b0e3d 100644 --- a/src/libs/storage/instance.ts +++ b/src/libs/storage/instance.ts @@ -20,11 +20,13 @@ export class LumeStorage { public db: Database; public account: Account | null; public platform: Platform | null; + public settings: { outbox: boolean; media: boolean; hashtag: boolean }; constructor(sqlite: Database, platform: Platform) { this.db = sqlite; this.account = null; this.platform = platform; + this.settings = { outbox: false, media: true, hashtag: true }; } public async secureSave(key: string, value: string) { diff --git a/src/libs/storage/provider.tsx b/src/libs/storage/provider.tsx index 9e398e95..3fdbdc13 100644 --- a/src/libs/storage/provider.tsx +++ b/src/libs/storage/provider.tsx @@ -1,4 +1,3 @@ -import { appConfigDir } from '@tauri-apps/api/path'; import { message } from '@tauri-apps/plugin-dialog'; import { platform } from '@tauri-apps/plugin-os'; import { relaunch } from '@tauri-apps/plugin-process'; @@ -29,11 +28,22 @@ const StorageProvider = ({ children }: PropsWithChildren) => { try { const sqlite = await Database.load('sqlite:lume_v2.db'); const platformName = await platform(); - const dir = await appConfigDir(); const lumeStorage = new LumeStorage(sqlite, platformName); if (!lumeStorage.account) await lumeStorage.getActiveAccount(); + const settings = await lumeStorage.getAllSettings(); + if (settings) { + settings.forEach((item) => { + if (item.key === 'outbox') lumeStorage.settings.outbox = !!parseInt(item.value); + + if (item.key === 'media') lumeStorage.settings.media = !!parseInt(item.value); + + if (item.key === 'hashtag') + lumeStorage.settings.hashtag = !!parseInt(item.value); + }); + } + // check update const update = await check(); if (update) { @@ -44,7 +54,6 @@ const StorageProvider = ({ children }: PropsWithChildren) => { } setDB(lumeStorage); - console.info(dir); } catch (e) { await message(`Cannot initialize database: ${e}`, { title: 'Lume', diff --git a/src/utils/hooks/useRichContent.tsx b/src/utils/hooks/useRichContent.tsx index 1e188ad7..4edeefd1 100644 --- a/src/utils/hooks/useRichContent.tsx +++ b/src/utils/hooks/useRichContent.tsx @@ -4,6 +4,8 @@ import { ReactNode } from 'react'; import { Link } from 'react-router-dom'; import reactStringReplace from 'react-string-replace'; +import { useStorage } from '@libs/storage/provider'; + import { Hashtag, ImagePreview, @@ -44,6 +46,8 @@ const VIDEOS = [ ]; export function useRichContent(content: string, textmode: boolean = false) { + const { db } = useStorage(); + let parsedContent: string | ReactNode[] = content.replace(/\n+/g, '\n'); let linkPreview: string; let images: string[] = []; @@ -54,8 +58,10 @@ export function useRichContent(content: string, textmode: boolean = false) { const words = text.split(/( |\n)/); if (!textmode) { - images = words.filter((word) => IMAGES.some((el) => word.endsWith(el))); - videos = words.filter((word) => VIDEOS.some((el) => word.endsWith(el))); + if (db.settings.media) { + images = words.filter((word) => IMAGES.some((el) => word.endsWith(el))); + videos = words.filter((word) => VIDEOS.some((el) => word.endsWith(el))); + } events = words.filter((word) => NOSTR_EVENTS.some((el) => word.startsWith(el))); } @@ -83,9 +89,10 @@ export function useRichContent(content: string, textmode: boolean = false) { if (hashtags.length) { hashtags.forEach((hashtag) => { - parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => ( - - )); + parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => { + if (db.settings.hashtag) return ; + return null; + }); }); }