From cabbe9a4bfdf4eb8e6de03dfe477954c3289db4f Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 30 Aug 2023 11:19:09 +0300 Subject: [PATCH] localState fixes --- src/js/components/Footer.tsx | 5 ++-- src/js/nostr/Key.ts | 12 +++------ src/js/state/Node.test.ts | 27 ++++++++++++++++++++- src/js/state/Node.ts | 2 +- src/js/views/login/ExistingAccountLogin.tsx | 2 +- src/js/views/login/Login.tsx | 2 +- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/js/components/Footer.tsx b/src/js/components/Footer.tsx index 4fd825a7..3f7b3f4e 100644 --- a/src/js/components/Footer.tsx +++ b/src/js/components/Footer.tsx @@ -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; diff --git a/src/js/nostr/Key.ts b/src/js/nostr/Key.ts index ab7d727b..21c8eab5 100644 --- a/src/js/nostr/Key.ts +++ b/src/js/nostr/Key.ts @@ -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 { diff --git a/src/js/state/Node.test.ts b/src/js/state/Node.test.ts index e729860d..a3c3b11a 100644 --- a/src/js/state/Node.test.ts +++ b/src/js/state/Node.test.ts @@ -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); diff --git a/src/js/state/Node.ts b/src/js/state/Node.ts index 712f13b0..8b5e5d88 100644 --- a/src/js/state/Node.ts +++ b/src/js/state/Node.ts @@ -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 diff --git a/src/js/views/login/ExistingAccountLogin.tsx b/src/js/views/login/ExistingAccountLogin.tsx index f51e33b3..54acc62d 100644 --- a/src/js/views/login/ExistingAccountLogin.tsx +++ b/src/js/views/login/ExistingAccountLogin.tsx @@ -49,7 +49,7 @@ const ExistingAccountLogin: React.FC = ({ fullScreen, onBack }) => { if (!k) { return; } - await Key.login(k, fullScreen); + await Key.login(k); event.target.value = ''; Helpers.copyToClipboard(''); // clear the clipboard }, diff --git a/src/js/views/login/Login.tsx b/src/js/views/login/Login.tsx index 98ab0b47..2191581b 100644 --- a/src/js/views/login/Login.tsx +++ b/src/js/views/login/Login.tsx @@ -22,7 +22,7 @@ const Login: React.FC = ({ fullScreen }) => { const loginAsNewUser = () => { console.log('name', name); - Key.loginAsNewUser(fullScreen); + Key.loginAsNewUser(); localState.get('showFollowSuggestions').put(true); name && setTimeout(() => {