import router from '@/router' ; import { LocalAccount } from '~/src/types/localAccount' ; import { Module , MutationTree , ActionTree } from 'vuex' ; import { RootState } from '@/store' ; import { StreamingError } from '~src/errors/streamingError' ; import { MyWindow } from '~/src/types/global' ; const win = ( window as any ) as MyWindow ; export type GlobalHeaderState = { accounts : Array < LocalAccount > ; changing : boolean ; hide : boolean ; } ; const state = ( ) : GlobalHeaderState => ( { accounts : [ ] , changing : false , hide : false } ) ; export const MUTATION_TYPES = { UPDATE_ACCOUNTS : 'updateAccounts' , UPDATE_CHANGING : 'updateChanging' , CHANGE_HIDE : 'changeHide' } ; const mutations = { [ MUTATION_TYPES . UPDATE_ACCOUNTS ] : ( state , accounts ) => { state . accounts = accounts ; } , [ MUTATION_TYPES . UPDATE_CHANGING ] : ( state , value ) => { state . changing = value ; } , [ MUTATION_TYPES . CHANGE_HIDE ] : ( state , value ) => { state . hide = value ; } } ; const actions = { initLoad : async ( { dispatch } ) : Promise < Array < LocalAccount > > => { try { await dispatch ( 'removeShortcutEvents' ) ; await dispatch ( 'loadHide' ) ; dispatch ( 'watchShortcutEvents' ) ; } catch ( err ) { console . error ( err ) ; } const accounts = await dispatch ( 'listAccounts' ) ; router . beforeEach ( ( to , from , next ) => { if ( ! ( to . fullPath === '/' && from . name ) ) { return next ( ) ; } } ) ; return accounts ; } , startStreamings : async ( { dispatch } ) => { dispatch ( 'bindNotification' ) ; dispatch ( 'startUserStreamings' ) ; } , listAccounts : async ( { dispatch , commit } ) : Promise < Array < LocalAccount > > => { const accounts = await win . ipcRenderer . invoke ( 'list-accounts' ) ; commit ( MUTATION_TYPES . UPDATE_ACCOUNTS , accounts ) ; dispatch ( 'refreshAccounts' ) ; return accounts ; } , refreshAccounts : async ( { commit } ) : Promise < Array < LocalAccount > > => { const accounts = await win . ipcRenderer . invoke ( 'refresh-accounts' ) ; commit ( MUTATION_TYPES . UPDATE_ACCOUNTS , accounts ) ; return accounts ; } , watchShortcutEvents : ( { state , commit , rootState , rootGetters } ) => { win . ipcRenderer . on ( 'change-account' , ( _ , account ) => { if ( state . changing ) { return null ; } if ( ( rootState . route . params . id as string ) === account . _id ! ) { return null ; } if ( rootGetters [ 'TimelineSpace/Modals/modalOpened' ] ) { return null ; } commit ( MUTATION_TYPES . UPDATE_CHANGING , true ) ; router . push ( `/${ account . _id }/home` ) ; return true ; } ) ; } , removeShortcutEvents : async ( ) => { win . ipcRenderer . removeAllListeners ( 'change-account' ) ; return true ; } , loadHide : async ( { commit } ) : Promise < boolean > => { const hide = await win . ipcRenderer . invoke ( 'get-global-header' ) ; commit ( MUTATION_TYPES . CHANGE_HIDE , hide ) ; return hide ; } , switchHide : async ( { dispatch } , hide ) : Promise < boolean > => { await win . ipcRenderer . invoke ( 'change-global-header' , hide ) ; dispatch ( 'loadHide' ) ; return true ; } , startUserStreamings : ( { state } ) : Promise < { } > => { return new Promise ( ( resolve , reject ) => { win . ipcRenderer . once ( 'error-start-all-user-streamings' , ( _ , err ) => { reject ( err ) ; } ) ; win . ipcRenderer . send ( 'start-all-user-streamings' , state . accounts ) ; } ) ; } , stopUserStreamings : ( ) => { win . ipcRenderer . send ( 'stop-all-user-streamings' ) ; } , bindNotification : ( ) => { win . ipcRenderer . removeAllListeners ( 'open-notification-tab' ) ; win . ipcRenderer . on ( 'open-notification-tab' , ( _ , id ) => { router . push ( `/${ id }/home` ) ; setTimeout ( ( ) => router . push ( `/${ id }/notifications` ) , 500 ) ; } ) ; } } ; const GlobalHeader = { namespaced : true , state : state , mutations : mutations , actions : actions } ; export default GlobalHeader ;