import queryString from 'query-string'; import {FormattedMessage} from 'react-intl'; import {Link, useLocation, useNavigate} from 'react-router'; import {Dropdown, Icon, Menu} from 'semantic-ui-react'; import {IndiInfo, JsonGedcomData} from 'topola'; import {isGoogleDriveConfigured} from '../datasource/google_drive_service'; import {Media} from '../util/media'; import {isOnWikitreeDomain} from '../util/wikitree_util'; import {GoogleDriveMenu} from './google_drive_menu'; import {MenuItem, MenuType} from './menu_item'; import {SearchBar} from './search'; import {UploadMenu} from './upload_menu'; import {UrlMenu} from './url_menu'; import {useSearch} from './use_search'; import {WikiTreeLoginMenu, WikiTreeMenu} from './wikitree_menu'; enum ScreenSize { LARGE, SMALL, } interface EventHandlers { onSelection: (indiInfo: IndiInfo) => void; onPrint: () => void; onDownloadPdf: () => void; onDownloadPng: () => void; onDownloadSvg: () => void; } interface Props { /** True if the application is currently showing a chart. */ showingChart: boolean; /** Data used for the search index. */ data?: JsonGedcomData; standalone: boolean; /** Whether to show the "All relatives" chart type in the menu. */ allowAllRelativesChart?: boolean; allowPrintAndDownload?: boolean; eventHandlers?: EventHandlers; /** Whether to show additional WikiTree menus. */ showWikiTreeMenus?: boolean; /** Whether the user has authorized Google Drive and has an active token. */ hasGoogleToken?: boolean; /** Callback to sign out of Google Drive. */ onGoogleSignOut?: () => void; /** Callback triggered when a new Google Drive token is acquired. */ onGoogleTokenAcquired?: () => void; } export function TopBar(props: Props) { const navigate = useNavigate(); const location = useLocation(); // WikiTree menus only work when hosted on apps.wikitree.com because // WikiTree's API requires same-origin access. The CORS proxy workaround is // no longer functional. const showWikiTree = props.showWikiTreeMenus && isOnWikitreeDomain(); const {searchResults, searchString, setSearchString, handleResultSelect} = useSearch({ data: props.data, onSelection: props.eventHandlers?.onSelection, }); function changeView(view: string) { const search = queryString.parse(location.search); if (search.view !== view) { search.view = view; location.search = queryString.stringify(search); navigate(location); } } function chartMenus(screenSize: ScreenSize) { if (!props.showingChart || !props.data) { return null; } const chartTypeItems = ( <> changeView('hourglass')}> {props.allowAllRelativesChart ? ( changeView('relatives')}> ) : null} changeView('donatso')}> changeView('fancy')}> ); switch (screenSize) { case ScreenSize.LARGE: return ( <> } className="item" disabled={!props.allowPrintAndDownload} > } className="item" > {chartTypeItems} ); case ScreenSize.SMALL: return ( <> {chartTypeItems} ); } } function title() { return ( Topola Genealogy ); } function googleDriveDisconnectMenu(screenSize: ScreenSize) { if (!props.hasGoogleToken || !isGoogleDriveConfigured()) { return null; } return ( <> {screenSize === ScreenSize.SMALL ? : null} ); } function fileMenuItems(menuType: MenuType) { return ( <> {showWikiTree && } ); } function fileMenus(screenSize: ScreenSize) { // In standalone WikiTree mode, show only the "Select WikiTree ID" menu. if (!props.standalone && showWikiTree) { switch (screenSize) { case ScreenSize.LARGE: return ; case ScreenSize.SMALL: return ( <> ); } } // Don't show "open" menus in non-standalone mode. if (!props.standalone) { return null; } switch (screenSize) { case ScreenSize.LARGE: // Show dropdown if chart is shown, otherwise show individual menu // items. return props.showingChart ? ( } className="item" > {fileMenuItems(MenuType.Dropdown)} ) : ( fileMenuItems(MenuType.Menu) ); case ScreenSize.SMALL: return ( <> {fileMenuItems(MenuType.Dropdown)} ); } } function wikiTreeLoginMenu(screenSize: ScreenSize) { if (!showWikiTree) { return null; } return ( <> {screenSize === ScreenSize.SMALL ? : null} ); } function mobileMenus() { return ( <> } className="item" icon={null} > {fileMenus(ScreenSize.SMALL)} {chartMenus(ScreenSize.SMALL)} {googleDriveDisconnectMenu(ScreenSize.SMALL)} {wikiTreeLoginMenu(ScreenSize.SMALL)}
{props.standalone ? {title()} : title()}
{props.showingChart && props.data && ( )} ); } function desktopMenus() { return ( <> {props.standalone ? {title()} : null} {fileMenus(ScreenSize.LARGE)} {chartMenus(ScreenSize.LARGE)} {googleDriveDisconnectMenu(ScreenSize.LARGE)} {wikiTreeLoginMenu(ScreenSize.LARGE)} ); } return (
{desktopMenus()} {mobileMenus()}
); }