mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 22:44:26 +01:00
update
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { createSignal, createEffect, onMount, Accessor, Setter } from 'solid-js';
|
||||
import { createSignal, createEffect, onMount, type Signal } from 'solid-js';
|
||||
import { createStore, SetStoreFunction, type Store, type StoreNode } from 'solid-js/store';
|
||||
|
||||
type GenericStorage<T> = {
|
||||
getItem(key: string): T | null;
|
||||
@@ -28,20 +29,42 @@ export const createSignalWithStorage = <T>(
|
||||
key: string,
|
||||
initialValue: T,
|
||||
storage: GenericStorage<T>,
|
||||
): [Accessor<T>, Setter<T>] => {
|
||||
): Signal<T> => {
|
||||
const [loaded, setLoaded] = createSignal<boolean>(false);
|
||||
const [value, setValue] = createSignal<T>(initialValue);
|
||||
const [state, setState] = createSignal(initialValue);
|
||||
|
||||
onMount(() => {
|
||||
const data = storage.getItem(key);
|
||||
// If there is no data, default value is used.
|
||||
if (data != null) setValue(() => data);
|
||||
if (data != null) setState(() => data);
|
||||
setLoaded(true);
|
||||
});
|
||||
|
||||
createEffect(() => {
|
||||
if (loaded()) storage.setItem(key, value());
|
||||
if (loaded()) storage.setItem(key, state());
|
||||
});
|
||||
|
||||
return [value, setValue];
|
||||
return [state, setState];
|
||||
};
|
||||
|
||||
export const createStoreWithStorage = <T extends StoreNode>(
|
||||
key: string,
|
||||
initialValue: T,
|
||||
storage: GenericStorage<T>,
|
||||
): [Store<T>, SetStoreFunction<T>] => {
|
||||
const [loaded, setLoaded] = createSignal<boolean>(false);
|
||||
const [state, setState] = createStore<T>(initialValue);
|
||||
|
||||
onMount(() => {
|
||||
const data = storage.getItem(key);
|
||||
// If there is no data, default value is used.
|
||||
if (data != null) setState(data);
|
||||
setLoaded(true);
|
||||
});
|
||||
|
||||
createEffect(() => {
|
||||
if (loaded()) storage.setItem(key, state);
|
||||
});
|
||||
|
||||
return [state, setState];
|
||||
};
|
||||
|
||||
20
src/hooks/useModalState.ts
Normal file
20
src/hooks/useModalState.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { createSignal, type Signal } from 'solid-js';
|
||||
|
||||
type ModalState =
|
||||
| { type: 'Profile'; pubkey: string }
|
||||
| { type: 'UserTimeline'; pubkey: string }
|
||||
| { type: 'Closed' };
|
||||
|
||||
const [modalState, setModalState] = createSignal<ModalState>({ type: 'Closed' });
|
||||
|
||||
const useModalState = () => {
|
||||
const showProfile = (pubkey: string) => {
|
||||
setModalState({ type: 'Profile', pubkey });
|
||||
};
|
||||
const closeModal = () => {
|
||||
setModalState({ type: 'Closed' });
|
||||
};
|
||||
return { modalState, setModalState, showProfile, closeModal };
|
||||
};
|
||||
|
||||
export default useModalState;
|
||||
Reference in New Issue
Block a user