import { detector } from 'megalodon' ; import { Module , MutationTree , ActionTree } from 'vuex' ; import { RootState } from '@/store' ; import { MyWindow } from '~/src/types/global' ; const win = ( window as any ) as MyWindow ; export type LoginState = { selectedInstance : string | null ; searching : boolean ; sns : 'mastodon' | 'pleroma' | 'misskey' ; } ; const state = ( ) : LoginState => ( { selectedInstance : null , searching : false , sns : 'mastodon' } ) ; export const MUTATION_TYPES = { CHANGE_INSTANCE : 'changeInstance' , CHANGE_SEARCHING : 'changeSearching' , CHANGE_SNS : 'changeSNS' } ; const mutations = { [ MUTATION_TYPES . CHANGE_INSTANCE ] : ( state , instance ) => { state . selectedInstance = instance ; } , [ MUTATION_TYPES . CHANGE_SEARCHING ] : ( state , searching ) => { state . searching = searching ; } , [ MUTATION_TYPES . CHANGE_SNS ] : ( state , sns ) => { state . sns = sns ; } } ; const actions = { fetchLogin : async ( { state } ) : Promise < string > => { const url = await win . ipcRenderer . invoke ( 'get-auth-url' , { instance : state . selectedInstance , sns : state . sns } ) ; return url ; } , pageBack : ( { commit } ) => { commit ( MUTATION_TYPES . CHANGE_INSTANCE , null ) ; } , confirmInstance : async ( { commit } , domain ) : Promise < boolean > => { commit ( MUTATION_TYPES . CHANGE_SEARCHING , true ) ; const cleanDomain = domain . trim ( ) ; try { const sns = await detector ( `https://${ cleanDomain }` ) ; commit ( MUTATION_TYPES . CHANGE_INSTANCE , cleanDomain ) ; commit ( MUTATION_TYPES . CHANGE_SNS , sns ) ; } finally { commit ( MUTATION_TYPES . CHANGE_SEARCHING , false ) ; } return true ; } } ; const Login = { namespaced : true , state : state , mutations : mutations , actions : actions } ; export default Login ;