Skip to content

Commit

Permalink
feat: change loading of accountsMachine
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight committed Dec 12, 2024
1 parent 108f46f commit f1af129
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 140 deletions.
4 changes: 2 additions & 2 deletions packages/app/src/systems/Account/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Services } from '~/store';

export function accountEvents(store: Store) {
return {
updateAccounts() {
store.send(Services.accounts, { type: 'REFRESH_ACCOUNTS' });
refreshAccounts(input?: { skipLoading?: boolean }) {
store.send(Services.accounts, { type: 'REFRESH_ACCOUNTS', input });
},
setCurrentAccount(account: Account) {
store.send(Services.accounts, {
Expand Down
4 changes: 1 addition & 3 deletions packages/app/src/systems/Account/hooks/useAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ const selectors = {
};

const listenerAccountFetcher = () => {
store.send(Services.accounts, {
type: 'REFRESH_ACCOUNTS',
});
store.refreshAccounts({ skipLoading: true });
};

export function useAccounts() {
Expand Down
153 changes: 87 additions & 66 deletions packages/app/src/systems/Account/machines/accountsMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MachineServices = {
};

export type AccountsMachineEvents =
| { type: 'REFRESH_ACCOUNTS'; input?: null }
| { type: 'REFRESH_ACCOUNTS'; input?: { skipLoading?: boolean } }
| { type: 'SET_CURRENT_ACCOUNT'; input: AccountInputs['setCurrentAccount'] }
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
| { type: 'LOGOUT'; input?: void }
Expand All @@ -41,6 +41,72 @@ export type AccountsMachineEvents =
input: AccountInputs['updateAccount'];
};

const fetchingAccountsState =
{
initial: 'fetchingAccounts',
states: {
fetchingAccounts: {
invoke: {
src: 'fetchAccounts',
onDone: [
{
target: 'recoveringWallet',
actions: ['assignAccounts', 'setIsLogged'],
cond: 'hasAccountsOrNeedsRecovery',
},
{
target: 'fetchingAccount',
actions: ['assignAccounts'],
},
],
onError: [
{
actions: 'assignError',
target: '#(machine).failed',
},
],
},
},
recoveringWallet: {
invoke: {
src: 'recoverWallet',
onDone: [
{
actions: 'assignError',
target: '#(machine).failed',
cond: FetchMachine.hasError,
},
{
target: 'fetchingAccount',
},
],
},
},
fetchingAccount: {
invoke: {
src: 'fetchAccount',
onDone: [
{
cond: FetchMachine.hasError,
actions: 'assignError',
target: '#(machine).failed',
},
{
target: '#(machine).idle',
actions: ['assignAccount'],
},
],
onError: [
{
actions: 'assignError',
target: '#(machine).failed',
},
],
},
},
},
};

export const accountsMachine = createMachine(
{
tsTypes: {} as import('./accountsMachine.typegen').Typegen0,
Expand Down Expand Up @@ -68,69 +134,17 @@ export const accountsMachine = createMachine(
* Update accounts every 5 seconds
*/
TIMEOUT: {
target: 'fetchingAccounts',
target: 'refreshingAccounts',
cond: 'isLoggedIn',
},
},
},
fetchingAccounts: {
invoke: {
src: 'fetchAccounts',
onDone: [
{
target: 'recoveringWallet',
actions: ['assignAccounts', 'setIsLogged'],
cond: 'hasAccountsOrNeedsRecovery',
},
{
target: 'fetchingAccount',
actions: ['assignAccounts'],
},
],
onError: [
{
actions: 'assignError',
target: 'failed',
},
],
},
},
recoveringWallet: {
invoke: {
src: 'recoverWallet',
onDone: [
{
actions: 'assignError',
target: 'failed',
cond: FetchMachine.hasError,
},
{
target: 'fetchingAccount',
},
],
},
tags: ['loading'],
...fetchingAccountsState,
},
fetchingAccount: {
invoke: {
src: 'fetchAccount',
onDone: [
{
cond: FetchMachine.hasError,
actions: 'assignError',
target: 'failed',
},
{
target: 'idle',
actions: ['assignAccount'],
},
],
onError: [
{
actions: 'assignError',
target: 'failed',
},
],
},
refreshingAccounts: {
...fetchingAccountsState,
},
settingCurrentAccount: {
invoke: {
Expand Down Expand Up @@ -181,9 +195,15 @@ export const accountsMachine = createMachine(
LOGOUT: {
target: 'loggingout',
},
REFRESH_ACCOUNTS: {
target: 'fetchingAccounts',
},
REFRESH_ACCOUNTS: [
{
cond: 'shouldSkipLoading',
target: 'refreshingAccounts',
},
{
target: 'fetchingAccounts',
}
],
},
},
{
Expand All @@ -210,11 +230,11 @@ export const accountsMachine = createMachine(
Storage.setItem(IS_LOGGED_KEY, true);
},
notifyUpdateAccounts: () => {
store.updateAccounts();
store.refreshAccounts();
},
redirectToHome: () => {
store.closeOverlay();
},
}
},
services: {
fetchAccounts: FetchMachine.create<
Expand Down Expand Up @@ -288,9 +308,6 @@ export const accountsMachine = createMachine(
isLoggedIn: () => {
return !!Storage.getItem(IS_LOGGED_KEY);
},
hasAccount: (ctx, ev) => {
return Boolean(ev?.data || ctx?.account);
},
hasAccountsOrNeedsRecovery: (ctx, ev) => {
const hasAccounts = Boolean(
(ev.data.accounts || ctx?.accounts || []).length
Expand All @@ -300,6 +317,10 @@ export const accountsMachine = createMachine(
);
return hasAccounts || needsRecovery;
},
shouldSkipLoading: (_, ev) => {
console.log(`asd ev`, ev);
return !!ev.input?.skipLoading;
},
},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const addAccountMachine = createMachine(
{
actions: {
notifyUpdateAccounts: () => {
store.updateAccounts();
store.refreshAccounts();
},
redirectToHome() {
store.closeOverlay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const editAccountMachine = createMachine(
account: (_, ev) => ev.data,
}),
notifyUpdateAccounts: () => {
store.updateAccounts();
store.refreshAccounts({ skipLoading: true });
},
redirectToList() {
store.openAccountList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const importAccountMachine = createMachine(
{
actions: {
notifyUpdateAccounts: () => {
store.updateAccounts();
store.refreshAccounts();
},
redirectToHome() {
store.closeOverlay();
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/systems/Account/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export class AccountService {
'Disaster on DB. Start recovering accounts / vaults / networks',
{
extra: dataToLog,
tags: { manual: true },
}
);
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const getTestNoDexieDbData = () =>
const request = store.getAll();
request.onsuccess = (event: Event) => {
const data = (event.target as IDBRequest).result;
console.log('Data retrieved successfully:', data);
resolve(data);
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/systems/Asset/machines/assetsMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const assetsMachine = createMachine(
toast.success('Asset added successfully');
},
notifyUpdateAccounts: () => {
store.updateAccounts();
store.refreshAccounts({ skipLoading: true });
},
},
services: {
Expand Down
5 changes: 4 additions & 1 deletion packages/app/src/systems/CRX/scripts/executeContentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export async function executeContentScript() {
}

function injectContentScript(tabId: number) {
const env = process.env?.NODE_ENV;
let env = undefined;
if (typeof process !== 'undefined') {
env = process?.env?.NODE_ENV;
}
chrome.scripting
.executeScript({
target: { tabId: tabId, allFrames: true },
Expand Down
Loading

0 comments on commit f1af129

Please sign in to comment.