Skip to content

Commit

Permalink
Use userRoomsByUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Staab committed Dec 17, 2024
1 parent d0565e7 commit 166bd81
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/app/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
loadMembership,
loadSettings,
getDefaultPubkeys,
getMembershipUrls,
userRoomsByUrl,
} from "@app/state"

// Utils
Expand Down Expand Up @@ -301,7 +301,7 @@ export const setRelayPolicy = (url: string, read: boolean, write: boolean) => {
url,
...INDEXER_RELAYS,
...ctx.app.router.FromUser().getUrls(),
...getMembershipUrls(userMembership.get()),
...userRoomsByUrl.get().keys(),
],
})
}
Expand All @@ -322,7 +322,7 @@ export const setInboxRelayPolicy = (url: string, enabled: boolean) => {
relays: [
...INDEXER_RELAYS,
...ctx.app.router.FromUser().getUrls(),
...getMembershipUrls(userMembership.get()),
...userRoomsByUrl.get().keys(),
],
})
}
Expand Down
5 changes: 2 additions & 3 deletions src/app/components/MenuSpace.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import RoomCreate from "@app/components/RoomCreate.svelte"
import MenuSpaceRoomItem from "@app/components/MenuSpaceRoomItem.svelte"
import {
getMembershipUrls,
userRoomsByUrl,
hasMembershipUrl,
userMembership,
memberships,
deriveUserRooms,
deriveOtherRooms,
Expand Down Expand Up @@ -93,7 +92,7 @@
</Button>
</li>
<li>
{#if getMembershipUrls($userMembership).includes(url)}
{#if $userRoomsByUrl.has(url)}
<Button on:click={leaveSpace} class="text-error">
<Icon icon="exit" />
Leave Space
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/MenuSpaces.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import CardButton from "@lib/components/CardButton.svelte"
import MenuSpacesItem from "@app/components/MenuSpacesItem.svelte"
import SpaceAdd from "@app/components/SpaceAdd.svelte"
import {userMembership, getMembershipUrls, PLATFORM_RELAY} from "@app/state"
import {userRoomsByUrl, PLATFORM_RELAY} from "@app/state"
import {pushModal} from "@app/modal"
const addSpace = () => pushModal(SpaceAdd)
Expand All @@ -15,8 +15,8 @@
{#if PLATFORM_RELAY}
<MenuSpacesItem url={PLATFORM_RELAY} />
<Divider />
{:else if getMembershipUrls($userMembership).length > 0}
{#each getMembershipUrls($userMembership) as url (url)}
{:else if $userRoomsByUrl.size > 0}
{#each $userRoomsByUrl.keys() as url (url)}
<MenuSpacesItem {url} />
{/each}
<Divider />
Expand Down
10 changes: 2 additions & 8 deletions src/app/components/PrimaryNav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
import MenuSpaces from "@app/components/MenuSpaces.svelte"
import MenuSettings from "@app/components/MenuSettings.svelte"
import PrimaryNavItemSpace from "@app/components/PrimaryNavItemSpace.svelte"
import {
userMembership,
getMembershipUrls,
canDecrypt,
PLATFORM_RELAY,
PLATFORM_LOGO,
} from "@app/state"
import {userRoomsByUrl, canDecrypt, PLATFORM_RELAY, PLATFORM_LOGO} from "@app/state"
import {pushModal} from "@app/modal"
import {makeSpacePath} from "@app/routes"
import {notifications} from "@app/notifications"
Expand All @@ -31,7 +25,7 @@
const openChat = () => ($canDecrypt ? goto("/chat") : pushModal(ChatEnable, {next: "/chat"}))
$: spaceUrls = getMembershipUrls($userMembership)
$: spaceUrls = Array.from($userRoomsByUrl.keys())
$: spacePaths = spaceUrls.map(url => makeSpacePath(url))
$: anySpaceNotifications = spacePaths.some(
path => !$page.url.pathname.startsWith(path) && $notifications.has(path),
Expand Down
4 changes: 2 additions & 2 deletions src/app/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Page} from "@sveltejs/kit"
import {userMembership, makeChatId, decodeRelay, encodeRelay, getMembershipUrls} from "@app/state"
import {makeChatId, decodeRelay, encodeRelay, userRoomsByUrl} from "@app/state"

export const makeSpacePath = (url: string, ...extra: string[]) => {
let path = `/spaces/${encodeRelay(url)}`
Expand Down Expand Up @@ -28,7 +28,7 @@ export const makeThreadPath = (url: string, eventId?: string) => {
export const getPrimaryNavItem = ($page: Page) => $page.route?.id?.split("/")[1]

export const getPrimaryNavItemIndex = ($page: Page) => {
const urls = getMembershipUrls(userMembership.get())
const urls = Array.from(userRoomsByUrl.get().keys())

switch (getPrimaryNavItem($page)) {
case "discover":
Expand Down
2 changes: 1 addition & 1 deletion src/app/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ export const userRoomsByUrl = withGetter(

export const deriveUserRooms = (url: string) =>
derived(userRoomsByUrl, $userRoomsByUrl =>
sortBy(roomComparator(url), uniq(Array.from($userRoomsByUrl.get(url) || []))),
sortBy(roomComparator(url), uniq(Array.from($userRoomsByUrl.get(url) || [GENERAL]))),
)

export const deriveOtherRooms = (url: string) =>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@
}
// Listen for space data, populate space-based notifications
let spacesSub: any
let unsubSpaces: any
userMembership.subscribe($membership => {
spacesSub?.close()
spacesSub = listenForNotifications()
unsubSpaces?.()
unsubSpaces = listenForNotifications()
})
// Listen for chats, populate chat-based notifications
Expand Down
4 changes: 2 additions & 2 deletions src/routes/discover/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import SpaceCheck from "@app/components/SpaceCheck.svelte"
import ProfileCircles from "@app/components/ProfileCircles.svelte"
import {
userMembership,
memberships,
membershipByPubkey,
getMembershipUrls,
userRoomsByUrl,
getDefaultPubkeys,
} from "@app/state"
import {discoverRelays} from "@app/commands"
Expand Down Expand Up @@ -98,7 +98,7 @@
{/if}
</div>
</div>
{#if getMembershipUrls($userMembership).includes(relay.url)}
{#if $userRoomsByUrl.has(relay.url)}
<div
class="tooltip absolute -right-1 -top-1 h-5 w-5 rounded-full bg-primary"
data-tip="You are already a member of this space.">
Expand Down
5 changes: 2 additions & 3 deletions src/routes/spaces/[relay]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
channelsById,
deriveUserRooms,
deriveOtherRooms,
userMembership,
getMembershipUrls,
userRoomsByUrl,
} from "@app/state"
import {makeChatPath, makeRoomPath, makeSpacePath} from "@app/routes"
import {notifications} from "@app/notifications"
Expand Down Expand Up @@ -51,7 +50,7 @@
</div>
<strong slot="title">Home</strong>
<div slot="action" class="row-2">
{#if !getMembershipUrls($userMembership).includes(url)}
{#if !$userRoomsByUrl.has(url)}
<Button class="btn btn-primary btn-sm" on:click={joinSpace}>
<Icon icon="login-2" />
Join Space
Expand Down
5 changes: 2 additions & 3 deletions src/routes/spaces/[relay]/[room]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import ChannelCompose from "@app/components/ChannelCompose.svelte"
import {
userSettingValues,
userMembership,
decodeRelay,
deriveEventsForUrl,
GENERAL,
tagRoom,
LEGACY_MESSAGE,
getMembershipRoomsByUrl,
userRoomsByUrl,
displayChannel,
} from "@app/state"
import {setChecked} from "@app/notifications"
Expand Down Expand Up @@ -160,7 +159,7 @@
</strong>
<div slot="action" class="row-2">
{#if room !== GENERAL}
{#if getMembershipRoomsByUrl(url, $userMembership).includes(room)}
{#if $userRoomsByUrl.get(url)?.has(room)}
<Button class="btn btn-neutral btn-sm" on:click={leaveRoom}>
<Icon icon="arrows-a-logout-2" />
Leave Room
Expand Down

0 comments on commit 166bd81

Please sign in to comment.