import React, { useCallback, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import { Input, Popover, PopoverBody } from 'reactstrap'; import classnames from 'classnames'; import data from '@emoji-mart/data'; import Picker from '@emoji-mart/react'; import { init } from 'emoji-mart'; import { gettext } from '../../../utils/constants'; init({ data }); // init data for emoji-mart, used in Picker and `getRandomEmoji` method const propTypes = { currentPageConfig: PropTypes.object.isRequired, onUpdatePage: PropTypes.func.isRequired, }; const PageHeader = ({ currentPageConfig, onUpdatePage }) => { const [isShowController, setIsShowController] = useState(false); const [isShowIconPanel, setIsShowIconPanel] = useState(false); const iconPanelPopoverRef = useRef(null); console.log('currentPageConfig', currentPageConfig); const handleRenameDocument = useCallback((e) => { const { nativeEvent: { isComposing } } = e; if (isComposing) return; const newName = e.target.value.trim(); const { id, name, icon } = currentPageConfig; if (newName === name) return; const pageConfig = { name: newName, icon }; onUpdatePage && onUpdatePage(id, pageConfig); }, [currentPageConfig, onUpdatePage]); const changeControllerDisplayed = useCallback(() => { setIsShowController(!isShowController); }, [isShowController]); const handleSetIcon = useCallback((emoji, cb) => { onUpdatePage(currentPageConfig.id, { name: currentPageConfig.name, icon: emoji }); cb && cb(); }, [currentPageConfig.id, currentPageConfig.name, onUpdatePage]); const setRandomEmoji = useCallback(() => { const nativeEmojis = Reflect.ownKeys(data.natives); const emojiCount = nativeEmojis.length; const emoji = nativeEmojis[Math.floor(Math.random() * emojiCount)]; handleSetIcon(emoji); }, [handleSetIcon]); const handleIconPanelDisplayedChange = useCallback(() => { setIsShowIconPanel(!isShowIconPanel); }, [isShowIconPanel]); const handleClickAddIcon = useCallback(() => { setRandomEmoji(); handleIconPanelDisplayedChange(); }, [handleIconPanelDisplayedChange, setRandomEmoji]); const handleIconPanelListener = useCallback((e) => { const isClickInPopover = iconPanelPopoverRef.current.contains(e.target); if (!isClickInPopover) handleIconPanelDisplayedChange(); }, [handleIconPanelDisplayedChange]); // Update current page favicon useEffect(() => { let faviconUrl = ''; if (currentPageConfig.icon) { faviconUrl = `data:image/svg+xml,`; } else { const { serviceUrl, mediaUrl, faviconPath } = window.seafile; faviconUrl = `${serviceUrl}${mediaUrl}${faviconPath}`; } document.getElementById('favicon').href = faviconUrl; }, [currentPageConfig.icon]); useEffect(() => { if (isShowIconPanel) { setTimeout(() => { // Avoid open behavior closing popover addEventListener('click', handleIconPanelListener); }, 0); } if (!isShowIconPanel) removeEventListener('click', handleIconPanelListener); return () => { removeEventListener('click', handleIconPanelListener); }; }, [handleIconPanelListener, isShowIconPanel]); const handleIconRemove = () => { handleSetIcon(''); handleIconPanelDisplayedChange(); }; const handleAddCover = useCallback(() => { }, []); return (