import React from 'react'; import { api, ShareWith, ShareType, RoomShare, Room, Permission } from '../Common/Api'; import './ShareWith.scss'; import ShareSelection from '../Common/ShareSelection'; import { PermissionsOptions } from '../Common/Translation'; type Props = { room: Room; permission: Permission.User | Permission.Moderator; shares: RoomShare[] | undefined; setShares: (shares: RoomShare[]) => void; } const ShareWith = ({ room, permission, shares: allShares, setShares }: Props): JSX.Element => { const isOwner = room.userId === OC.currentUser; const shares = (allShares && permission === Permission.Moderator) ? allShares.filter(share => share.permission !== Permission.User) : allShares; const sharedUserIds = shares ? shares.filter(share => share.shareType === ShareType.User).map(share => share.shareWith) : []; const sharedGroupIds = shares ? shares.filter(share => share.shareType === ShareType.Group).map(share => share.shareWith) : []; const sharedCircleIds = shares ? shares.filter(share => share.shareType === ShareType.Circle).map(share => share.shareWith) : []; async function addRoomShare(shareWith: string, shareType: number, displayName: string, permission: Permission) { const roomShare = await api.createRoomShare(room.id, shareType, shareWith, permission); roomShare.shareWithDisplayName = displayName; const newShares = allShares ? [...allShares] : []; const index = newShares.findIndex(share => share.id === roomShare.id); if (index > -1) { newShares[index] = roomShare; } else { newShares.push(roomShare); } setShares(newShares); } async function deleteRoomShare(id: number) { console.log('deleteRoomShare', id); await api.deleteRoomShare(id); setShares((allShares ? [...allShares] : []).filter(share => share.id !== id)); } async function setSharePermission(share: RoomShare, newPermission: number) { return addRoomShare(share.shareWith, share.shareType, share.shareWithDisplayName || share.shareWith, newPermission); } function getAvatarUrl(userId: string) { return OC.generateUrl('/avatar/' + encodeURIComponent(userId) + '/' + 32, { user: userId, size: 32, requesttoken: OC.requestToken, }); } function ucFirst(s: string) { return s && s[0].toUpperCase() + s.slice(1); } function selectPermission(value: Permission, onChange: (value: number) => void) { return (