localState fixes

This commit is contained in:
Martti Malmi 2023-08-30 11:19:09 +03:00
parent 3718e6ef5c
commit cabbe9a4bf
6 changed files with 35 additions and 15 deletions

View File

@ -8,6 +8,8 @@ import {
import { useEffect, useState } from 'preact/hooks';
import { Link } from 'preact-router';
import useLocalState from '@/state/useLocalState.ts';
import Key from '../nostr/Key';
import localState from '../state/LocalState.ts';
import Icons from '../utils/Icons';
@ -23,12 +25,11 @@ const MENU_ITEMS = [
];
const Footer = () => {
const [isMyProfile, setIsMyProfile] = useState(false);
const [isMyProfile] = useLocalState('isMyProfile', false);
const [activeRoute, setActiveRoute] = useState('/');
const [chatId, setChatId] = useState(null);
useEffect(() => {
localState.get('isMyProfile').on((value) => setIsMyProfile(value));
localState.get('activeRoute').on((activeRoute) => {
const replaced = activeRoute.replace('/chat/new', '').replace('/chat/', '');
const chatId = replaced.length < activeRoute.length ? replaced : null;

View File

@ -7,7 +7,6 @@ import {
signEvent,
UnsignedEvent,
} from 'nostr-tools';
import { route } from 'preact-router';
import { PublicKey } from '@/utils/Hex/Hex.ts';
@ -32,10 +31,10 @@ export default {
windowNostrQueue: [] as any[],
isProcessingQueue: false,
getPublicKey, // TODO confusing similarity to getPubKey
loginAsNewUser(redirect = false) {
this.login(this.generateKey(), redirect);
loginAsNewUser() {
this.login(this.generateKey());
},
login(key: any, redirect = false) {
login(key: any) {
const shouldRefresh = !!this.key;
this.key = key;
localStorage.setItem('iris.myKey', JSON.stringify(key));
@ -43,11 +42,6 @@ export default {
location.reload();
}
localState.get('loggedIn').put(true);
if (redirect) {
setTimeout(() => {
route('/following');
});
}
localState.get('showLoginModal').put(false);
},
generateKey(): Key {

View File

@ -101,7 +101,32 @@ describe('Node', () => {
expect(mockCallback).toHaveBeenCalledTimes(2);
});
it('should trigger map callbacks when a new child is added', async () => {
it('should trigger map callbacks when children are added', async () => {
const mockCallback: Callback = vi.fn();
const unsubscribe: Unsubscribe = node.map(mockCallback);
await node.get('child1').put('value1');
await node.get('child2').put('value2');
expect(mockCallback).toHaveBeenCalledWith(
'value1',
'test/child1',
expect.any(Number),
expect.any(Function),
);
expect(mockCallback).toHaveBeenCalledWith(
'value2',
'test/child2',
expect.any(Number),
expect.any(Function),
);
unsubscribe();
await node.get('child3').put('value3');
expect(mockCallback).toHaveBeenCalledTimes(2);
});
it('should trigger map callbacks when a nested child is added', async () => {
const node = new Node({ id: 'root', adapters: [new MemoryAdapter()] });
const mockCallback: Callback = vi.fn();
const unsubscribe = node.get('chats').map(mockCallback);

View File

@ -124,7 +124,7 @@ export default class Node {
});
}
// note to self: may be problematic that on behaves differently for leaf and branch nodes
// is it problematic that on behaves differently for leaf and branch nodes?
/**
* Subscribe to a value
* @param callback

View File

@ -49,7 +49,7 @@ const ExistingAccountLogin: React.FC<Props> = ({ fullScreen, onBack }) => {
if (!k) {
return;
}
await Key.login(k, fullScreen);
await Key.login(k);
event.target.value = '';
Helpers.copyToClipboard(''); // clear the clipboard
},

View File

@ -22,7 +22,7 @@ const Login: React.FC<Props> = ({ fullScreen }) => {
const loginAsNewUser = () => {
console.log('name', name);
Key.loginAsNewUser(fullScreen);
Key.loginAsNewUser();
localState.get('showFollowSuggestions').put(true);
name &&
setTimeout(() => {