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
35 changes: 0 additions & 35 deletions shared/constants/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {invalidPasswordErrorString} from './util'
import {navigateAppend} from '../router2/util'
import {storeRegistry} from '../store-registry'
import {useWhatsNewState} from '../whats-new'
import {getSelectedConversation} from '@/constants/chat2/common'

type Store = T.Immutable<{
forceSmallNav: boolean
Expand Down Expand Up @@ -244,15 +243,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {
set(s => {
s.gregorReachable = r
})
// Re-get info about our account if you log in/we're done handshaking/became reachable
if (r === T.RPCGen.Reachable.yes) {
// not in waiting state
if (storeRegistry.getState('daemon').handshakeWaiters.size === 0) {
ignorePromise(storeRegistry.getState('daemon').dispatch.loadDaemonBootstrapStatus())
}
}

storeRegistry.getState('teams').dispatch.eagerLoadTeams()
}

const setGregorPushState = (state: T.RPCGen.Gregor1.State) => {
Expand Down Expand Up @@ -299,13 +289,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {
set(s => {
s.appFocused = f
})

if (!isMobile || !f) {
return
}
const {dispatch} = storeRegistry.getConvoState(getSelectedConversation())
dispatch.loadMoreMessages({reason: 'foregrounding'})
dispatch.markThreadAsRead()
},
checkForUpdate: () => {
const f = async () => {
Expand Down Expand Up @@ -402,8 +385,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {
set(s => {
s.installerRanCount++
})

storeRegistry.getState('fs').dispatch.checkKbfsDaemonRpcStatus()
},
loadIsOnline: () => {
const f = async () => {
Expand Down Expand Up @@ -764,11 +745,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {

if (!changed) return

if (loggedIn) {
ignorePromise(storeRegistry.getState('daemon').dispatch.loadDaemonBootstrapStatus())
}
storeRegistry.getState('daemon').dispatch.loadDaemonAccounts()

const {loadOnStart} = get().dispatch
if (loggedIn) {
if (!causedByStartup) {
Expand All @@ -784,14 +760,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {
} else {
Z.resetAllStores()
}

if (loggedIn) {
storeRegistry.getState('fs').dispatch.checkKbfsDaemonRpcStatus()
}

if (!causedByStartup) {
ignorePromise(storeRegistry.getState('daemon').dispatch.refreshAccounts())
}
},
setLoginError: error => {
set(s => {
Expand All @@ -806,9 +774,6 @@ export const useConfigState = Z.createZustand<State>((set, get) => {
set(s => {
s.mobileAppState = nextAppState
})
if (nextAppState === 'background' && storeRegistry.getState('chat').inboxSearch) {
storeRegistry.getState('chat').dispatch.toggleInboxSearch(false)
}
},
setNotifySound: n => {
set(s => {
Expand Down
126 changes: 65 additions & 61 deletions shared/constants/platform-specific/index.desktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {initSharedSubscriptions} from './shared'
import {switchTab} from '../router2/util'
import {storeRegistry} from '../store-registry'
import {wrapErrors} from '@/util/debug'
import {getSelectedConversation} from '@/constants/chat2/common'

const {showMainWindow, activeChanged, requestWindowsStartService, dumpNodeLogger} = KB2.functions
const {quitApp, exitApp, setOpenAtLogin, ctlQuit, copyToClipboard} = KB2.functions
Expand Down Expand Up @@ -157,39 +158,49 @@ export const initPlatformListener = () => {
})

useConfigState.subscribe((s, old) => {
if (s.loggedIn === old.loggedIn) return
s.dispatch.osNetworkStatusChanged(navigator.onLine, 'notavailable', true)
})
if (s.loggedIn !== old.loggedIn) {
s.dispatch.osNetworkStatusChanged(navigator.onLine, 'notavailable', true)
}

useConfigState.subscribe((s, prev) => {
if (s.appFocused !== prev.appFocused) {
if (s.appFocused !== old.appFocused) {
maybePauseVideos()
if (!old.appFocused && s.appFocused) {
const {dispatch} = storeRegistry.getConvoState(getSelectedConversation())
dispatch.loadMoreMessages({reason: 'foregrounding'})
dispatch.markThreadAsRead()
}
}
})

useDaemonState.subscribe((s, old) => {
if (s.handshakeVersion === old.handshakeVersion) return
if (!isWindows) return

const f = async () => {
const waitKey = 'pipeCheckFail'
const version = s.handshakeVersion
const {wait} = s.dispatch
wait(waitKey, version, true)
try {
logger.info('Checking RPC ownership')
if (KB2.functions.winCheckRPCOwnership) {
await KB2.functions.winCheckRPCOwnership()
if (s.openAtLogin !== old.openAtLogin) {
const {openAtLogin} = s
const f = async () => {
if (__DEV__) {
console.log('onSetOpenAtLogin disabled for dev mode')
return
} else {
await T.RPCGen.configGuiSetValueRpcPromise({
path: ConfigConstants.openAtLoginKey,
value: {b: openAtLogin, isNull: false},
})
}
if (isLinux || isWindows) {
const enabled =
(await T.RPCGen.ctlGetOnLoginStartupRpcPromise()) === T.RPCGen.OnLoginStartupStatus.enabled
if (enabled !== openAtLogin) {
try {
await T.RPCGen.ctlSetOnLoginStartupRpcPromise({enabled: openAtLogin})
} catch (error_) {
const error = error_ as RPCError
logger.warn(`Error in sending ctlSetOnLoginStartup: ${error.message}`)
}
}
} else {
logger.info(`Login item settings changed! now ${openAtLogin ? 'on' : 'off'}`)
await setOpenAtLogin?.(openAtLogin)
}
wait(waitKey, version, false)
} catch (error_) {
// error will be logged in bootstrap check
getEngine().reset()
const error = error_ as RPCError
wait(waitKey, version, false, error.message || 'windows pipe owner fail', true)
}
ignorePromise(f())
}
ignorePromise(f())
})

const handleWindowFocusEvents = () => {
Expand All @@ -215,46 +226,39 @@ export const initPlatformListener = () => {
}
setupReachabilityWatcher()

useConfigState.subscribe((s, old) => {
if (s.openAtLogin === old.openAtLogin) return
const {openAtLogin} = s
const f = async () => {
if (__DEV__) {
console.log('onSetOpenAtLogin disabled for dev mode')
return
} else {
await T.RPCGen.configGuiSetValueRpcPromise({
path: ConfigConstants.openAtLoginKey,
value: {b: openAtLogin, isNull: false},
})
}
if (isLinux || isWindows) {
const enabled =
(await T.RPCGen.ctlGetOnLoginStartupRpcPromise()) === T.RPCGen.OnLoginStartupStatus.enabled
if (enabled !== openAtLogin) {
try {
await T.RPCGen.ctlSetOnLoginStartupRpcPromise({enabled: openAtLogin})
} catch (error_) {
const error = error_ as RPCError
logger.warn(`Error in sending ctlSetOnLoginStartup: ${error.message}`)
useDaemonState.subscribe((s, old) => {
if (s.handshakeVersion !== old.handshakeVersion) {
if (!isWindows) return

const f = async () => {
const waitKey = 'pipeCheckFail'
const version = s.handshakeVersion
const {wait} = s.dispatch
wait(waitKey, version, true)
try {
logger.info('Checking RPC ownership')
if (KB2.functions.winCheckRPCOwnership) {
await KB2.functions.winCheckRPCOwnership()
}
wait(waitKey, version, false)
} catch (error_) {
// error will be logged in bootstrap check
getEngine().reset()
const error = error_ as RPCError
wait(waitKey, version, false, error.message || 'windows pipe owner fail', true)
}
} else {
logger.info(`Login item settings changed! now ${openAtLogin ? 'on' : 'off'}`)
await setOpenAtLogin?.(openAtLogin)
}
ignorePromise(f())
}
ignorePromise(f())
})

useDaemonState.subscribe((s, old) => {
if (s.handshakeState === old.handshakeState || s.handshakeState !== 'done') return
useConfigState.getState().dispatch.setStartupDetails({
conversation: Chat.noConversationIDKey,
followUser: '',
link: '',
tab: undefined,
})
if (s.handshakeState !== old.handshakeState && s.handshakeState === 'done') {
useConfigState.getState().dispatch.setStartupDetails({
conversation: Chat.noConversationIDKey,
followUser: '',
link: '',
tab: undefined,
})
}
})

if (isLinux) {
Expand Down
112 changes: 72 additions & 40 deletions shared/constants/platform-specific/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,91 @@ import * as UsersUtil from '../users/util'

export const initSharedSubscriptions = () => {
useConfigState.subscribe((s, old) => {
if (s.loadOnStartPhase === old.loadOnStartPhase) return
if (s.loadOnStartPhase !== old.loadOnStartPhase) {
if (s.loadOnStartPhase === 'startupOrReloginButNotInARush') {
const getFollowerInfo = () => {
const {uid} = useCurrentUserState.getState()
logger.info(`getFollowerInfo: init; uid=${uid}`)
if (uid) {
// request follower info in the background
T.RPCGen.configRequestFollowingAndUnverifiedFollowersRpcPromise()
.then(() => {})
.catch(() => {})
}
}

if (s.loadOnStartPhase === 'startupOrReloginButNotInARush') {
const getFollowerInfo = () => {
const {uid} = useCurrentUserState.getState()
logger.info(`getFollowerInfo: init; uid=${uid}`)
if (uid) {
// request follower info in the background
T.RPCGen.configRequestFollowingAndUnverifiedFollowersRpcPromise()
.then(() => {})
.catch(() => {})
const updateServerConfig = async () => {
if (s.loggedIn) {
try {
await T.RPCGen.configUpdateLastLoggedInAndServerConfigRpcPromise({
serverConfigPath: serverConfigFileName,
})
} catch {}
}
}

const updateTeams = () => {
useTeamsState.getState().dispatch.getTeams()
useTeamsState.getState().dispatch.refreshTeamRoleMap()
}

const updateSettings = () => {
useSettingsContactsState.getState().dispatch.loadContactImportEnabled()
}
}

const updateServerConfig = async () => {
if (s.loggedIn) {
const updateChat = async () => {
// On login lets load the untrusted inbox. This helps make some flows easier
if (useCurrentUserState.getState().username) {
const {inboxRefresh} = useChatState.getState().dispatch
inboxRefresh('bootstrap')
}
try {
await T.RPCGen.configUpdateLastLoggedInAndServerConfigRpcPromise({
serverConfigPath: serverConfigFileName,
})
const rows = await T.RPCGen.configGuiGetValueRpcPromise({path: 'ui.inboxSmallRows'})
const ri = rows.i ?? -1
if (ri > 0) {
useChatState.getState().dispatch.setInboxNumSmallRows(ri, true)
}
} catch {}
}
}

const updateTeams = () => {
useTeamsState.getState().dispatch.getTeams()
useTeamsState.getState().dispatch.refreshTeamRoleMap()
getFollowerInfo()
ignorePromise(updateServerConfig())
updateTeams()
updateSettings()
ignorePromise(updateChat())
}
}

const updateSettings = () => {
useSettingsContactsState.getState().dispatch.loadContactImportEnabled()
if (s.gregorReachable !== old.gregorReachable) {
// Re-get info about our account if you log in/we're done handshaking/became reachable
if (s.gregorReachable === T.RPCGen.Reachable.yes) {
// not in waiting state
if (storeRegistry.getState('daemon').handshakeWaiters.size === 0) {
ignorePromise(storeRegistry.getState('daemon').dispatch.loadDaemonBootstrapStatus())
}
storeRegistry.getState('teams').dispatch.eagerLoadTeams()
}
}

const updateChat = async () => {
// On login lets load the untrusted inbox. This helps make some flows easier
if (useCurrentUserState.getState().username) {
const {inboxRefresh} = useChatState.getState().dispatch
inboxRefresh('bootstrap')
}
try {
const rows = await T.RPCGen.configGuiGetValueRpcPromise({path: 'ui.inboxSmallRows'})
const ri = rows.i ?? -1
if (ri > 0) {
useChatState.getState().dispatch.setInboxNumSmallRows(ri, true)
}
} catch {}
if (s.installerRanCount !== old.installerRanCount) {
storeRegistry.getState('fs').dispatch.checkKbfsDaemonRpcStatus()
}

if (s.loggedIn !== old.loggedIn) {
if (s.loggedIn) {
ignorePromise(storeRegistry.getState('daemon').dispatch.loadDaemonBootstrapStatus())
storeRegistry.getState('fs').dispatch.checkKbfsDaemonRpcStatus()
}
storeRegistry.getState('daemon').dispatch.loadDaemonAccounts()
if (!s.loggedInCausedbyStartup) {
ignorePromise(storeRegistry.getState('daemon').dispatch.refreshAccounts())
}
}

getFollowerInfo()
ignorePromise(updateServerConfig())
updateTeams()
updateSettings()
ignorePromise(updateChat())
if (s.mobileAppState !== old.mobileAppState) {
if (s.mobileAppState === 'background' && storeRegistry.getState('chat').inboxSearch) {
storeRegistry.getState('chat').dispatch.toggleInboxSearch(false)
}
}
})
}
Expand Down