import { app , autoUpdater , shell } from 'electron' ; import deepEqual from 'fast-deep-equal' ; import path from 'path' ; import { B_URL , ISSUES_URL } from '../../config/CONSTANTS' ; import { clickedApp , clickedUrlBar , pressedKey , startedPicker , } from '../../renderers/picker/state/actions' ; import { clickedHomepageButton , clickedOpenIssueButton , clickedRescanApps , clickedSetAsDefaultBrowserButton , clickedUpdateButton , clickedUpdateRestartButton , confirmedReset , startedPrefs , } from '../../renderers/prefs/state/actions' ; import type { Middleware } from '../../shared/state/model' ; import type { RootState } from '../../shared/state/reducer.root' ; import { database } from '../database' ; import { createTray , tray } from '../tray' ; import copyUrlToClipboard from '../utils/copy-url-to-clipboard' ; import { getInstalledAppIds } from '../utils/get-installed-app-ids' ; import { initUpdateChecker } from '../utils/init-update-checker' ; import { openApp } from '../utils/open-app' ; import { removeWindowsFromMemory } from '../utils/remove-windows-from-memory' ; import { createWindows , pickerWindow , prefsWindow , showPickerWindow , showPrefsWindow , } from '../windows' ; import { availableUpdate , clickedOpenPrefs , clickedRestorePicker , openedUrl , readiedApp , receivedRendererStartupSignal , } from './actions' ; /**
 * Asynchronously update perma store on state.storage changes
 */ function updateDatabase ( previousState , nextState ) { return new Promise ( ( resolve ) => { if ( ! deepEqual ( previousState . storage , nextState . storage ) ) { database . setAll ( nextState . storage ) ; } resolve ( ) ; } ) ; } /**
 * Actions that need to be dealt with by main.
 */ export const actionHubMiddleware = ( ) : Middleware => ( { dispatch , getState } ) => ( next ) => ( action ) => { const previousState = getState ( ) ; if ( readiedApp . match ( action ) && ! previousState . storage . isSetup ) { app . setAsDefaultProtocolClient ( 'http' ) ; app . setAsDefaultProtocolClient ( 'https' ) ; } const result = next ( action ) ; const nextState = getState ( ) ; updateDatabase ( previousState , nextState ) ; if ( readiedApp . match ( action ) ) { app . dock . hide ( ) ; createWindows ( ) ; createTray ( ) ; initUpdateChecker ( ) ; } else if ( startedPicker . match ( action ) || startedPrefs . match ( action ) ) { dispatch ( receivedRendererStartupSignal ( nextState ) ) ; } else if ( clickedUrlBar . match ( action ) ) { if ( copyUrlToClipboard ( nextState . data . url ) ) { pickerWindow ?. hide ( ) ; } } else if ( clickedSetAsDefaultBrowserButton . match ( action ) ) { app . setAsDefaultProtocolClient ( 'http' ) ; app . setAsDefaultProtocolClient ( 'https' ) ; } else if ( availableUpdate . match ( action ) ) { tray ?. setImage ( path . join ( __dirname , '/static/icon/tray_iconBlue.png' ) ) ; } else if ( clickedUpdateButton . match ( action ) ) { autoUpdater . checkForUpdates ( ) ; } else if ( clickedUpdateRestartButton . match ( action ) ) { autoUpdater . quitAndInstall ( ) ; removeWindowsFromMemory ( ) ; } else if ( clickedRescanApps . match ( action ) ) { getInstalledAppIds ( ) ; } else if ( clickedApp . match ( action ) ) { const { appId , isAlt , isShift } = action . payload ; if ( appId ) { openApp ( appId , nextState . data . url , isAlt , isShift ) ; pickerWindow ?. hide ( ) ; } } else if ( pressedKey . match ( action ) ) { if ( action . payload . physicalKey === 'Escape' ) { pickerWindow ?. hide ( ) ; } else if ( action . payload . metaKey && action . payload . virtualKey === 'c' ) { if ( copyUrlToClipboard ( nextState . data . url ) ) { pickerWindow ?. hide ( ) ; } } else { const foundApp = nextState . storage . apps . find ( ( storedApp ) => storedApp . hotCode === action . payload . physicalKey ) ; if ( ! action . payload . metaKey && foundApp ) { openApp ( foundApp . id , nextState . data . url , action . payload . altKey , action . payload . shiftKey ) ; pickerWindow ?. hide ( ) ; } } } else if ( openedUrl . match ( action ) ) { showPickerWindow ( ) ; if ( nextState . data . installedApps . length === 0 ) { getInstalledAppIds ( ) ; } } else if ( clickedRestorePicker . match ( action ) ) { showPickerWindow ( ) ; if ( nextState . data . installedApps . length === 0 ) { getInstalledAppIds ( ) ; } } else if ( clickedOpenPrefs . match ( action ) ) { showPrefsWindow ( ) ; if ( nextState . data . installedApps . length === 0 ) { getInstalledAppIds ( ) ; } } else if ( clickedHomepageButton . match ( action ) ) { shell . openExternal ( B_URL ) ; } else if ( clickedOpenIssueButton . match ( action ) ) { shell . openExternal ( ISSUES_URL ) ; } else if ( confirmedReset . match ( action ) ) { if ( process . env . NODE_ENV === 'development' ) { prefsWindow ?. hide ( ) ; } else { removeWindowsFromMemory ( ) ; app . relaunch ( ) ; app . exit ( ) ; } } return result ; } ;