Files
Toju/src/app/store/rooms/rooms.selectors.ts
2026-03-03 22:56:12 +01:00

106 lines
3.0 KiB
TypeScript

import { createFeatureSelector, createSelector } from '@ngrx/store';
import { RoomsState } from './rooms.reducer';
/** Selects the top-level rooms feature state. */
export const selectRoomsState = createFeatureSelector<RoomsState>('rooms');
/** Selects the room the user is currently viewing. */
export const selectCurrentRoom = createSelector(
selectRoomsState,
(state) => state.currentRoom
);
/** Selects the current room's settings (name, topic, privacy, etc.). */
export const selectRoomSettings = createSelector(
selectRoomsState,
(state) => state.roomSettings
);
/** Selects server search results from the directory. */
export const selectSearchResults = createSelector(
selectRoomsState,
(state) => state.searchResults
);
/** Whether a server directory search is currently in progress. */
export const selectIsSearching = createSelector(
selectRoomsState,
(state) => state.isSearching
);
/** Whether a room connection is being established. */
export const selectIsConnecting = createSelector(
selectRoomsState,
(state) => state.isConnecting
);
/** Whether the user is currently connected to a room. */
export const selectIsConnected = createSelector(
selectRoomsState,
(state) => state.isConnected
);
/** Selects the most recent rooms-related error message. */
export const selectRoomsError = createSelector(
selectRoomsState,
(state) => state.error
);
/** Selects the ID of the current room, or null. */
export const selectCurrentRoomId = createSelector(
selectCurrentRoom,
(room) => room?.id ?? null
);
/** Selects the display name of the current room. */
export const selectCurrentRoomName = createSelector(
selectCurrentRoom,
(room) => room?.name ?? ''
);
/** Selects the host ID of the current room (for ownership checks). */
export const selectIsCurrentUserHost = createSelector(
selectCurrentRoom,
(room) => room?.hostId
);
/** Selects all locally-saved rooms. */
export const selectSavedRooms = createSelector(
selectRoomsState,
(state) => state.savedRooms
);
/** Whether rooms are currently being loaded from local storage. */
export const selectRoomsLoading = createSelector(
selectRoomsState,
(state) => state.loading
);
/** Selects the ID of the currently active text channel. */
export const selectActiveChannelId = createSelector(
selectRoomsState,
(state) => state.activeChannelId
);
/** Selects all channels defined on the current room. */
export const selectCurrentRoomChannels = createSelector(
selectCurrentRoom,
(room) => room?.channels ?? []
);
/** Selects only text channels, sorted by position. */
export const selectTextChannels = createSelector(
selectCurrentRoomChannels,
(channels) => channels
.filter((channel) => channel.type === 'text')
.sort((channelA, channelB) => channelA.position - channelB.position)
);
/** Selects only voice channels, sorted by position. */
export const selectVoiceChannels = createSelector(
selectCurrentRoomChannels,
(channels) => channels
.filter((channel) => channel.type === 'voice')
.sort((channelA, channelB) => channelA.position - channelB.position)
);