Skip to content

Commit

Permalink
Remember the latest selected account id for next sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhankoral committed Sep 30, 2024
1 parent 6b4b922 commit 6cc5382
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/dui3/components/accounts/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const { accounts, defaultAccount, userSelectedAccount, isLoading } =
const selectAccount = (acc: DUIAccount) => {
userSelectedAccount.value = acc
accountStore.setUserSelectedAccount(acc) // saves the selected account id into DUI3Config.db for later use
showAccountsDialog.value = false
void trackEvent('DUI3 Action', { name: 'Account change' })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dui3/components/wizard/ProjectSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ watch(searchText, () => {
emit('search-text-update', searchText.value)
})
// TODO: this function is never triggered!! remove or evaluate
const selectAccount = (account: DUIAccount) => {
selectedAccountId.value = account.accountInfo.id
void trackEvent('DUI3 Action', { name: 'Account Select' }, account.accountInfo.id)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/dui3/lib/bindings/definitions/IConfigBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface IConfigBinding extends IBinding<IConfigBindingEvents> {
getIsDevMode: () => Promise<boolean>
getConfig: () => Promise<ConnectorConfig>
updateConfig: (config: ConnectorConfig) => void
setUserSelectedAccountId: (accountId: string) => void
getUserSelectedAccountId: () => Promise<AccountsConfig>
}

export interface IConfigBindingEvents extends IBindingSharedEvents {}
Expand All @@ -24,5 +26,9 @@ export type ConnectorConfig = {
darkTheme: boolean
}

export type AccountsConfig = {
userSelectedAccountId: string
}

// Useless, but will do for now :)
export class MockedConfigBinding extends BaseBridge {}
27 changes: 26 additions & 1 deletion packages/dui3/store/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const accountTestQuery = gql`

export const useAccountStore = defineStore('accountStore', () => {
const app = useNuxtApp()
const { $accountBinding } = app
const { $accountBinding, $configBinding } = app

const apolloClients = {} as Record<string, ApolloClient<unknown>>
const accounts = ref<DUIAccount[]>([])
Expand All @@ -47,6 +47,16 @@ export const useAccountStore = defineStore('accountStore', () => {
return userSelectedAccount.value || defaultAccount.value
})

const setUserSelectedAccount = (acc: DUIAccount) => {
userSelectedAccount.value = acc
try {
// NOTE: for the safe merge!
$configBinding.setUserSelectedAccountId(acc.accountInfo.id) // not need to await, fire and forget?
} catch (error) {
console.warn(error)
}
}

const testAccounts = async () => {
isLoading.value = true

Expand Down Expand Up @@ -185,13 +195,28 @@ export const useAccountStore = defineStore('accountStore', () => {
void testAccounts()
})

const init = async () => {
await refreshAccounts()
try {
const accountsConfig = await $configBinding.getUserSelectedAccountId()
userSelectedAccount.value = accounts.value.find(
(a) => a.accountInfo.id === accountsConfig.userSelectedAccountId
) as DUIAccount
} catch (error) {
console.warn(error)
}
}

init()

app.vueApp.provide(ApolloClients, apolloClients)
return {
isLoading,
accounts,
defaultAccount,
activeAccount,
userSelectedAccount,
setUserSelectedAccount,
accountByServerUrl,
isAccountExistsById,
isAccountExistsByServer,
Expand Down

0 comments on commit 6cc5382

Please sign in to comment.