diff --git a/src/lib/feature/community/CommunityItem.svelte b/src/lib/feature/community/CommunityItem.svelte index 40c43636..5c60bfa5 100644 --- a/src/lib/feature/community/CommunityItem.svelte +++ b/src/lib/feature/community/CommunityItem.svelte @@ -27,6 +27,29 @@ showCounts = true, children, }: Props = $props() + + // Optimistic subscribe state lives locally instead of mutating the + // `community` prop, which this component does not own (the list passes it + // unbound). Server state wins again on the next load. + let subscribedOverride = $state(undefined) + let subscribed = $derived( + subscribedOverride ?? community.viewer?.subscribed === true, + ) + + async function toggleSubscribe(): Promise { + const wasSubscribed = subscribed + try { + if (wasSubscribed) { + await coves().unsubscribe({ community: community.did }) + } else { + await coves().subscribe({ community: community.did }) + } + subscribedOverride = !wasSubscribed + } catch (err) { + const errorMsg = err instanceof Error ? err.message : String(err) + toast({ content: errorMsg, type: 'error' }) + } + } {#snippet communityInfo()} @@ -61,27 +84,9 @@ {#if profile.current?.jwt} - {@const subscribed = community.viewer?.subscribed === true} {#if profile.current?.jwt} - {@const subscribed = community.viewer?.subscribed === true}