diff --git a/packages/core/ui/FileSelector/FileSelector.tsx b/packages/core/ui/FileSelector/FileSelector.tsx index 46668ff7bc..e4d53300f8 100644 --- a/packages/core/ui/FileSelector/FileSelector.tsx +++ b/packages/core/ui/FileSelector/FileSelector.tsx @@ -1,14 +1,10 @@ -import React, { useState, useRef } from 'react' +import React, { useState } from 'react' import { Box, FormHelperText, InputLabel, MenuItem, Tooltip, - Paper, - Popper, - Grow, - ClickAwayListener, Menu, } from '@material-ui/core' @@ -38,6 +34,13 @@ function ToggleButtonWithTooltip(props: ToggleButtonProps) { ) } +function shorten(str: string, len: number) { + if (typeof str === 'string' && str.length > len) { + return `${str.substring(0, len)}…` + } + return str +} + const FileSelector = observer( (props: { location?: FileLocation @@ -54,28 +57,23 @@ const FileSelector = observer( ? location.internetAccountId : fileOrUrl, ) - const internetAccounts = isAppRootModel(rootModel) + const accts = isAppRootModel(rootModel) ? rootModel.internetAccounts.slice() : [] - const numAccountsShown = 2 - const [shownInternetAccounts, setShownInternetAccounts] = useState( - internetAccounts.slice(0, numAccountsShown), - ) - const [hiddenInternetAccounts, setHiddenInternetAccounts] = useState( - internetAccounts.slice(numAccountsShown), - ) - const [moreMenuOpen, setMoreMenuOpen] = useState(false) - const moreMenuRef = useRef(null) + const numShown = 2 + const [shownAccts, setShownAccts] = useState(accts.slice(0, numShown)) + const [hiddenAccts, setHiddenAccts] = useState(accts.slice(numShown)) + const [anchorEl, setAnchorEl] = useState(null) - const selectedInternetAccount = internetAccounts.find( + const selectedAcct = accts.find( ia => ia.internetAccountId === toggleButtonValue, ) const setLocationWithAccount = (location: UriLocation) => { setLocation({ ...location, - internetAccountId: selectedInternetAccount - ? selectedInternetAccount.internetAccountId + internetAccountId: selectedAcct + ? selectedAcct.internetAccountId : undefined, }) } @@ -83,9 +81,9 @@ const FileSelector = observer( // if you swap account selection after inputting url if ( location && - selectedInternetAccount && + selectedAcct && isUriLocation(location) && - location.internetAccountId !== selectedInternetAccount.internetAccountId + location.internetAccountId !== selectedAcct.internetAccountId ) { setLocationWithAccount(location) } @@ -94,31 +92,19 @@ const FileSelector = observer( ) if (toggleButtonValue === 'file') { locationInput = } - if (selectedInternetAccount?.SelectorComponent) { - const { SelectorComponent } = selectedInternetAccount + if (selectedAcct?.SelectorComponent) { + const { SelectorComponent } = selectedAcct locationInput = ( ) } - const handleChange = ( - _event: React.MouseEvent, - newState: string, - ) => { - if (newState) { - setToggleButtonValue(newState) - } - if (isUriLocation(location)) { - setLocationWithAccount(location) - } - } - return ( <> @@ -129,7 +115,14 @@ const FileSelector = observer( { + if (newState) { + setToggleButtonValue(newState) + } + if (isUriLocation(location)) { + setLocationWithAccount(location) + } + }} aria-label="file, url, or account picker" > {new URLSearchParams(window.location.search).get( @@ -142,112 +135,64 @@ const FileSelector = observer( URL - {shownInternetAccounts?.map(internetAccount => { - const { toggleContents: customToggleContents, name } = - internetAccount - let toggleContents = customToggleContents || name - const maxLength = 5 - if ( - typeof toggleContents === 'string' && - toggleContents.length > maxLength - ) { - toggleContents = `${toggleContents.substring(0, maxLength)}…` - } - return ( - - {toggleContents} - - ) - })} - {hiddenInternetAccounts.length ? ( + {shownAccts.map(({ internetAccountId, toggleContents, name }) => ( + + {shorten(toggleContents || name, 5)} + + ))} + {hiddenAccts.length ? ( { - setMoreMenuOpen((prevOpen: boolean) => !prevOpen) - }} + onClick={event => setAnchorEl(event.target as HTMLElement)} selected={false} - ref={moreMenuRef} > More ) : null} - setAnchorEl(null)} + getContentAnchorEl={null} + anchorOrigin={{ + vertical: 'bottom', + horizontal: 'center', + }} + transformOrigin={{ + vertical: 'top', + horizontal: 'center', + }} > - {({ TransitionProps, placement }) => ( - ( + { + const prev = shownAccts[shownAccts.length - 1] + setShownAccts([ + ...shownAccts.slice(0, shownAccts.length - 1), + acct, + ]) + setHiddenAccts([ + prev, + ...hiddenAccts.slice(0, idx), + ...hiddenAccts.slice(idx + 1), + ]) + setToggleButtonValue(acct.internetAccountId) + setAnchorEl(null) }} > - - setMoreMenuOpen(false)} - > - setMoreMenuOpen(false)} - getContentAnchorEl={null} - anchorOrigin={{ - vertical: 'bottom', - horizontal: 'center', - }} - transformOrigin={{ - vertical: 'top', - horizontal: 'center', - }} - > - {hiddenInternetAccounts?.map((account, idx) => ( - { - const newlySelectedInternetAccount = - hiddenInternetAccounts[idx] - const lastShownInternetAccount = - shownInternetAccounts[ - shownInternetAccounts.length - 1 - ] - setShownInternetAccounts([ - ...shownInternetAccounts.slice( - 0, - shownInternetAccounts.length - 1, - ), - newlySelectedInternetAccount, - ]) - setHiddenInternetAccounts([ - lastShownInternetAccount, - ...hiddenInternetAccounts.slice(0, idx), - ...hiddenInternetAccounts.slice(idx + 1), - ]) - setToggleButtonValue(account.internetAccountId) - setMoreMenuOpen(false) - }} - > - {account.name} - - ))} - - - - - )} - + {acct.name} + + ))} + {locationInput}