Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions shared/constants/init/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ import {useDarkModeState} from '@/stores/darkmode'
import {useFollowerState} from '@/stores/followers'
import {useModalHeaderState} from '@/stores/modal-header'
import {useProvisionState} from '@/stores/provision'
import {usePushState} from '@/stores/push'
import {useSettingsContactsState} from '@/stores/settings-contacts'
import {useState as useRecoverPasswordState} from '@/stores/recover-password'
import {useTeamsState} from '@/stores/teams'
import {useTrackerState} from '@/stores/tracker'
import {useUsersState} from '@/stores/users'
import {useRouterState} from '@/stores/router'
import * as Util from '@/constants/router'
import {setConvoDefer} from '@/stores/convostate'
Expand Down Expand Up @@ -124,21 +121,9 @@ export const initTeamBuildingCallbacks = () => {
onAddMembersWizardPushMembers: (members: Array<T.Teams.AddingMember>) => {
useTeamsState.getState().dispatch.addMembersWizardPushMembers(members)
},
onGetSettingsContactsImportEnabled: () => {
return useSettingsContactsState.getState().importEnabled
},
onGetSettingsContactsUserCountryCode: () => {
return useSettingsContactsState.getState().userCountryCode
},
onShowUserProfile: (username: string) => {
navToProfile(username)
},
onUsersGetBlockState: (usernames: ReadonlyArray<string>) => {
useUsersState.getState().dispatch.getBlockState(usernames)
},
onUsersUpdates: (infos: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
useUsersState.getState().dispatch.updates(infos)
},
}

const namespaces: Array<T.TB.AllowedNamespace> = ['chat', 'crypto', 'teams', 'people']
Expand Down Expand Up @@ -193,25 +178,15 @@ export const initChat2Callbacks = () => {
dispatch: {
...currentState.dispatch,
defer: {
onGetDaemonState: () => {
const daemonState = storeRegistry.getState('daemon')
return {dispatch: daemonState.dispatch, handshakeVersion: daemonState.handshakeVersion}
},
onGetTeamsTeamIDToMembers: (teamID: T.Teams.TeamID) => {
return storeRegistry.getState('teams').teamIDToMembers.get(teamID)
},
onGetUsersInfoMap: () => {
return storeRegistry.getState('users').infoMap
},
onTeamsGetMembers: async (teamID: T.Teams.TeamID) => {
return storeRegistry.getState('teams').dispatch.getMembers(teamID)
},
onTeamsUpdateTeamRetentionPolicy: (metas: ReadonlyArray<T.Chat.ConversationMeta>) => {
storeRegistry.getState('teams').dispatch.updateTeamRetentionPolicy(metas)
},
onUsersUpdates: (updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
storeRegistry.getState('users').dispatch.updates(updates)
},
},
},
})
Expand All @@ -232,24 +207,6 @@ export const initTeamsCallbacks = () => {
) => {
storeRegistry.getState('chat').dispatch.previewConversation(p)
},
onUsersUpdates: (updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
storeRegistry.getState('users').dispatch.updates(updates)
},
},
},
})
}

export const initPushCallbacks = () => {
const currentState = usePushState.getState()
usePushState.setState({
dispatch: {
...currentState.dispatch,
defer: {
...currentState.dispatch.defer,
onGetDaemonHandshakeState: () => {
return useDaemonState.getState().handshakeState
},
},
},
})
Expand All @@ -270,21 +227,6 @@ export const initRecoverPasswordCallbacks = () => {
})
}

export const initTracker2Callbacks = () => {
const currentState = useTrackerState.getState()
useTrackerState.setState({
dispatch: {
...currentState.dispatch,
defer: {
...currentState.dispatch.defer,
onUsersUpdates: (updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
useUsersState.getState().dispatch.updates(updates)
},
},
},
})
}

