generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add share modal #674
Closed
Yashsharma1911
wants to merge
16
commits into
layer5io:master
from
Yashsharma1911:yash/addShareModal
Closed
Add share modal #674
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
26f1ef7
Added User search field component
Yashsharma1911 3a4a43c
Fix lint issue
Yashsharma1911 ccd9f63
feat: added chain icon
Yashsharma1911 23841cc
feat: fix PersonIcon
Yashsharma1911 ea95bd4
Fix userSearchField export typo
Yashsharma1911 d1e6a68
feat: added share modal
Yashsharma1911 ff851a7
feat: fix lint issues
Yashsharma1911 f835a8a
fix: fix typo
Yashsharma1911 41446e1
fix: fix user interface type issues
Yashsharma1911 5b9dbad
feat: add lockIcon and PublicIcon
Yashsharma1911 e1ed8b2
feat: Added people with access
Yashsharma1911 cdf86c2
fix: fix lint issues
Yashsharma1911 310a65e
fix: fix share modal export
Yashsharma1911 b19a411
fix: fix share modal export and lint errors
Yashsharma1911 f77d836
fix: fix styles
Yashsharma1911 a8561ac
fix: remove consoles
Yashsharma1911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
import { SelectChangeEvent } from '@mui/material'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { | ||
Avatar, | ||
IconButton, | ||
List, | ||
ListItem, | ||
ListItemAvatar, | ||
ListItemSecondaryAction, | ||
ListItemText, | ||
MenuItem, | ||
Typography | ||
} from '../../base'; | ||
import { ChainIcon, DeleteIcon, LockIcon, PublicIcon } from '../../icons'; | ||
import { useTheme } from '../../theme'; | ||
import { BLACK, WHITE } from '../../theme/colors'; | ||
import { Modal, ModalBody, ModalButtonPrimary, ModalButtonSecondary, ModalFooter } from '../Modal'; | ||
import { UserSearchField } from '../UserSearchField'; | ||
import { | ||
CustomDialogContentText, | ||
CustomListItemText, | ||
CustomSelect, | ||
FormControlWrapper, | ||
IconButtonWrapper, | ||
ListWrapper, | ||
VisibilityIconWrapper | ||
} from './style'; | ||
|
||
const options = { | ||
PUBLIC: 'Anyone with the link can edit', | ||
PRIVATE: 'Only people with access can open with the link' | ||
}; | ||
|
||
const SHARE_MODE = { | ||
PRIVATE: 'private', | ||
PUBLIC: 'public' | ||
}; | ||
|
||
interface User { | ||
id: string; | ||
user_id: string; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
avatar_url?: string; | ||
deleted_at?: { Valid: boolean }; | ||
} | ||
|
||
interface AccessListProps { | ||
accessList: User[]; | ||
ownerData: User; | ||
handleDelete: (email: string) => void; | ||
hostURL?: string | null; | ||
} | ||
|
||
/** | ||
* Custom component to show users list with delete icon and owner tag | ||
*/ | ||
const AccessList: React.FC<AccessListProps> = ({ | ||
accessList, | ||
ownerData, | ||
handleDelete, | ||
hostURL | ||
}: AccessListProps) => { | ||
const openInNewTab = (url: string) => { | ||
window.open(url, '_blank', 'noreferrer'); | ||
}; | ||
|
||
const theme = useTheme(); | ||
|
||
return ( | ||
<> | ||
{accessList.length > 0 && ( | ||
<Typography variant="h6" style={{ marginTop: '0.5rem' }}> | ||
People with Access | ||
</Typography> | ||
)} | ||
<ListWrapper> | ||
<List dense> | ||
{accessList.map((actorData) => ( | ||
<ListItem key={actorData.id} style={{ paddingLeft: '0' }}> | ||
<ListItemAvatar> | ||
<Avatar | ||
alt={actorData.first_name} | ||
src={actorData.avatar_url} | ||
imgProps={{ referrerPolicy: 'no-referrer' }} | ||
onClick={() => { | ||
hostURL && openInNewTab(`${hostURL}/user/${actorData.id}`); | ||
}} | ||
/> | ||
</ListItemAvatar> | ||
<ListItemText | ||
primary={`${actorData.first_name || ''} ${actorData.last_name || ''}`} | ||
secondary={actorData.email} | ||
/> | ||
<ListItemSecondaryAction> | ||
{ownerData.id === actorData.id ? ( | ||
<div>Owner</div> | ||
) : ( | ||
<IconButton | ||
edge="end" | ||
aria-label="delete" | ||
onClick={() => handleDelete(actorData.email)} | ||
> | ||
<DeleteIcon fill={theme.palette.background.neutral?.default} /> | ||
</IconButton> | ||
)} | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
))} | ||
</List> | ||
</ListWrapper> | ||
</> | ||
); | ||
}; | ||
|
||
interface SelectedResource { | ||
visibility: string; | ||
name: string; | ||
[key: string]: unknown; | ||
} | ||
|
||
interface ShareModalProps { | ||
/** Function to close the share modal */ | ||
handleShareModalClose: () => void; | ||
/** The resource that is selected for sharing.*/ | ||
selectedResource: SelectedResource; | ||
/** The name of the data being shared, like design or filter */ | ||
dataName: string; | ||
/** Data of the user who owns the resource */ | ||
ownerData: User; | ||
/** Function to fetch the list of users who have access to the resource */ | ||
fetchAccessActors: () => Promise<User[]>; | ||
/** Function to handle the sharing of the resource with specified users and options */ | ||
handleShare: (shareUserData: User[], selectedOption: string | undefined) => void; | ||
/** Optional URL of the host application. Defaults to `null` if not provided */ | ||
hostURL?: string | null; | ||
/** | ||
* Optional URL of the resource. Defaults to empty string if not provided | ||
* Resource URL will be the URL which user will copy with Copy Link Button | ||
*/ | ||
resourceURL?: string; | ||
/** Optional flag to disable the visibility selector. Defaults to `false` if not provided */ | ||
isVisibilitySelectorDisabled?: boolean; | ||
/** | ||
* Function to fetch user suggestions based on the input value. | ||
* @param {string} value - The input value for which suggestions are to be fetched. | ||
* @returns {Promise<User[]>} A promise that resolves to an array of user suggestions. | ||
*/ | ||
fetchSuggestions: (value: string) => Promise<User[]>; | ||
} | ||
|
||
/** | ||
* ShareModal component allows sharing a resource with specified users | ||
* and configuring visibility options. | ||
*/ | ||
const ShareModal: React.FC<ShareModalProps> = ({ | ||
handleShareModalClose, | ||
selectedResource, | ||
dataName, | ||
ownerData, | ||
fetchAccessActors, | ||
handleShare, | ||
hostURL = null, | ||
resourceURL = '', | ||
isVisibilitySelectorDisabled = false, | ||
fetchSuggestions | ||
}: ShareModalProps): JSX.Element => { | ||
const theme = useTheme(); | ||
const [openMenu, setMenu] = useState<boolean>(false); | ||
const [selectedOption, setOption] = useState<string | undefined>(selectedResource?.visibility); | ||
const [shareUserData, setShareUserData] = useState<User[]>([]); | ||
|
||
const handleDelete = (email: string) => { | ||
setShareUserData((prevData) => prevData.filter((user) => user.email !== email)); | ||
}; | ||
|
||
const handleOptionClick = (event: SelectChangeEvent<unknown>) => { | ||
const value = event.target.value as string; | ||
setOption(value); | ||
}; | ||
|
||
const handleMenuClose = () => setMenu(false); | ||
|
||
/** | ||
* Copy design link in clipboard | ||
*/ | ||
const handleCopy = () => { | ||
navigator.clipboard.writeText(resourceURL); | ||
}; | ||
|
||
const isShareDisabled = () => { | ||
const existingAccessIds = shareUserData.map((user) => user.id); | ||
const ownerDataId = ownerData?.id; | ||
|
||
if (ownerDataId) { | ||
existingAccessIds.push(ownerDataId); | ||
} | ||
|
||
const hasMismatchedUsers = !shareUserData.every((user) => existingAccessIds.includes(user.id)); | ||
|
||
return ( | ||
shareUserData.length === existingAccessIds.length && | ||
!hasMismatchedUsers && | ||
(selectedOption === selectedResource?.visibility || | ||
shareUserData.length !== existingAccessIds.length) | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchActors = async () => { | ||
const actors = await fetchAccessActors(); | ||
setShareUserData(actors); | ||
}; | ||
fetchActors(); | ||
}, [fetchAccessActors]); | ||
|
||
useEffect(() => { | ||
if (selectedResource) { | ||
setOption(selectedResource?.visibility); | ||
} | ||
}, [selectedResource]); | ||
|
||
return ( | ||
<div style={{ marginBottom: '1rem' }}> | ||
<Modal | ||
open={true} | ||
closeModal={handleShareModalClose} | ||
title={`Share ${dataName} "${selectedResource?.name}"`} | ||
> | ||
<ModalBody> | ||
<UserSearchField | ||
setUsersData={setShareUserData} | ||
usersData={shareUserData} | ||
label="Search Users" | ||
customUsersList={ | ||
<AccessList | ||
accessList={shareUserData} | ||
ownerData={ownerData} | ||
handleDelete={handleDelete} | ||
hostURL={hostURL} | ||
/> | ||
} | ||
fetchSuggestions={fetchSuggestions} | ||
/> | ||
<CustomListItemText> | ||
<Typography variant="h6">General Access</Typography> | ||
</CustomListItemText> | ||
<CustomDialogContentText> | ||
<FormControlWrapper size="small"> | ||
<div style={{ display: 'flex', justifyContent: 'start', alignItems: 'center' }}> | ||
<VisibilityIconWrapper> | ||
{selectedOption === SHARE_MODE.PUBLIC ? ( | ||
<PublicIcon | ||
width={24} | ||
height={24} | ||
stroke={theme.palette.mode === 'dark' ? WHITE : BLACK} | ||
/> | ||
) : ( | ||
<LockIcon | ||
width={24} | ||
height={24} | ||
stroke={theme.palette.mode === 'dark' ? WHITE : BLACK} | ||
/> | ||
)} | ||
</VisibilityIconWrapper> | ||
<div style={{ display: 'flex', flexDirection: 'column' }}> | ||
<CustomSelect | ||
variant="outlined" | ||
defaultValue={selectedOption} | ||
labelId="share-menu-select" | ||
id="share-menu" | ||
open={openMenu} | ||
onClose={handleMenuClose} | ||
onOpen={() => setMenu(true)} | ||
onChange={handleOptionClick} | ||
disabled={isVisibilitySelectorDisabled} | ||
> | ||
{Object.values(SHARE_MODE).map((option) => ( | ||
<MenuItem key={option} selected={option === selectedOption} value={option}> | ||
{option.charAt(0).toUpperCase() + option.slice(1)} | ||
</MenuItem> | ||
))} | ||
</CustomSelect> | ||
<Typography component="span" variant="body2"> | ||
{selectedOption === SHARE_MODE.PRIVATE ? options.PRIVATE : options.PUBLIC} | ||
</Typography> | ||
</div> | ||
</div> | ||
</FormControlWrapper> | ||
</CustomDialogContentText> | ||
</ModalBody> | ||
|
||
<ModalFooter variant="filled"> | ||
<ModalButtonSecondary | ||
variant="outlined" | ||
onClick={handleCopy} | ||
style={{ marginRight: '1rem', padding: '7px 16px' }} | ||
> | ||
<IconButtonWrapper> | ||
<ChainIcon width={24} height={24} fill={theme.palette.text.constant?.white} /> | ||
</IconButtonWrapper> | ||
<Typography>Copy Link</Typography> | ||
</ModalButtonSecondary> | ||
<ModalButtonPrimary | ||
disabled={isShareDisabled()} | ||
variant="contained" | ||
color="primary" | ||
onClick={() => handleShare(shareUserData, selectedOption)} | ||
> | ||
Share | ||
</ModalButtonPrimary> | ||
</ModalFooter> | ||
</Modal> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ShareModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ShareModal from './ShareModal'; | ||
|
||
export { ShareModal }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use direct colors, use tokens here.