respect user settings

This commit is contained in:
reya 2023-11-19 14:50:59 +07:00
parent 7117ed05a9
commit b7a18bea34
4 changed files with 28 additions and 8 deletions

View File

@ -47,12 +47,14 @@ export function GeneralSettingScreen() {
const toggleMedia = async () => { const toggleMedia = async () => {
await db.createSetting('media', String(+!settings.media)); await db.createSetting('media', String(+!settings.media));
db.settings.media = !settings.media;
// update state // update state
setSettings((prev) => ({ ...prev, media: !settings.media })); setSettings((prev) => ({ ...prev, media: !settings.media }));
}; };
const toggleHashtag = async () => { const toggleHashtag = async () => {
await db.createSetting('hashtag', String(+!settings.hashtag)); await db.createSetting('hashtag', String(+!settings.hashtag));
db.settings.hashtag = !settings.hashtag;
// update state // update state
setSettings((prev) => ({ ...prev, hashtag: !settings.hashtag })); setSettings((prev) => ({ ...prev, hashtag: !settings.hashtag }));
}; };

View File

@ -20,11 +20,13 @@ export class LumeStorage {
public db: Database; public db: Database;
public account: Account | null; public account: Account | null;
public platform: Platform | null; public platform: Platform | null;
public settings: { outbox: boolean; media: boolean; hashtag: boolean };
constructor(sqlite: Database, platform: Platform) { constructor(sqlite: Database, platform: Platform) {
this.db = sqlite; this.db = sqlite;
this.account = null; this.account = null;
this.platform = platform; this.platform = platform;
this.settings = { outbox: false, media: true, hashtag: true };
} }
public async secureSave(key: string, value: string) { public async secureSave(key: string, value: string) {

View File

@ -1,4 +1,3 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { message } from '@tauri-apps/plugin-dialog'; import { message } from '@tauri-apps/plugin-dialog';
import { platform } from '@tauri-apps/plugin-os'; import { platform } from '@tauri-apps/plugin-os';
import { relaunch } from '@tauri-apps/plugin-process'; import { relaunch } from '@tauri-apps/plugin-process';
@ -29,11 +28,22 @@ const StorageProvider = ({ children }: PropsWithChildren<object>) => {
try { try {
const sqlite = await Database.load('sqlite:lume_v2.db'); const sqlite = await Database.load('sqlite:lume_v2.db');
const platformName = await platform(); const platformName = await platform();
const dir = await appConfigDir();
const lumeStorage = new LumeStorage(sqlite, platformName); const lumeStorage = new LumeStorage(sqlite, platformName);
if (!lumeStorage.account) await lumeStorage.getActiveAccount(); 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 // check update
const update = await check(); const update = await check();
if (update) { if (update) {
@ -44,7 +54,6 @@ const StorageProvider = ({ children }: PropsWithChildren<object>) => {
} }
setDB(lumeStorage); setDB(lumeStorage);
console.info(dir);
} catch (e) { } catch (e) {
await message(`Cannot initialize database: ${e}`, { await message(`Cannot initialize database: ${e}`, {
title: 'Lume', title: 'Lume',

View File

@ -4,6 +4,8 @@ import { ReactNode } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import reactStringReplace from 'react-string-replace'; import reactStringReplace from 'react-string-replace';
import { useStorage } from '@libs/storage/provider';
import { import {
Hashtag, Hashtag,
ImagePreview, ImagePreview,
@ -44,6 +46,8 @@ const VIDEOS = [
]; ];
export function useRichContent(content: string, textmode: boolean = false) { export function useRichContent(content: string, textmode: boolean = false) {
const { db } = useStorage();
let parsedContent: string | ReactNode[] = content.replace(/\n+/g, '\n'); let parsedContent: string | ReactNode[] = content.replace(/\n+/g, '\n');
let linkPreview: string; let linkPreview: string;
let images: string[] = []; let images: string[] = [];
@ -54,8 +58,10 @@ export function useRichContent(content: string, textmode: boolean = false) {
const words = text.split(/( |\n)/); const words = text.split(/( |\n)/);
if (!textmode) { if (!textmode) {
images = words.filter((word) => IMAGES.some((el) => word.endsWith(el))); if (db.settings.media) {
videos = words.filter((word) => VIDEOS.some((el) => word.endsWith(el))); 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))); 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) { if (hashtags.length) {
hashtags.forEach((hashtag) => { hashtags.forEach((hashtag) => {
parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => ( parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => {
<Hashtag key={match + i} tag={hashtag} /> if (db.settings.hashtag) return <Hashtag key={match + i} tag={hashtag} />;
)); return null;
});
}); });
} }