UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

41 lines (40 loc) 2.7 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useCallback, useEffect, useState } from 'react'; import { Box, FormGroup, FormHelperText, InputLabel } from '@mui/material'; import { observer } from 'mobx-react'; import LocationInput from "./LocationInput.js"; import SourceTypeSelector from "./SourceTypeSelector.js"; import useInternetAccounts from "./useInternetAccounts.js"; import { addAccountToLocation, getInitialSourceType } from "./util.js"; import { notEmpty } from "../../util/index.js"; import { isUriLocation } from "../../util/types/index.js"; const FileSelector = observer(function FileSelector({ inline, location, name, description, rootModel, setLocation, }) { const [sourceType, setSourceType] = useState(() => getInitialSourceType(location)); const { accountMap, shownAccountIds, hiddenAccountIds, recentlyUsed, setRecentlyUsed, } = useInternetAccounts(rootModel); const selectedAccount = accountMap[sourceType]; const handleLocationChange = useCallback((loc) => { setLocation(addAccountToLocation(loc, selectedAccount)); }, [setLocation, selectedAccount]); useEffect(() => { if (selectedAccount && isUriLocation(location) && location.internetAccountId !== selectedAccount.internetAccountId) { handleLocationChange(location); } }, [location, selectedAccount, handleLocationChange]); const handleSourceTypeChange = useCallback((newValue) => { if (newValue) { setRecentlyUsed([ ...new Set([newValue, ...recentlyUsed].filter(notEmpty)), ]); setSourceType(newValue); if (isUriLocation(location)) { handleLocationChange(location); } } }, [location, recentlyUsed, setRecentlyUsed, handleLocationChange]); return (_jsxs(_Fragment, { children: [_jsx(Box, { display: "flex", children: _jsx(InputLabel, { shrink: true, children: name }) }), _jsx(FormGroup, { children: _jsxs(Box, { display: "flex", flexDirection: inline ? 'row' : 'column', gap: 0.5, children: [_jsx(SourceTypeSelector, { value: sourceType, shownAccountIds: shownAccountIds, hiddenAccountIds: hiddenAccountIds, accountMap: accountMap, onChange: (_event, newValue) => { handleSourceTypeChange(newValue); }, onHiddenAccountSelect: handleSourceTypeChange }), _jsx(LocationInput, { toggleButtonValue: sourceType, selectedAccount: selectedAccount, location: location, inline: inline, setLocation: handleLocationChange })] }) }), _jsx(FormHelperText, { children: description })] })); }); export default FileSelector;