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 () => {
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 }));
};

View File

@ -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) {

View File

@ -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<object>) => {
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<object>) => {
}
setDB(lumeStorage);
console.info(dir);
} catch (e) {
await message(`Cannot initialize database: ${e}`, {
title: 'Lume',

View File

@ -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) => (
<Hashtag key={match + i} tag={hashtag} />
));
parsedContent = reactStringReplace(parsedContent, hashtag, (match, i) => {
if (db.settings.hashtag) return <Hashtag key={match + i} tag={hashtag} />;
return null;
});
});
}