Skip to content

Commit

Permalink
feat: Refactor setFlagshipUI feature
Browse files Browse the repository at this point in the history
- Better CSS value fetching
- Typescript instead of JavaScript
  • Loading branch information
acezard committed Mar 19, 2022
1 parent 2dceef3 commit d6dfd0b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion react/ActionMenu/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const ActionMenu = ({
containerElRef
}) => {
const theme = useTheme()
const sidebar = document.querySelector('[class*="sidebar"]')
const sidebar = document.getElementById('sidebar')

useSetFlagshipUI(
{
Expand Down
7 changes: 5 additions & 2 deletions react/Dialog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const Dialog = props => {
const theme = useTheme()
const cozBar = document.querySelector('.coz-bar-wrapper')
const sidebar = document.getElementById('sidebar')
const backdrop = document.querySelector('.MuiBackdrop-root')
const backdropOverlay =
backdrop && getComputedStyle(backdrop).getPropertyValue('background-color')

useSetFlagshipUI(
props.fullScreen
Expand All @@ -39,8 +42,8 @@ const Dialog = props => {
: {
bottomBackground: theme.palette.background.default,
bottomTheme: 'light',
bottomOverlay: 'rgba(0, 0, 0, 0.5)',
topOverlay: 'rgba(0, 0, 0, 0.5)',
bottomOverlay: backdropOverlay,
topOverlay: backdropOverlay,
topBackground: theme.palette.background.paper,
topTheme: 'light'
},
Expand Down
25 changes: 0 additions & 25 deletions react/hooks/useSetFlagshipUi/useSetFlagshipUI.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { renderHook } from '@testing-library/react-hooks'
import { useSetFlagshipUI } from './useSetFlagshipUI'
import { useWebviewIntent } from 'cozy-intent'
Expand Down
41 changes: 41 additions & 0 deletions react/hooks/useSetFlagshipUi/useSetFlagshipUI.ts
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
}

0 comments on commit d6dfd0b

Please sign in to comment.