From c97dcc94e3a7aca78ed85d4bd1c44dfb7ba2534c Mon Sep 17 00:00:00 2001 From: Shusui MOYATANI Date: Sun, 18 Jun 2023 01:10:25 +0900 Subject: [PATCH] fix: lost contacts --- src/components/modal/ProfileDisplay.tsx | 76 ++++++++++++------------- src/nostr/useFollowings.ts | 2 +- src/nostr/useProfile.ts | 19 +++---- 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/components/modal/ProfileDisplay.tsx b/src/components/modal/ProfileDisplay.tsx index 0cb8bd0..c057972 100644 --- a/src/components/modal/ProfileDisplay.tsx +++ b/src/components/modal/ProfileDisplay.tsx @@ -24,6 +24,7 @@ import useVerification from '@/nostr/useVerification'; import ensureNonNull from '@/utils/ensureNonNull'; import epoch from '@/utils/epoch'; import npubEncodeFallback from '@/utils/npubEncodeFallback'; +import sleep from '@/utils/sleep'; import timeout from '@/utils/timeout'; export type ProfileDisplayProps = { @@ -47,6 +48,7 @@ const ProfileDisplay: Component = (props) => { const npub = createMemo(() => npubEncodeFallback(props.pubkey)); + const [updatingContacts, setUpdatingContacts] = createSignal(false); const [hoverFollowButton, setHoverFollowButton] = createSignal(false); const [showFollowers, setShowFollowers] = createSignal(false); @@ -117,48 +119,42 @@ const ProfileDisplay: Component = (props) => { }, }); - const handlePromise = - (f: (...params: Params) => Promise) => - (onError: (err: any) => void) => - (...params: Params) => { - f(...params).catch((err) => { - onError(err); + const updateContacts = async (update: (current: string[]) => string[]) => { + try { + const p = myPubkey(); + if (p == null) return; + setUpdatingContacts(true); + + await refetchMyFollowing(); + await sleep(3000); + + const current = myFollowingPubkeys(); + console.debug('current pubkeys', current); + + await updateContactsMutation.mutateAsync({ + relayUrls: config().relayUrls, + pubkey: p, + content: myFollowingQuery.data?.content ?? '', + followingPubkeys: uniq(update(current)), }); - }; + } finally { + setUpdatingContacts(false); + } + }; - const follow = handlePromise(async () => { - const p = myPubkey(); - if (p == null) return; - if (!myFollowingQuery.isFetched) return; - - await refetchMyFollowing(); - updateContactsMutation.mutate({ - relayUrls: config().relayUrls, - pubkey: p, - content: myFollowingQuery.data?.content ?? '', - followingPubkeys: uniq([...myFollowingPubkeys(), props.pubkey]), + const follow = () => { + updateContacts((current) => [...current, props.pubkey]).catch((err) => { + console.log('failed to follow', err); }); - })((err) => { - console.log('failed to follow', err); - }); - - const unfollow = handlePromise(async () => { - const p = myPubkey(); - if (p == null) return; - if (!myFollowingQuery.isFetched) return; + }; + const unfollow = () => { if (!window.confirm('本当にフォロー解除しますか?')) return; - await refetchMyFollowing(); - updateContactsMutation.mutate({ - relayUrls: config().relayUrls, - pubkey: p, - content: myFollowingQuery.data?.content ?? '', - followingPubkeys: myFollowingPubkeys().filter((k) => k !== props.pubkey), + updateContacts((current) => current.filter((k) => k !== props.pubkey)).catch((err) => { + console.log('failed to unfollow', err); }); - })((err) => { - console.log('failed to unfollow', err); - }); + }; const menu: MenuItem[] = [ /* @@ -247,16 +243,16 @@ const ProfileDisplay: Component = (props) => { 編集 + + + 更新中 + + 読み込み中 - - - 更新中 - -