export const initSharedSubscriptions = () => {
// HMR cleanup: unsubscribe old store subscriptions before re-subscribing
for (const unsub of _sharedUnsubs) unsub()
Expand Down Expand Up @@ -557,9 +499,7 @@ export const initSharedSubscriptions = () => {
initChat2Callbacks()
initTeamBuildingCallbacks()
initTeamsCallbacks()
initPushCallbacks()
initRecoverPasswordCallbacks()
initTracker2Callbacks()
}

// This is to defer loading stores we don't need immediately.
Expand Down
25 changes: 7 additions & 18 deletions shared/stores/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as TeamConstants from '@/constants/teams'
import * as Z from '@/util/zustand'
import isEqual from 'lodash/isEqual'
import logger from '@/logger'
import type {State as DaemonState} from '@/stores/daemon'
import type * as Router2 from '@/constants/router'
import {ProviderScreen} from '@/stores/convostate'
import type {GetOptionsRet, RouteDef} from '@/constants/types/router'
Expand All @@ -32,6 +31,8 @@ import {storeRegistry} from '@/stores/store-registry'
import {uint8ArrayToString} from '@/util/uint8array'
import {useConfigState} from '@/stores/config'
import {useCurrentUserState} from '@/stores/current-user'
import {useDaemonState} from '@/stores/daemon'
import {useUsersState} from '@/stores/users'
import {useWaitingState} from '@/stores/waiting'

const defaultTopReacjis = [
Expand Down Expand Up @@ -227,14 +228,11 @@ export type State = Store & {
badgesUpdated: (badgeState?: T.RPCGen.BadgeState) => void
clearMetas: () => void
defer: {
onGetDaemonState: () => Pick<DaemonState, 'dispatch' | 'handshakeVersion'>
onGetTeamsTeamIDToMembers: (
teamID: T.Teams.TeamID
) => ReadonlyMap<string, T.Teams.MemberInfo> | undefined
onGetUsersInfoMap: () => ReadonlyMap<string, T.Users.UserInfo>
onTeamsGetMembers: (teamID: T.Teams.TeamID) => Promise<void>
onTeamsUpdateTeamRetentionPolicy: (metas: ReadonlyArray<T.Chat.ConversationMeta>) => void
onUsersUpdates: (updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => void
}
createConversation: (participants: ReadonlyArray<string>, highlightMessageID?: T.Chat.MessageID) => void
ensureWidgetMetas: () => void
Expand Down Expand Up @@ -387,24 +385,15 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
ignorePromise(f())
},
defer: {
onGetDaemonState: () => {
throw new Error('onGetDaemonState not properly initialized')
},
onGetTeamsTeamIDToMembers: (_teamID: T.Teams.TeamID) => {
throw new Error('onGetTeamsTeamIDToMembers not properly initialized')
},
onGetUsersInfoMap: () => {
throw new Error('onGetUsersInfoMap not properly initialized')
},
onTeamsGetMembers: (_teamID: T.Teams.TeamID) => {
throw new Error('onTeamsGetMembers not properly initialized')
},
onTeamsUpdateTeamRetentionPolicy: (_metas: ReadonlyArray<T.Chat.ConversationMeta>) => {
throw new Error('onTeamsUpdateTeamRetentionPolicy not properly initialized')
},
onUsersUpdates: (_updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
throw new Error('onUsersUpdates not properly initialized')
},
},
ensureWidgetMetas: () => {
const {inboxLayout} = get()
Expand Down Expand Up @@ -453,7 +442,7 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
if (get().staticConfig) {
return
}
const {handshakeVersion, dispatch} = get().dispatch.defer.onGetDaemonState()
const {handshakeVersion, dispatch} = useDaemonState.getState()
const f = async () => {
const name = 'chat.loadStatic'
dispatch.wait(name, handshakeVersion, true)
Expand Down Expand Up @@ -748,7 +737,7 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
const usernames = update.CanonicalName.split(',')
const broken = (update.breaks.breaks || []).map(b => b.user.username)
const updates = usernames.map(name => ({info: {broken: broken.includes(name)}, name}))
get().dispatch.defer.onUsersUpdates(updates)
useUsersState.getState().dispatch.updates(updates)
break
}
case 'chat.1.chatUi.chatInboxUnverified':
Expand Down Expand Up @@ -969,7 +958,7 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
},
onGetInboxConvsUnboxed: action => {
// TODO not reactive
const infoMap = get().dispatch.defer.onGetUsersInfoMap()
const {infoMap} = useUsersState.getState()
const {convs} = action.payload.params
const inboxUIItems = JSON.parse(convs) as Array<T.RPCChat.InboxUIItem>
const metas: Array<T.Chat.ConversationMeta> = []
Expand All @@ -995,7 +984,7 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
})
})
if (Object.keys(usernameToFullname).length > 0) {
get().dispatch.defer.onUsersUpdates(
useUsersState.getState().dispatch.updates(
Object.keys(usernameToFullname).map(name => ({
info: {fullname: usernameToFullname[name]},
name,
Expand Down Expand Up @@ -1029,7 +1018,7 @@ export const useChatState = Z.createZustand<State>('chat', (set, get) => {
return map
}, {})

get().dispatch.defer.onUsersUpdates(
useUsersState.getState().dispatch.updates(
Object.keys(usernameToFullname).map(name => ({
info: {fullname: usernameToFullname[name]},
name,
Expand Down
3 changes: 0 additions & 3 deletions shared/stores/push.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ type Store = T.Immutable<{

export type State = Store & {
dispatch: {
defer: {
onGetDaemonHandshakeState?: () => T.Config.DaemonHandshakeState
}
checkPermissions: () => Promise<boolean>
clearPendingPushNotification: () => void
deleteToken: (version: number) => void
Expand Down
5 changes: 0 additions & 5 deletions shared/stores/push.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export const usePushState = Z.createZustand<State>('push', () => {
return Promise.resolve(false)
},
clearPendingPushNotification: () => {},
defer: {
onGetDaemonHandshakeState: () => {
return 'done'
},
},
deleteToken: () => {},
handlePush: () => {},
initialPermissionsCheck: () => {},
Expand Down
8 changes: 2 additions & 6 deletions shared/stores/push.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ignorePromise, neverThrowPromiseFunc, timeoutPromise} from '@/constants/
import {emitDeepLink} from '@/router-v2/linking'
import {useConfigState} from '@/stores/config'
import {useCurrentUserState} from '@/stores/current-user'
import {useDaemonState} from '@/stores/daemon'
import {useLogoutState} from '@/stores/logout'
import {useWaitingState} from '@/stores/waiting'
import * as Z from '@/util/zustand'
Expand Down Expand Up @@ -114,11 +115,6 @@ export const usePushState = Z.createZustand<State>('push', (set, get) => {
s.pendingPushNotification = undefined
})
},
defer: {
onGetDaemonHandshakeState: () => {
throw new Error('onGetDaemonHandshakeState not implemented')
},
},
deleteToken: version => {
const f = async () => {
const waitKey = 'push:deleteToken'
Expand Down Expand Up @@ -328,7 +324,7 @@ export const usePushState = Z.createZustand<State>('push', (set, get) => {
if (
p.show &&
useConfigState.getState().loggedIn &&
get().dispatch.defer.onGetDaemonHandshakeState?.() === 'done' &&
useDaemonState.getState().handshakeState === 'done' &&
!get().justSignedUp &&
!get().hasPermissions
) {
Expand Down
27 changes: 6 additions & 21 deletions shared/stores/team-building.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {validateEmailAddress} from '@/util/email-address'
import {registerDebugClear} from '@/util/debug'
import {searchWaitingKey} from '@/constants/strings'
import {navigateUp, getModalStack} from '@/constants/router'
import {useSettingsContactsState} from '@/stores/settings-contacts'
import {useUsersState} from '@/stores/users'
export {allServices, selfToUser} from '@/constants/team-building'
export {searchWaitingKey} from '@/constants/strings'

Expand Down Expand Up @@ -48,11 +50,7 @@ export type State = Store & {
onAddMembersWizardPushMembers: (members: Array<T.Teams.AddingMember>) => void
onFinishedTeamBuildingChat: (users: ReadonlySet<T.TB.User>) => void
onFinishedTeamBuildingCrypto: (users: ReadonlySet<T.TB.User>) => void
onGetSettingsContactsImportEnabled: () => boolean | undefined
onGetSettingsContactsUserCountryCode: () => string | undefined
onShowUserProfile: (username: string) => void
onUsersGetBlockState: (usernames: ReadonlyArray<string>) => void
onUsersUpdates: (infos: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => void
}
fetchUserRecs: () => void
finishTeamBuilding: () => void
Expand Down Expand Up @@ -307,21 +305,9 @@ const createSlice: Z.ImmerStateCreator<State> = (set, get) => {
onFinishedTeamBuildingCrypto: (_users: ReadonlySet<T.TB.User>) => {
throw new Error('onFinishedTeamBuildingCrypto not properly initialized')
},
onGetSettingsContactsImportEnabled: () => {
throw new Error('onGetSettingsContactsImportEnabled not properly initialized')
},
onGetSettingsContactsUserCountryCode: () => {
throw new Error('onGetSettingsContactsUserCountryCode not properly initialized')
},
onShowUserProfile: (_username: string) => {
throw new Error('onShowUserProfile not properly initialized')
},
onUsersGetBlockState: (_usernames: ReadonlyArray<string>) => {
throw new Error('onUsersGetBlockState not properly initialized')
},
onUsersUpdates: (_infos: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
throw new Error('onUsersUpdates not properly initialized')
},
},
fetchUserRecs: () => {
const includeContacts = get().namespace === 'chat'
Expand All @@ -337,8 +323,7 @@ const createSlice: Z.ImmerStateCreator<State> = (set, get) => {
const contactRes = _contactRes || []
const contacts = contactRes.map(contactToUser)
let suggestions = suggestionRes.map(interestingPersonToUser)
const expectingContacts =
get().dispatch.defer.onGetSettingsContactsImportEnabled() && includeContacts
const expectingContacts = useSettingsContactsState.getState().importEnabled && includeContacts
if (expectingContacts) {
suggestions = suggestions.slice(0, 10)
}
Expand Down Expand Up @@ -439,7 +424,7 @@ const createSlice: Z.ImmerStateCreator<State> = (set, get) => {
let users: typeof _users
if (selectedService === 'keybase') {
// If we are on Keybase tab, do additional search if query is phone/email.
const userRegion = get().dispatch.defer.onGetSettingsContactsUserCountryCode()
const {userCountryCode: userRegion} = useSettingsContactsState.getState()
users = await specialContactSearch(_users, searchQuery, userRegion)
} else {
users = _users
Expand All @@ -458,9 +443,9 @@ const createSlice: Z.ImmerStateCreator<State> = (set, get) => {
blocks.push(keybase)
}
}
get().dispatch.defer.onUsersUpdates(updates)
useUsersState.getState().dispatch.updates(updates)
if (blocks.length) {
get().dispatch.defer.onUsersGetBlockState(blocks)
useUsersState.getState().dispatch.getBlockState(blocks)
}
}
ignorePromise(f())
Expand Down
7 changes: 2 additions & 5 deletions shared/stores/teams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {storeRegistry} from '@/stores/store-registry'
import {useConfigState} from '@/stores/config'
import {type useChatState} from '@/stores/chat'
import {useCurrentUserState} from '@/stores/current-user'
import {useUsersState} from '@/stores/users'
import * as Util from '@/constants/teams'
import {getTab} from '@/constants/router'

Expand Down Expand Up @@ -834,7 +835,6 @@ export type State = Store & {
onChatPreviewConversation?: (
p: Parameters<ReturnType<typeof useChatState.getState>['dispatch']['previewConversation']>[0]
) => void
onUsersUpdates?: (updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => void
}
addMembersWizardPushMembers: (members: Array<T.Teams.AddingMember>) => void
addMembersWizardRemoveMember: (assertion: string) => void
Expand Down Expand Up @@ -1313,9 +1313,6 @@ export const useTeamsState = Z.createZustand<State>('teams', (set, get) => {
}) => {
throw new Error('onChatPreviewConversation not implemented')
},
onUsersUpdates: (_updates: ReadonlyArray<{name: string; info: Partial<T.Users.UserInfo>}>) => {
throw new Error('onUsersUpdates not implemented')
},
},
deleteChannelConfirmed: (teamID, conversationIDKey) => {
const f = async () => {
Expand Down Expand Up @@ -1494,7 +1491,7 @@ export const useTeamsState = Z.createZustand<State>('teams', (set, get) => {
set(s => {
s.teamIDToMembers.set(teamID, members)
})
get().dispatch.defer.onUsersUpdates?.(
useUsersState.getState().dispatch.updates(
[...members.values()].map(m => ({
info: {fullname: m.fullName},
name: m.username,
Expand Down
3 changes: 1 addition & 2 deletions shared/stores/tests/push.desktop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ afterEach(() => {
resetAllStores()
})

test('desktop push store reports the desktop handshake state and resettable defaults', async () => {
test('desktop push store reports resettable defaults', async () => {
const {dispatch} = usePushState.getState()

await expect(dispatch.checkPermissions()).resolves.toBe(false)
expect(dispatch.defer.onGetDaemonHandshakeState?.()).toBe('done')

dispatch.clearPendingPushNotification()
dispatch.deleteToken(1)
Expand Down
Loading