-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor setFlagshipUI feature
- Better CSS value fetching - Typescript instead of JavaScript
- Loading branch information
Showing
5 changed files
with
48 additions
and
28 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 was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...useSetFlagshipUi/useSetFlagshipUI.spec.js → ...useSetFlagshipUi/useSetFlagshipUI.spec.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
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,41 @@ | ||
import { useEffect } from 'react' | ||
import pickBy from 'lodash/pickBy' | ||
import identity from 'lodash/identity' | ||
import isEmpty from 'lodash/isEmpty' | ||
import isObject from 'lodash/isObject' | ||
|
||
import { useWebviewIntent } from 'cozy-intent' | ||
import { FlagshipUI } from 'cozy-intent/dist/api/models/applications' | ||
import { WebviewService } from 'cozy-intent/dist/api/services/WebviewService' | ||
|
||
const parseArg = ( | ||
webviewIntent?: WebviewService | void, | ||
arg?: FlagshipUI | ||
): void => { | ||
if (!webviewIntent) return | ||
|
||
const sanitized = isObject(arg) && pickBy(arg, identity) | ||
const validUI = !isEmpty(sanitized) && sanitized | ||
|
||
validUI && webviewIntent.call('setFlagshipUI', validUI) | ||
} | ||
|
||
export const useSetFlagshipUI = ( | ||
onMount: FlagshipUI, | ||
onUnmount?: FlagshipUI | ||
): void => { | ||
const webviewIntent = useWebviewIntent() | ||
|
||
useEffect(() => { | ||
parseArg(webviewIntent, onMount) | ||
|
||
return () => parseArg(webviewIntent, onUnmount) | ||
|
||
/** | ||
* We don't want to listen to onMount/onUnmount arguments | ||
* It will create far too many unwanted calls | ||
* We only care about webviewIntent presence, | ||
* which should be undefined on first call (not guaranteed) | ||
*/ | ||
}, [webviewIntent]) // eslint-disable-line react-hooks/exhaustive-deps | ||
} |