Refactor ElectronContext for improved Electron detection and streamlined URL handling
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
- Simplified the `isElectron` function to directly return the result of `isElectrobunBridgeReady` or `isElectrobunDesktop`. - Removed redundant checks and consolidated URL opening logic to enhance clarity and reduce code duplication. - Updated event handling for Electron-specific features to utilize the desktopBridge API, improving maintainability and performance.
This commit is contained in:
parent
84a6a86bf3
commit
35ce3c39af
@ -6,32 +6,9 @@ import desktopBridge, {
|
||||
isElectrobunDesktop
|
||||
} from '../../../electrobun-bridge.js'
|
||||
|
||||
const electron = window.require ? window.require('electron') : null
|
||||
const ipcRenderer = electron ? electron.ipcRenderer : null
|
||||
|
||||
// eslint-disable-next-line react-refresh/only-export-components
|
||||
export function isElectron() {
|
||||
if (isElectrobunDesktop()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (
|
||||
typeof window !== 'undefined' &&
|
||||
window.process &&
|
||||
window.process.type === 'renderer'
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (
|
||||
typeof navigator === 'object' &&
|
||||
typeof navigator.userAgent === 'string' &&
|
||||
navigator.userAgent.indexOf('Electron') >= 0
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
return isElectrobunBridgeReady() || isElectrobunDesktop()
|
||||
}
|
||||
|
||||
const ElectronContext = createContext()
|
||||
@ -56,7 +33,6 @@ const ElectronProvider = ({ children }) => {
|
||||
const [isMaximized, setIsMaximized] = useState(false)
|
||||
const [isFullScreen, setIsFullScreen] = useState(false)
|
||||
const [electronAvailable] = useState(isElectron())
|
||||
const useElectrobun = isElectrobunBridgeReady() || isElectrobunDesktop()
|
||||
const navigate = useNavigate()
|
||||
const lastNavigationAtRef = useRef(0)
|
||||
|
||||
@ -85,74 +61,34 @@ const ElectronProvider = ({ children }) => {
|
||||
}, [])
|
||||
|
||||
const openExternalUrl = (url) => {
|
||||
if (useElectrobun) {
|
||||
void desktopBridge.openExternalUrl(url).catch((error) => {
|
||||
console.warn('[ElectronContext] Failed to open external url:', error)
|
||||
})
|
||||
return true
|
||||
}
|
||||
if (electronAvailable && ipcRenderer) {
|
||||
ipcRenderer.invoke('open-external-url', url)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
if (!electronAvailable) return false
|
||||
void desktopBridge.openExternalUrl(url).catch((error) => {
|
||||
console.warn('[ElectronContext] Failed to open external url:', error)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
const openInternalUrl = (url) => {
|
||||
if (useElectrobun) {
|
||||
void desktopBridge.openInternalUrl(url).catch((error) => {
|
||||
console.warn('[ElectronContext] Failed to open internal url:', error)
|
||||
})
|
||||
return true
|
||||
}
|
||||
if (electronAvailable && ipcRenderer) {
|
||||
ipcRenderer.invoke('open-internal-url', url)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
if (!electronAvailable) return false
|
||||
void desktopBridge.openInternalUrl(url).catch((error) => {
|
||||
console.warn('[ElectronContext] Failed to open internal url:', error)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!electronAvailable) return
|
||||
|
||||
if (useElectrobun) {
|
||||
desktopBridge.getOsInfo().then((info) => {
|
||||
if (info?.platform) {
|
||||
setPlatform(info.platform)
|
||||
if (info.platform === 'darwin') {
|
||||
document.documentElement.classList.add('macos-vibrancy')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
desktopBridge.getWindowState().then(applyWindowState)
|
||||
|
||||
const unsubWindowState = desktopBridge.onMessage(
|
||||
'windowState',
|
||||
applyWindowState
|
||||
)
|
||||
const unsubNavigate = desktopBridge.onMessage('navigate', (url) => {
|
||||
if (url.toLowerCase() == '/favicon.ico') {
|
||||
return
|
||||
}
|
||||
console.log('[ElectronContext] Navigating to:', url)
|
||||
navigate(url)
|
||||
})
|
||||
const unsubNavigationGesture = desktopBridge.onMessage(
|
||||
'navigationGesture',
|
||||
navigateHistory
|
||||
)
|
||||
|
||||
return () => {
|
||||
unsubWindowState()
|
||||
unsubNavigate()
|
||||
unsubNavigationGesture()
|
||||
}
|
||||
document.body.classList.add('electron-body')
|
||||
return () => {
|
||||
document.body.classList.remove('electron-body')
|
||||
}
|
||||
}, [electronAvailable])
|
||||
|
||||
if (!ipcRenderer) return
|
||||
useEffect(() => {
|
||||
if (!electronAvailable) return
|
||||
|
||||
ipcRenderer.invoke('os-info').then((info) => {
|
||||
desktopBridge.getOsInfo().then((info) => {
|
||||
if (info?.platform) {
|
||||
setPlatform(info.platform)
|
||||
if (info.platform === 'darwin') {
|
||||
@ -161,39 +97,30 @@ const ElectronProvider = ({ children }) => {
|
||||
}
|
||||
})
|
||||
|
||||
ipcRenderer.invoke('window-state').then(applyWindowState)
|
||||
desktopBridge.getWindowState().then(applyWindowState)
|
||||
|
||||
const windowStateHandler = (_event, state) => {
|
||||
applyWindowState(state)
|
||||
}
|
||||
ipcRenderer.on('window-state', windowStateHandler)
|
||||
|
||||
const navigateHandler = (_event, url) => {
|
||||
const unsubWindowState = desktopBridge.onMessage(
|
||||
'windowState',
|
||||
applyWindowState
|
||||
)
|
||||
const unsubNavigate = desktopBridge.onMessage('navigate', (url) => {
|
||||
if (url.toLowerCase() == '/favicon.ico') {
|
||||
return
|
||||
}
|
||||
console.log('[ElectronContext] Navigating to:', url)
|
||||
navigate(url)
|
||||
}
|
||||
ipcRenderer.on('navigate', navigateHandler)
|
||||
|
||||
const navigationGestureHandler = (_event, direction) => {
|
||||
navigateHistory(direction)
|
||||
}
|
||||
ipcRenderer.on('navigation-gesture', navigationGestureHandler)
|
||||
})
|
||||
const unsubNavigationGesture = desktopBridge.onMessage(
|
||||
'navigationGesture',
|
||||
navigateHistory
|
||||
)
|
||||
|
||||
return () => {
|
||||
ipcRenderer.removeListener('navigate', navigateHandler)
|
||||
ipcRenderer.removeListener('navigation-gesture', navigationGestureHandler)
|
||||
ipcRenderer.removeListener('window-state', windowStateHandler)
|
||||
unsubWindowState()
|
||||
unsubNavigate()
|
||||
unsubNavigationGesture()
|
||||
}
|
||||
}, [
|
||||
applyWindowState,
|
||||
electronAvailable,
|
||||
navigate,
|
||||
navigateHistory,
|
||||
useElectrobun
|
||||
])
|
||||
}, [applyWindowState, electronAvailable, navigate, navigateHistory])
|
||||
|
||||
useEffect(() => {
|
||||
if (!electronAvailable || platform !== 'darwin') return
|
||||
@ -227,103 +154,74 @@ const ElectronProvider = ({ children }) => {
|
||||
}, [electronAvailable, navigateHistory, platform])
|
||||
|
||||
const handleWindowControl = (action) => {
|
||||
if (useElectrobun) {
|
||||
void desktopBridge.windowControl(action)
|
||||
return
|
||||
}
|
||||
if (electronAvailable && ipcRenderer) {
|
||||
ipcRenderer.send('window-control', action)
|
||||
}
|
||||
if (!electronAvailable) return
|
||||
void desktopBridge.windowControl(action)
|
||||
}
|
||||
|
||||
const getAuthSession = async () => {
|
||||
if (!electronAvailable) return null
|
||||
if (useElectrobun) return await desktopBridge.getAuthSession()
|
||||
if (!ipcRenderer) return null
|
||||
return await ipcRenderer.invoke('auth-session-get')
|
||||
return await desktopBridge.getAuthSession()
|
||||
}
|
||||
|
||||
const setAuthSession = async (session) => {
|
||||
if (!electronAvailable) return false
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.setAuthSession(session)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('auth-session-set', session)
|
||||
const result = await desktopBridge.setAuthSession(session)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
|
||||
const clearAuthSession = async () => {
|
||||
if (!electronAvailable) return false
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.clearAuthSession()
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('auth-session-clear')
|
||||
const result = await desktopBridge.clearAuthSession()
|
||||
return result?.ok ?? false
|
||||
}
|
||||
|
||||
const getAppSettings = useCallback(async () => {
|
||||
if (!electronAvailable) return {}
|
||||
if (useElectrobun) return await desktopBridge.getAppSettings()
|
||||
if (!ipcRenderer) return {}
|
||||
return await ipcRenderer.invoke('app-settings-get')
|
||||
}, [electronAvailable, useElectrobun])
|
||||
return await desktopBridge.getAppSettings()
|
||||
}, [electronAvailable])
|
||||
|
||||
const setAppSettings = useCallback(
|
||||
async (settings) => {
|
||||
if (!electronAvailable) return false
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.setAppSettings(settings)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('app-settings-set', settings)
|
||||
const result = await desktopBridge.setAppSettings(settings)
|
||||
return result?.ok ?? false
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const startAppUpdate = useCallback(
|
||||
async (update) => {
|
||||
if (!electronAvailable) return false
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.startAppUpdate(update)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('app-update-start', update)
|
||||
const result = await desktopBridge.startAppUpdate(update)
|
||||
return result?.ok ?? false
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const checkAppUpdateResult = useCallback(async () => {
|
||||
if (!electronAvailable || !useElectrobun) return null
|
||||
if (!electronAvailable) return null
|
||||
return await desktopBridge.checkAppUpdateResult()
|
||||
}, [electronAvailable, useElectrobun])
|
||||
}, [electronAvailable])
|
||||
|
||||
const checkDuplicateInstallations = useCallback(async () => {
|
||||
if (!electronAvailable || !useElectrobun) return null
|
||||
if (!electronAvailable) return null
|
||||
return await desktopBridge.checkDuplicateInstallations()
|
||||
}, [electronAvailable, useElectrobun])
|
||||
}, [electronAvailable])
|
||||
|
||||
const removeDuplicateInstallations = useCallback(async () => {
|
||||
if (!electronAvailable || !useElectrobun) return false
|
||||
if (!electronAvailable) return false
|
||||
const result = await desktopBridge.removeDuplicateInstallations()
|
||||
return result?.ok ?? false
|
||||
}, [electronAvailable, useElectrobun])
|
||||
}, [electronAvailable])
|
||||
|
||||
const onDuplicateInstallationsRemoved = useCallback(
|
||||
(handler) => {
|
||||
if (
|
||||
!electronAvailable ||
|
||||
!useElectrobun ||
|
||||
typeof handler !== 'function'
|
||||
) {
|
||||
if (!electronAvailable || typeof handler !== 'function') {
|
||||
return () => {}
|
||||
}
|
||||
return desktopBridge.onMessage('duplicateInstallationsRemoved', handler)
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const onAppUpdateProgress = useCallback(
|
||||
@ -331,24 +229,9 @@ const ElectronProvider = ({ children }) => {
|
||||
if (!electronAvailable || typeof handler !== 'function') {
|
||||
return () => {}
|
||||
}
|
||||
|
||||
if (useElectrobun) {
|
||||
return desktopBridge.onMessage('appUpdateProgress', handler)
|
||||
}
|
||||
|
||||
if (!ipcRenderer) return () => {}
|
||||
|
||||
const progressHandler = (_event, progress) => {
|
||||
handler(progress)
|
||||
}
|
||||
|
||||
ipcRenderer.on('app-update-progress', progressHandler)
|
||||
|
||||
return () => {
|
||||
ipcRenderer.removeListener('app-update-progress', progressHandler)
|
||||
}
|
||||
return desktopBridge.onMessage('appUpdateProgress', handler)
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const onCheckForUpdatesRequest = useCallback(
|
||||
@ -356,24 +239,9 @@ const ElectronProvider = ({ children }) => {
|
||||
if (!electronAvailable || typeof handler !== 'function') {
|
||||
return () => {}
|
||||
}
|
||||
|
||||
if (useElectrobun) {
|
||||
return desktopBridge.onMessage('checkForUpdates', handler)
|
||||
}
|
||||
|
||||
if (!ipcRenderer) return () => {}
|
||||
|
||||
const checkHandler = () => {
|
||||
handler()
|
||||
}
|
||||
|
||||
ipcRenderer.on('check-for-updates', checkHandler)
|
||||
|
||||
return () => {
|
||||
ipcRenderer.removeListener('check-for-updates', checkHandler)
|
||||
}
|
||||
return desktopBridge.onMessage('checkForUpdates', handler)
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const getToken = async () => {
|
||||
@ -389,12 +257,8 @@ const ElectronProvider = ({ children }) => {
|
||||
const resizeSpotlightWindow = async (height) => {
|
||||
if (!electronAvailable) return false
|
||||
try {
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.resizeSpotlightWindow(height)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('spotlight-window-resize', height)
|
||||
const result = await desktopBridge.resizeSpotlightWindow(height)
|
||||
return result?.ok ?? false
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
'[ElectronContext] Failed to resize spotlight window:',
|
||||
@ -407,31 +271,22 @@ const ElectronProvider = ({ children }) => {
|
||||
const setSidebarViewMenu = useCallback(
|
||||
async (sections) => {
|
||||
if (!electronAvailable) return false
|
||||
if (useElectrobun) {
|
||||
const result = await desktopBridge.setSidebarViewMenu(sections)
|
||||
return result?.ok ?? false
|
||||
}
|
||||
if (!ipcRenderer) return false
|
||||
return await ipcRenderer.invoke('set-sidebar-view-menu', sections)
|
||||
const result = await desktopBridge.setSidebarViewMenu(sections)
|
||||
return result?.ok ?? false
|
||||
},
|
||||
[electronAvailable, useElectrobun]
|
||||
[electronAvailable]
|
||||
)
|
||||
|
||||
const getElectronVersion = useCallback(async () => {
|
||||
if (!electronAvailable) return null
|
||||
if (useElectrobun) return await desktopBridge.getAppVersion()
|
||||
if (!ipcRenderer) return null
|
||||
return await ipcRenderer.invoke('electron-version')
|
||||
}, [electronAvailable, useElectrobun])
|
||||
return await desktopBridge.getAppVersion()
|
||||
}, [electronAvailable])
|
||||
|
||||
const getAppEngine = useCallback(async () => {
|
||||
if (!electronAvailable) return 'native'
|
||||
if (useElectrobun) {
|
||||
const engine = await desktopBridge.getAppEngine()
|
||||
return engine === 'chromium' ? 'chromium' : 'native'
|
||||
}
|
||||
return 'native'
|
||||
}, [electronAvailable, useElectrobun])
|
||||
const engine = await desktopBridge.getAppEngine()
|
||||
return engine === 'chromium' ? 'chromium' : 'native'
|
||||
}, [electronAvailable])
|
||||
|
||||
return (
|
||||
<ElectronContext.Provider
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user