-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSharingBar.jsx
153 lines (134 loc) · 3.86 KB
/
SharingBar.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { useEffect, useState } from 'react';
import ContextMenu from '../../react-chayns-contextmenu/component/ContextMenu';
import Icon from '../../react-chayns-icon/component/Icon';
import share from './sharingActions';
import './sharingBar.scss';
import {
getAvailableShareProviders,
getDefaultShareLink,
} from './sharingHelper';
import TextString from '../../react-chayns-textstring/component/TextString';
/**
* A context menu for sharing a link and some text on various platforms.
*/
const SharingBar = ({
link: linkProp,
linkText = '',
className,
stopPropagation = false,
style,
children,
}) => {
const [sharingProvider, setSharingProvider] = useState([]);
const [textStringsLoaded, setTextStringsLoaded] = useState(false);
useEffect(() => {
getAvailableShareProviders().then((provider) => {
setSharingProvider(provider.filter((item) => item.available));
});
if (!children) {
TextString.loadLibrary('ChaynsComponents').then(() => {
setTextStringsLoaded(true);
});
}
}, [children]);
const mobileShare = sharingProvider.find(
(app) => app.id === 10 || app.id === 11
);
let link = linkProp;
if (!link) {
link = getDefaultShareLink();
}
if (!children && !textStringsLoaded) {
return null;
}
const indicator = children || [
<Icon
key="icon"
icon="fal fa-share-alt"
className="sharing-bar__icon"
/>,
<TextString
key="textstring"
stringName="txt_chayns_components_sharingbar_share"
>
<span className="sharing-bar__text" />
</TextString>,
];
if (mobileShare) {
const onClick = (e) => {
share(mobileShare, link, linkText);
if (stopPropagation) e.stopPropagation();
};
return (
<div
className={classNames('sharing-bar', className)}
style={style}
onClick={onClick}
>
{indicator}
</div>
);
}
const sharingItems = [];
sharingProvider
.filter((item) => item.available)
.forEach((x) => {
sharingItems.push({
className: null,
onClick: (e) => {
if (stopPropagation && e && e.stopPropagation)
e.stopPropagation();
share(x, link, linkText);
},
stringName: x.name,
icon: x.icon,
});
});
return (
<div className={classNames('sharing-bar', className)} style={style}>
<ContextMenu
items={sharingItems}
childrenStyle={{ display: 'inline' }}
disableDialog={chayns.env.isDialog}
>
{indicator}
</ContextMenu>
</div>
);
};
SharingBar.propTypes = {
/**
* The link that should be shared.
*/
link: PropTypes.string,
/**
* A text that will be added in front of the shared link.
*/
linkText: PropTypes.string,
/**
* A classname string that will be applied to the container element.
*/
className: PropTypes.string,
/**
* Wether click events should be prevented from propagating to parent
* elements.
*/
stopPropagation: PropTypes.bool,
/**
* A React style object that will be applied to the container element.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* The children nodes of the `SharingBar`.
*/
children: PropTypes.node,
};
SharingBar.displayName = 'SharingBar';
export default SharingBar;