/
PrintButton.jsx
90 lines (72 loc) · 2 KB
/
PrintButton.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { useState, useEffect, forwardRef } from 'react'
import PropTypes from 'prop-types'
import { useWebviewIntent } from 'cozy-intent'
import Button from '../../Buttons'
import IconButton from '../../IconButton'
import Icon from '../../Icon'
import ActionsItems, {
actionsItemsComponentPropTypes
} from '../../ActionsMenu/ActionsItems'
import { print } from '../../ActionsMenu/Actions/print'
import { makeActions } from '../../ActionsMenu/Actions/helpers'
const ActionComponent = forwardRef(({ action, variant, onClick }, ref) => {
const { label, icon } = action
if (variant === 'button') {
return (
<Button
ref={ref}
variant="secondary"
aria-label={label}
label={<Icon icon={icon} />}
onClick={onClick}
/>
)
}
return (
<IconButton
ref={ref}
className="u-white"
aria-label={label}
onClick={onClick}
>
<Icon icon={icon} />
</IconButton>
)
})
ActionComponent.displayName = 'ActionComponent'
ActionComponent.propTypes = {
...actionsItemsComponentPropTypes,
variant: PropTypes.oneOf(['default', 'button'])
}
const PrintButton = ({ file, variant }) => {
const [isPrintAvailable, setIsPrintAvailable] = useState(false)
const webviewIntent = useWebviewIntent()
const isPDFDoc = file.mime === 'application/pdf'
const showPrintButton = isPDFDoc && isPrintAvailable
useEffect(() => {
const init = async () => {
const isAvailable =
(await webviewIntent?.call('isAvailable', 'print')) ?? true
setIsPrintAvailable(isAvailable)
}
init()
}, [webviewIntent])
if (!showPrintButton) return null
const actions = makeActions([print])
return (
<ActionsItems
docs={[file]}
actions={actions}
component={ActionComponent}
variant={variant}
/>
)
}
PrintButton.propTypes = {
file: PropTypes.object.isRequired,
variant: PropTypes.oneOf(['default', 'button'])
}
PrintButton.defaultProps = {
variant: 'default'
}
export default PrintButton