forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update/rewrite use my jetpack connection hook to typescript (Automatt…
…ic#36300) * Update useMyJetpackConnection hook to typescript * changelog * Return empty object instead of null * Update other uses of null to object
- Loading branch information
1 parent
2f10669
commit b3b74e7
Showing
23 changed files
with
123 additions
and
82 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
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
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
38 changes: 29 additions & 9 deletions
38
projects/packages/my-jetpack/_inc/data/utils/get-my-jetpack-window-state.ts
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,17 +1,37 @@ | ||
type MyJetpackState = Window[ 'myJetpackInitialState' ]; | ||
type InitialState = Window[ 'myJetpackInitialState' ]; | ||
type RestState = Window[ 'myJetpackRest' ]; | ||
type ConnectionState = Window[ 'JP_CONNECTION_INITIAL_STATE' ]; | ||
|
||
type ReturnType< A > = A extends undefined | ||
? MyJetpackState | ||
: MyJetpackState[ A extends keyof MyJetpackState ? A : never ]; | ||
// Handles typing based on whether or not a key is provided. S = type of state, A = type of key | ||
type StateReturnType< S, A > = A extends undefined ? S : S[ A extends keyof S ? A : never ]; | ||
|
||
const getMyJetpackWindowState = < A extends keyof MyJetpackState | undefined = undefined >( | ||
export const getMyJetpackWindowInitialState = < | ||
A extends keyof InitialState | undefined = undefined, | ||
>( | ||
key?: A | ||
): ReturnType< A > => { | ||
): StateReturnType< InitialState, A > => { | ||
if ( ! key ) { | ||
return window?.myJetpackInitialState as ReturnType< A >; | ||
return window?.myJetpackInitialState as StateReturnType< InitialState, A >; | ||
} | ||
|
||
return ( window?.myJetpackInitialState?.[ key ] ?? null ) as ReturnType< A >; | ||
return ( window?.myJetpackInitialState?.[ key ] ?? {} ) as StateReturnType< InitialState, A >; | ||
}; | ||
|
||
export default getMyJetpackWindowState; | ||
export const getMyJetpackWindowRestState = () => { | ||
return ( window?.myJetpackRest ?? {} ) as RestState; | ||
}; | ||
|
||
export const getMyJetpackWindowConnectionState = < | ||
A extends keyof ConnectionState | undefined = undefined, | ||
>( | ||
key?: A | ||
): StateReturnType< ConnectionState, A > => { | ||
if ( ! key ) { | ||
return window?.JP_CONNECTION_INITIAL_STATE as StateReturnType< ConnectionState, A >; | ||
} | ||
|
||
return ( window?.JP_CONNECTION_INITIAL_STATE?.[ key ] ?? {} ) as StateReturnType< | ||
ConnectionState, | ||
A | ||
>; | ||
}; |
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
4 changes: 2 additions & 2 deletions
4
projects/packages/my-jetpack/_inc/hooks/use-analytics/index.js
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
Oops, something went wrong.