refactor(bundles): add RootState type to selectors

This commit is contained in:
Christopher Astfalk
2021-05-19 17:49:02 +02:00
parent 734aff7101
commit 609462f55a
12 changed files with 52 additions and 44 deletions
+10 -9
View File
@@ -1,5 +1,6 @@
import { createSlice, createSelector, PayloadAction, Action } from '@reduxjs/toolkit'
import { RootState } from '../store/configureStore'
import { AvatarData, SavedAvatarData, Grid, HoodieObject } from '../types/viewer'
// Reducer for viewer-account and state
@@ -143,19 +144,19 @@ export const {
// Selectors
export const selectIsSignedIn = (state: any): boolean => state.account.loggedIn
export const selectIsSignedIn = (state: RootState) => state.account.loggedIn
export const selectIsUnlocked = (state: any): boolean => state.account.unlocked
export const selectIsUnlocked = (state: RootState) => state.account.unlocked
export const selectUserName = (state: any): string => state.account.username
export const selectUserName = (state: RootState) => state.account.username
export const selectSavedAvatars = (state: any): SavedAvatarData[] => state.account.savedAvatars
export const selectSavedAvatars = (state: RootState) => state.account.savedAvatars
export const selectSavedAvatarsAreLoaded = (state: any): boolean => state.account.savedAvatarsLoaded
export const selectSavedAvatarsAreLoaded = (state: RootState) => state.account.savedAvatarsLoaded
export const selectAnonymAvatarData = (state: any): AvatarData => state.account.anonymAvatarData
export const selectAnonymAvatarData = (state: RootState) => state.account.anonymAvatarData!
export const selectSavedGrids = (state: any): Grid[] => state.account.savedGrids
export const selectSavedGrids = (state: RootState) => state.account.savedGrids as Grid[]
export const selectGridsByName = createSelector(
selectSavedGrids,
@@ -168,7 +169,7 @@ export const selectGridsByName = createSelector(
}
)
export const selectSavedGridsAreLoaded = (state: any): boolean => state.account.savedGridsLoaded
export const selectSavedGridsAreLoaded = (state: RootState) => state.account.savedGridsLoaded
export const selectShowUnlockDialog = createSelector(
[
@@ -178,7 +179,7 @@ export const selectShowUnlockDialog = createSelector(
(isSignedIn, isUnlocked) => !isUnlocked && isSignedIn
)
export const selectResetKeys = (state: any): string[] | null => state.account.resetKeys
export const selectResetKeys = (state: RootState) => state.account.resetKeys
// Helpers
+5 -4
View File
@@ -8,6 +8,7 @@ import { login, logout, userWasKicked, LoginAction } from './session'
import { getValueOf, mapBlockOf } from '../network/msgGetters'
import { RootState } from '../store/configureStore'
import { Friend } from '../types/people'
export type FriendOnlineStateAction = PayloadAction<{
@@ -77,11 +78,11 @@ const friendsSlice = createSlice({
[login.type] (state, action: PayloadAction<LoginAction>) {
// If a avatar has no buddies, then the buddy-list doesn't exist!
if (!Array.isArray(action.payload.sessionInfo['buddy-list'])) return state
return action.payload.sessionInfo['buddy-list'].map(friend => {
const rightsGiven = friend.buddy_rights_given // from me to friend
const rightsHas = friend.buddy_rights_has // Friend has given me rights
return {
id: friend.buddy_id,
online: false,
@@ -101,9 +102,9 @@ export default friendsSlice.reducer
export const { changeRights, onlineStateChanged } = friendsSlice.actions
// selectors
export const selectFriends = (state: any): Friend[] => state.friends
export const selectFriends = (state: RootState): Friend[] => state.friends
export const selectFriendById = (state: any, id: string) => selectFriends(state)
export const selectFriendById = (state: RootState, id: string) => selectFriends(state)
.find(friend => friend.id === id)
// Helpers
+2 -1
View File
@@ -7,6 +7,7 @@ import { logout, userWasKicked } from './session'
import { getValueOf, mapBlockOf } from '../network/msgGetters'
import { RootState } from '../store/configureStore'
import { Group } from '../types/people'
const groupSlice = createSlice({
@@ -125,7 +126,7 @@ export const { chatSessionStarted } = groupSlice.actions
* Select all groups the user is part of.
* @param {object} state Reducer State
*/
export const selectGroups = (state: any): Group[] => state.groups
export const selectGroups = (state: RootState): Group[] => state.groups
export const selectGroupsIDs = createSelector(
[
+3 -2
View File
@@ -7,6 +7,7 @@ import { createSlice, createSelector, PayloadAction } from '@reduxjs/toolkit'
import { chatSessionStarted } from './groups'
import { logout, userWasKicked } from './session'
import { RootState } from '../store/configureStore'
import { IMChatType, IMChat, InstantMessage } from '../types/chat'
const imSlice = createSlice({
@@ -236,7 +237,7 @@ export const {
// Selectors
export const selectIMChats = (state: any): { [key: string]: IMChat } => state.IMs.chats
export const selectIMChats = (state: RootState): { [key: string]: IMChat } => state.IMs.chats
export const selectActiveIMChats = createSelector(
[
@@ -245,7 +246,7 @@ export const selectActiveIMChats = createSelector(
chats => Object.values(chats).filter(chat => chat.active)
)
export const selectChatMessages = (state: any, id: string): InstantMessage[] | undefined => {
export const selectChatMessages = (state: RootState, id: string): InstantMessage[] | undefined => {
return state.IMs.messages[id]
}
+7 -5
View File
@@ -4,6 +4,7 @@ import { createReducer, PayloadAction } from '@reduxjs/toolkit'
import { login, logout, userWasKicked, LoginAction } from './session'
import { RootState } from '../store/configureStore'
import { Folder, AssetType, FolderType } from '../types/inventory'
export default createReducer({
@@ -56,18 +57,19 @@ export default createReducer({
}
})
export const selectFolders = (state: any): { [key: string]: Folder } => state.inventory.folders
export const selectFolders = (state: RootState): { [key: string]: Folder } =>
state.inventory.folders
export const selectFolderById = (state: any, id: string) => selectFolders(state)[id]
export const selectFolderById = (state: RootState, id: string) => selectFolders(state)[id]
export const selectInventoryRootId = (state: any): string => state.inventory.root
export const selectInventoryRootId = (state: RootState): string => state.inventory.root!
export const selectInventoryRoot = (state: any) => selectFolderById(
export const selectInventoryRoot = (state: RootState) => selectFolderById(
state,
selectInventoryRootId(state)
)
export const selectFolderForAssetType = (state: any, type: AssetType) => {
export const selectFolderForAssetType = (state: RootState, type: AssetType) => {
const rootFolder = selectInventoryRoot(state)
// Folders are loaded on login and removed on logout.
+2 -1
View File
@@ -7,6 +7,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { UUID as LLUUID } from '../llsd'
import { login, logout, userWasKicked, LoginAction } from './session'
import { RootState } from '../store/configureStore'
import {
LocalChatMessage,
LocalChatAudible,
@@ -151,4 +152,4 @@ export const {
savingFinished
} = chatSlice.actions
export const selectLocalChat = (state: any): LocalChatMessage[] => state.localChat
export const selectLocalChat = (state: RootState): LocalChatMessage[] => state.localChat
+4 -3
View File
@@ -25,6 +25,7 @@ import {
} from './session'
import { UUID as LLUUID } from '../llsd'
import { RootState } from '../store/configureStore'
import {
LocalChatMessage,
LocalChatSourceType,
@@ -218,13 +219,13 @@ export const {
displayNamesLoaded
} = nameSlice.actions
export const selectNames = (state: any): { [key: string]: AvatarName } => state.names.names
export const selectNames = (state: RootState): { [key: string]: AvatarName } => state.names.names
export function selectAvatarNameById (state: any, id: string): AvatarName | undefined {
export function selectAvatarNameById (state: RootState, id: string): AvatarName | undefined {
return selectNames(state)[id]
}
export const selectDisplayNamesURL = (state: any): string => state.names.getDisplayNamesURL
export const selectDisplayNamesURL = (state: RootState): string => state.names.getDisplayNamesURL
export const selectOwnAvatarName = createSelector(
[
+2 -1
View File
@@ -3,6 +3,7 @@ import { createSlice, createSelector, PayloadAction } from '@reduxjs/toolkit'
import { onlineStateChanged, FriendOnlineStateAction } from './friends'
import { logout, userWasKicked, selectActiveTab } from './session'
import { RootState } from '../store/configureStore'
import { Notification, NotificationTypes } from '../types/chat'
const notificationSlice = createSlice({
@@ -59,7 +60,7 @@ export default notificationSlice.reducer
export const { receive, close } = notificationSlice.actions
export const selectNotifications = (state: any): Notification[] => state.notifications.active
export const selectNotifications = (state: RootState): Notification[] => state.notifications.active
export const selectShouldDisplayNotifications = createSelector(
[
+7 -5
View File
@@ -4,6 +4,7 @@ import { login, logout, userWasKicked, LoginAction } from './session'
import { getValueOf, getStringValueOf } from '../network/msgGetters'
import { RootState } from '../store/configureStore'
import { Maturity, parseMaturity } from '../types/viewer'
function getInitialState () {
@@ -32,7 +33,7 @@ function getInitialState () {
useEstateSun: true,
sunHour: 0
},
sim: {
name: '',
ip: '',
@@ -125,10 +126,11 @@ const regionSlice = createSlice({
export default regionSlice.reducer
export const selectRegionId = (state: any): string => state.region.region.id
export const selectRegionId = (state: RootState): string => state.region.region.id
export const selectParentEstateID = (state: any): number => state.region.region.parentEstateID
export const selectParentEstateID = (state: RootState): number => state.region.region.parentEstateID
export const selectPosition = (state: any): number[] => state.region.position
export const selectPosition = (state: RootState): number[] => state.region.position
export const selectEventQueueGetUrl = (state: any): string => state.region.sim.eventQueueGetUrl
export const selectEventQueueGetUrl = (state: RootState): string =>
state.region.sim.eventQueueGetUrl
+8 -7
View File
@@ -5,6 +5,7 @@ import { createSlice, createSelector, PayloadAction, Action } from '@reduxjs/too
import { selectIsSignedIn, selectSavedAvatars, selectAnonymAvatarData } from './account'
import AvatarName from '../avatarName'
import { RootState } from '../store/configureStore'
import { LocalChatMessage } from '../types/chat'
import { Grid, Maturity, MaturityString, parseMaturity } from '../types/viewer'
@@ -90,10 +91,10 @@ export const {
// Selectors
export const selectAndromedaSessionId = (state: any): string | null =>
export const selectAndromedaSessionId = (state: RootState): string | null =>
state.session.andromedaSessionId
export const selectAvatarIdentifier = (state: any): string => state.session.avatarIdentifier
export const selectAvatarIdentifier = (state: RootState): string => state.session.avatarIdentifier!
export const selectCurrentAvatarData = createSelector(
[
@@ -113,11 +114,11 @@ export const selectAvatarDataSaveId = createSelector(
avatarData => avatarData != null ? avatarData.dataSaveId : null
)
export const selectErrorMessage = (state: any): string | null => state.session.error
export const selectErrorMessage = (state: RootState): string | null => state.session.error
export const selectAgentId = (state: any): string => state.session.agentId
export const selectAgentId = (state: RootState): string => state.session.agentId!
export const selectSessionId = (state: any): string => state.session.sessionId
export const selectSessionId = (state: RootState): string => state.session.sessionId!
export const selectIsLoggedIn = createSelector(
[
@@ -127,9 +128,9 @@ export const selectIsLoggedIn = createSelector(
(avatarIdentifier, sessionId) => avatarIdentifier != null && sessionId != null
)
export const selectShouldSync = (state: any): boolean => state.session.sync
export const selectShouldSync = (state: RootState): boolean => state.session.sync
export const selectActiveTab = (state: any): string => state.session.activeChatTab
export const selectActiveTab = (state: RootState): string => state.session.activeChatTab
// checks if the chat history should be saved and synced
export const selectShouldSaveChat = createSelector(
+1 -1
View File
@@ -15,8 +15,8 @@ import { proxyFetch, fetchLLSD } from './llsdFetch'
import AvatarName from '../avatarName'
export type AppDispatch = ThunkDispatch<RootState, ExtraArguments, Action<string>>
export type RootState = ReturnType<typeof rootReducer>
export type AppDispatch = ThunkDispatch<RootState, ExtraArguments, Action<string>>
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
RootState,
+1 -5
View File
@@ -80,11 +80,7 @@ export async function createTestStore ({ localDB, remoteDB, state = AppState.Log
state?: AppState
}) {
let isSetup = true // when the the create Databases callback is called the first time
const extraArgument = createExtraArgument(({ local, remote }: {
local?: boolean,
remote?: string,
skipSetup?: boolean
}) => {
const extraArgument = createExtraArgument(({ local, remote }) => {
const result: { local: PouchDB.Database | null, remote: PouchDB.Database | null } = {
local: null,
remote: null