Files
lnreader/src/hooks/useSourceStorage.ts
T
Rider21 be2dab5b5d feat: Add New Sources (#583)
* fix

* Update ranobelib.js

* fix

* test

* fix

* fix path

* test

* added sources

* added ALL sources on novel.tl

* add tags

* Update NovelTlScraper.js

* fix
2023-03-30 13:19:00 +05:30

65 lines
1.6 KiB
TypeScript

import CookieManager from '@react-native-cookies/cookies';
import { useMMKVObject } from 'react-native-mmkv';
import { MMKVStorage } from '@utils/mmkv/mmkv';
import { showToast } from './showToast';
export const SOURCE_STORAGE = 'SOURCE_STORAGE';
interface SourceStorage {
cookies?: any;
}
export type SourceStorageMap = Record<number, Partial<SourceStorage>>;
const useSourceStorage = ({ sourceId = -1 }: { sourceId?: number }) => {
const [values, setValues] = useMMKVObject<SourceStorageMap>(
SOURCE_STORAGE,
MMKVStorage,
);
const setSourceStorage = (key: keyof SourceStorage, value: any): void => {
setValues({
...values,
[sourceId]: {
...values?.[sourceId],
[key]: value,
},
});
};
const clearCookies = (): void => {
// let tempStorage = {};
// if (values) {
// tempStorage = Object.fromEntries(
// Object.entries(values).map(([id, sourceData]) => {
// delete sourceData.cookies;
// return [id, sourceData];
// }),
// );
// }
CookieManager.clearAll();
CookieManager.clearAll(true); //clears cookies in webkit
setValues({});
showToast('Cookies cleared');
};
const sourceStorage = values?.[sourceId];
return {
...sourceStorage,
setSourceStorage,
clearCookies,
};
};
export default useSourceStorage;
export const getSourceStorage = (sourceId: number) => {
const rawSettings = MMKVStorage.getString(SOURCE_STORAGE) || '{}';
const parsedSettings: Partial<SourceStorageMap> = JSON.parse(rawSettings);
return parsedSettings[sourceId] || {};
};