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 = (
<>