2020-08-27 17:21:34 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { api, ShareWith, ShareType, ShareWithOption } from '../Common/Api';
|
2020-08-29 10:33:59 +02:00
|
|
|
import './ShareSelection.scss';
|
2020-08-27 17:21:34 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
selectShare: (selection: ShareWithOption) => void;
|
|
|
|
shareType?: ShareType[];
|
|
|
|
excluded?: {
|
|
|
|
groupIds?: string[];
|
|
|
|
userIds?: string[];
|
2020-08-29 13:20:28 +02:00
|
|
|
circleIds?: string[];
|
2020-08-27 17:21:34 +02:00
|
|
|
};
|
|
|
|
placeholder?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ShareSelection: React.FC<Props> = (props) => {
|
|
|
|
const [search, setSearch] = useState<string>('');
|
|
|
|
const [hasFocus, setFocus] = useState<boolean>(false);
|
|
|
|
const [showSearchResults, setShowSearchResults] = useState<boolean>(false);
|
|
|
|
const [recommendations, setRecommendations] = useState<ShareWith>();
|
|
|
|
const [searchResults, setSearchResults] = useState<ShareWith>();
|
|
|
|
|
|
|
|
const shareType = props.shareType || [ShareType.User, ShareType.Group];
|
|
|
|
const excluded = {
|
|
|
|
userIds: props.excluded?.userIds || [],
|
|
|
|
groupIds: props.excluded?.groupIds || [],
|
2020-08-29 13:20:28 +02:00
|
|
|
circleIds: props.excluded?.circleIds || [],
|
2020-08-27 17:21:34 +02:00
|
|
|
};
|
2020-10-26 14:24:53 +01:00
|
|
|
const placeholder = props.placeholder || t('bbb', 'Name, group …');
|
2020-08-27 17:21:34 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSearchResults(undefined);
|
|
|
|
const searchQuery = search;
|
|
|
|
|
|
|
|
if (!searchQuery) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
api.searchShareWith(searchQuery, shareType).then(result => {
|
|
|
|
if (searchQuery === search) {
|
|
|
|
setSearchResults(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [search]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
api.getRecommendedShareWith(shareType).then(result => setRecommendations(result));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-09-24 09:53:47 +02:00
|
|
|
setShowSearchResults(hasFocus);
|
2020-08-27 17:21:34 +02:00
|
|
|
}, [hasFocus]);
|
|
|
|
|
2020-09-24 09:53:47 +02:00
|
|
|
function preventOnBlurEvent(ev: React.MouseEvent) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
async function selectShare(share: ShareWithOption) {
|
|
|
|
props.selectShare(share);
|
|
|
|
|
|
|
|
setSearch('');
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderSearchResults(options: ShareWith|undefined) {
|
|
|
|
const results = options ? [
|
2021-01-25 18:39:35 +01:00
|
|
|
...options.exact.users.filter(user => !excluded.userIds.includes(user.value.shareWith)),
|
|
|
|
...options.exact.groups.filter(group => !excluded.groupIds.includes(group.value.shareWith)),
|
|
|
|
...options.exact.circles.filter(circle => !excluded.circleIds.includes(circle.value.shareWith)),
|
2020-08-27 17:21:34 +02:00
|
|
|
...options.users.filter(user => !excluded.userIds.includes(user.value.shareWith)),
|
|
|
|
...options.groups.filter(group => !excluded.groupIds.includes(group.value.shareWith)),
|
2020-08-29 13:20:28 +02:00
|
|
|
...options.circles.filter(circle => !excluded.circleIds.includes(circle.value.shareWith)),
|
2020-08-27 17:21:34 +02:00
|
|
|
] : [];
|
|
|
|
|
|
|
|
const renderOption = (option: ShareWithOption) => {
|
2020-09-24 09:53:47 +02:00
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={option.value.shareWith}
|
|
|
|
className="suggestion"
|
|
|
|
onMouseDown={preventOnBlurEvent}
|
|
|
|
onClick={() => selectShare(option)}>
|
|
|
|
{option.label}{option.value.shareType === ShareType.Group ? ` (${t('bbb', 'Group')})` : ''}
|
|
|
|
</li>);
|
2020-08-27 17:21:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="bbb-selection">
|
|
|
|
{!options ?
|
|
|
|
<li><span className="icon icon-loading-small icon-visible"></span> {t('bbb', 'Searching')}</li> :
|
|
|
|
(
|
|
|
|
(results.length === 0 && search) ? <li>{t('bbb', 'No matches')}</li> : results.map(renderOption)
|
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="bbb-selection-container">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={search}
|
|
|
|
onChange={ev => setSearch(ev.currentTarget.value)}
|
|
|
|
onFocus={() => setFocus(true)}
|
|
|
|
onBlur={() => setFocus(false)}
|
|
|
|
placeholder={placeholder} />
|
|
|
|
{showSearchResults && renderSearchResults((search && searchResults) ? searchResults : ((recommendations && !search) ? recommendations : undefined))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ShareSelection;
|