Skip to content

Commit

Permalink
feat: Handle FlagshipUI for cozy-drive cases
Browse files Browse the repository at this point in the history
  • Loading branch information
acezard committed Mar 17, 2022
1 parent 4b16466 commit 4958d7c
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 31 deletions.
24 changes: 24 additions & 0 deletions react/ActionMenu/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Radio from '../Radio'
import { spacingProp } from '../Stack'
import { usePopper } from 'react-popper'
import createDepreciationLogger from '../helpers/createDepreciationLogger'
import { useSetFlagshipUI } from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
import { useTheme } from '@material-ui/core'

const ActionMenuWrapper = ({
inline,
Expand Down Expand Up @@ -101,6 +103,28 @@ const ActionMenu = ({
anchorElRef,
containerElRef
}) => {
const theme = useTheme()

useSetFlagshipUI(
{
bottomBackground: theme.palette.background.paper,
bottomTheme: 'dark',
topOverlay: getCssVariableValue('overlay'),
topBackground: theme.palette.background.paper,
topTheme: 'light'
},
{
bottomBackground:
theme.palette.background[
document.querySelector('[class*="sidebar"]') ? 'default' : 'paper'
],
bottomTheme: 'dark',
topOverlay: 'transparent',
topBackground: theme.palette.background.paper,
topTheme: 'dark'
}
)

if (placement)
logDepecratedPlacement(
'<ActionMenu placement /> is deprecated, use <ActionMenu popperOptions={{ placement }} /> instead'
Expand Down
23 changes: 0 additions & 23 deletions react/BarContextProvider/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,4 @@ describe('BarContextProvider', () => {

expect(queryByText(mockVoidWebviewService)).toBeInTheDocument()
})

it('should not try to provide a cozy-intent context if one is provided', () => {
// Set Web context
window.cozy.isFlagshipApp = false

const client = createMockClient({})
const mockStore = configureStore()
const store = mockStore(x => x)

const { queryByText } = render(
<Provider store={store}>
<CozyProvider client={client}>
<I18n lang="en" dictRequire={() => locales}>
<App webviewService={mockWebviewService}>
<IntentComponent />
</App>
</I18n>
</CozyProvider>
</Provider>
)

expect(queryByText(mockWebviewService.foo)).not.toBeInTheDocument()
})
})
43 changes: 41 additions & 2 deletions react/Dialog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { RemoveScroll } from 'react-remove-scroll'
import useBreakpoints from '../hooks/useBreakpoints'
import { useCozyTheme } from '../CozyTheme'
import themesStyles from '../../stylus/settings/palette.styl'
import { useSetFlagshipUI } from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
import { useTheme } from '@material-ui/core'

const Dialog = props => {
const { isMobile, isTablet } = useBreakpoints()
Expand All @@ -21,12 +23,49 @@ const Dialog = props => {
(props.open || props.opened) && shouldBlockScroll
? RemoveScroll
: React.Fragment
const cozyTheme = useCozyTheme()
const theme = useTheme()
const cozBar = document.querySelector('.coz-bar-wrapper')

const theme = useCozyTheme()
useSetFlagshipUI(
props.fullScreen
? {
bottomBackground: theme.palette.background.paper,
bottomTheme: 'dark',
topBackground: theme.palette.background.paper,
topTheme: 'dark'
}
: {
bottomBackground: theme.palette.background.default,
bottomTheme: 'light',
bottomOverlay: 'rgba(0, 0, 0, 0.5)',
topOverlay: 'rgba(0, 0, 0, 0.5)',
topBackground: theme.palette.background.paper,
topTheme: 'light'
},
{
bottomBackground:
theme.palette.background[
document.querySelector('[class*="sidebar"]') ? 'default' : 'paper'
],
bottomTheme: 'dark',
bottomOverlay: 'transparent',
topOverlay: 'transparent',
topBackground: getComputedStyle(cozBar).getPropertyValue(
'background-color'
),
topTheme: cozBar.classList.contains('coz-theme-primary')
? 'light'
: 'dark'
}
)

return (
<Wrapper>
<MUIDialog className={themesStyles[`CozyTheme--${theme}`]} {...props} />
<MUIDialog
className={themesStyles[`CozyTheme--${cozyTheme}`]}
{...props}
/>
</Wrapper>
)
}
Expand Down
35 changes: 34 additions & 1 deletion react/SelectionBar/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'

Expand All @@ -11,6 +11,8 @@ import useBreakpoints from '../hooks/useBreakpoints'
import styles from './styles.styl'

import CrossIcon from 'cozy-ui/transpiled/react/Icons/Cross'
import { useWebviewIntent } from 'cozy-intent'
import { useTheme } from '@material-ui/core'

/*
Expand Down Expand Up @@ -39,6 +41,37 @@ const SelectionBar = ({ actions, selected, hideSelectionBar }) => {
action.displayCondition === undefined || action.displayCondition(selected)
)
})
const webviewIntent = useWebviewIntent()
const theme = useTheme()

// This component is always rendered but hidden with CSS if there is no selection
// That is why we do not use useSetFlagship API here because that hook can not accept changing values
useEffect(() => {
if (!webviewIntent || !theme) return

selectedCount === 0 &&
webviewIntent &&
webviewIntent.call('setFlagshipUI', {
bottomBackground: theme.palette.background.default,
bottomTheme: 'dark'
})

selectedCount > 0 &&
webviewIntent &&
webviewIntent.call('setFlagshipUI', {
bottomBackground: theme.palette.grey[700],
bottomTheme: 'light'
})

return () =>
webviewIntent &&
theme &&
webviewIntent.call('setFlagshipUI', {
bottomBackground: theme.palette.background.default,
bottomTheme: 'dark'
})
}, [selectedCount, webviewIntent])

return (
<div
data-testid="selectionBar"
Expand Down
26 changes: 21 additions & 5 deletions react/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ import React from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import styles from './styles.styl'
import { useSetFlagshipUI } from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
import { useTheme } from '@material-ui/core'

const Sidebar = ({ children, className, ...restProps }) => (
<aside className={cx(styles['o-sidebar'], className)} {...restProps}>
{children}
</aside>
)
const Sidebar = ({ children, className, ...restProps }) => {
const theme = useTheme()

useSetFlagshipUI(
{
bottomBackground: theme.palette.background.default,
bottomTheme: 'dark'
},
{
bottomBackground: theme.palette.background.paper
}
)

return (
<aside className={cx(styles['o-sidebar'], className)} {...restProps}>
{children}
</aside>
)
}

Sidebar.propTypes = {
children: PropTypes.node,
Expand Down
17 changes: 17 additions & 0 deletions react/Viewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import ViewerByFile from './ViewerByFile'
import { isValidForPanel } from './helpers'
import PanelContent from './Panel/PanelContent'
import FooterContent from './Footer/FooterContent'
import { useSetFlagshipUI } from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
import { useTheme } from '@material-ui/core'

const KEY_CODE_LEFT = 37
const KEY_CODE_RIGHT = 39
Expand Down Expand Up @@ -149,6 +151,21 @@ const ViewerInformationsWrapper = ({
validForPanel,
toolbarRef
}) => {
const theme = useTheme()

useSetFlagshipUI(
{
bottomBackground: theme.palette.background.paper,
bottomTheme: 'dark'
},
{
bottomBackground:
theme.palette.background[
document.querySelector('[class*="sidebar"]') ? 'default' : 'paper'
]
}
)

return (
<>
{!disableFooter && (
Expand Down
14 changes: 14 additions & 0 deletions react/hooks/useSetFlagshipUi/useSetFlagshipUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from 'react'
import { useWebviewIntent } from 'cozy-intent'

export const useSetFlagshipUI = (onMount, onUnmount) => {
const webviewIntent = useWebviewIntent()

useEffect(() => {
webviewIntent && webviewIntent.call('setFlagshipUI', onMount)

return () => {
webviewIntent && webviewIntent.call('setFlagshipUI', onUnmount)
}
}, [webviewIntent])
}
95 changes: 95 additions & 0 deletions react/hooks/useSetFlagshipUi/useSetFlagshipUI.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { renderHook } from '@testing-library/react-hooks'
import { useSetFlagshipUI } from './useSetFlagshipUI'
import { useWebviewIntent } from 'cozy-intent'

jest.mock('cozy-intent')

const mockCall = jest.fn()
const mockCSSValue = '#fff'

beforeEach(() => {
useWebviewIntent.mockImplementation(() => ({ call: mockCall }))
})

afterEach(() => {
mockCall.mockClear()
})

it('should call webviewIntent with the correct params, once at mount and once at unmount', () => {
const { unmount } = renderHook(() =>
useSetFlagshipUI(
{
bottomBackground: mockCSSValue,
bottomOverlay: 'transparent',
bottomTheme: 'dark',
topBackground: mockCSSValue,
topOverlay: 'transparent',
topTheme: 'dark'
},
{
bottomBackground: mockCSSValue,
bottomOverlay: 'transparent',
bottomTheme: 'light',
topBackground: mockCSSValue,
topOverlay: 'transparent',
topTheme: 'light'
}
)
)

expect(mockCall).toHaveBeenNthCalledWith(1, 'setFlagshipUI', {
bottomBackground: mockCSSValue,
bottomOverlay: 'transparent',
bottomTheme: 'dark',
topBackground: mockCSSValue,
topOverlay: 'transparent',
topTheme: 'dark'
})

unmount()

expect(mockCall).toHaveBeenNthCalledWith(2, 'setFlagshipUI', {
bottomBackground: mockCSSValue,
bottomOverlay: 'transparent',
bottomTheme: 'light',
topBackground: mockCSSValue,
topOverlay: 'transparent',
topTheme: 'light'
})
})

it('should call webviewIntent with the correct params with few args, once at mount and once at unmount', () => {
const { unmount } = renderHook(() =>
useSetFlagshipUI(
{ bottomBackground: mockCSSValue },
{ bottomBackground: mockCSSValue }
)
)

expect(mockCall).toHaveBeenNthCalledWith(1, 'setFlagshipUI', {
bottomBackground: mockCSSValue
})

unmount()

expect(mockCall).toHaveBeenNthCalledWith(2, 'setFlagshipUI', {
bottomBackground: mockCSSValue
})
})

it('should call nothing with no webviewIntent and not throw', () => {
useWebviewIntent.mockImplementation(() => undefined)

const { unmount } = renderHook(() =>
useSetFlagshipUI(
{ bottomBackground: mockCSSValue },
{ bottomBackground: mockCSSValue }
)
)

expect(mockCall).not.toHaveBeenCalled()

unmount()

expect(mockCall).not.toHaveBeenCalled()
})

0 comments on commit 4958d7c

Please sign in to comment.