-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into e2e-isolate
- Loading branch information
Showing
15 changed files
with
281 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,70 @@ | ||
import React from 'react'; | ||
import { | ||
type BackgroundToAppMessage, | ||
BACKGROUND_TO_APP_CONNECTION_NAME as CONNECTION_NAME, | ||
MessageManager, | ||
type AppToBackgroundMessage, | ||
} from '@/shared/messages'; | ||
import { useBrowser } from '@/pages/shared/lib/context'; | ||
import { dispatch } from './store'; | ||
|
||
export { useBrowser, useTranslation } from '@/pages/shared/lib/context'; | ||
|
||
export function WaitForStateLoad({ children }: React.PropsWithChildren) { | ||
const message = useMessage(); | ||
const [isLoading, setIsLoading] = React.useState(true); | ||
|
||
React.useEffect(() => { | ||
async function get() { | ||
const response = await message.send('GET_DATA_APP'); | ||
|
||
if (response.success) { | ||
dispatch({ type: 'SET_DATA_APP', data: response.payload }); | ||
setIsLoading(false); | ||
} | ||
} | ||
|
||
get(); | ||
}, [message]); | ||
|
||
if (isLoading) { | ||
return <>Loading</>; | ||
} | ||
|
||
return <>{children}</>; | ||
} | ||
|
||
const MessageContext = React.createContext< | ||
MessageManager<AppToBackgroundMessage> | ||
>({} as MessageManager<AppToBackgroundMessage>); | ||
|
||
export const useMessage = () => React.useContext(MessageContext); | ||
|
||
export const MessageContextProvider = ({ | ||
children, | ||
}: React.PropsWithChildren) => { | ||
const browser = useBrowser(); | ||
const message = new MessageManager<AppToBackgroundMessage>({ browser }); | ||
|
||
React.useEffect(() => { | ||
const port = browser.runtime.connect({ name: CONNECTION_NAME }); | ||
port.onMessage.addListener((message: BackgroundToAppMessage) => { | ||
switch (message.type) { | ||
case 'SET_TRANSIENT_STATE': | ||
return dispatch(message); | ||
} | ||
}); | ||
port.onDisconnect.addListener(() => { | ||
// nothing to do | ||
}); | ||
return () => { | ||
port.disconnect(); | ||
}; | ||
}, [browser]); | ||
|
||
return ( | ||
<MessageContext.Provider value={message}> | ||
{children} | ||
</MessageContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { | ||
AppStore, | ||
DeepNonNullable, | ||
PopupTransientState, | ||
} from '@/shared/types'; | ||
import { proxy, useSnapshot } from 'valtio'; | ||
|
||
export type AppState = Required<DeepNonNullable<AppStore>>; | ||
|
||
export const store = proxy<AppState>({ | ||
transientState: {} as PopupTransientState, | ||
} as AppState); | ||
|
||
// easier access to the store via this hook | ||
export const useAppState = () => useSnapshot(store); | ||
|
||
export const dispatch = async ({ type, data }: Actions) => { | ||
switch (type) { | ||
case 'SET_DATA_APP': | ||
for (const key of Object.keys(data) as Array<keyof AppState>) { | ||
// @ts-expect-error we know TypeScript | ||
store[key] = data[key]; | ||
} | ||
break; | ||
case 'SET_TRANSIENT_STATE': | ||
store.transientState = data; | ||
break; | ||
default: | ||
throw new Error('Unknown action'); | ||
} | ||
}; | ||
|
||
type Actions = | ||
| { type: 'SET_TRANSIENT_STATE'; data: PopupTransientState } | ||
| { type: 'SET_DATA_APP'; data: Pick<Storage, 'connected' | 'publicKey'> }; |
Oops, something went wrong.