import React, { useContext, useState } from 'react'; import { Button, CssBaseline, Divider, IconButton, List, ListItem, Drawer, AppBar, Toolbar, Typography, InputBase, ListItemIcon, ListItemText, Menu, MenuItem, Hidden, Tooltip, Link, styled } from '@mui/material'; import { alpha, Theme, useTheme } from '@mui/material/styles'; import { ChevronLeft, ChevronRight, Menu as MenuIcon, Search as SearchIcon, AccountCircle } from '@mui/icons-material'; import { Link as RouterLink, useNavigate } from 'react-router-dom'; import { doLogout, UserContext } from '../state'; const drawerWidth = 270; const DrawerHeader = styled('div')(({ theme }) => ({ ...theme.mixins.toolbar })); const StyledDrawer = styled(Drawer)(({ theme }) => ({ flexShrink: 0, whiteSpace: "nowrap", [theme.breakpoints.up("sm")]: { width: drawerWidth, }, } )); const StyledSearchBar = styled('div')(({ theme }) => ({ position: 'relative', borderRadius: theme.shape.borderRadius, backgroundColor: alpha(theme.palette.common.white, 0.15), '&:hover': { backgroundColor: alpha(theme.palette.common.white, 0.25), }, marginLeft: 0, width: '100%', [theme.breakpoints.up('sm')]: { marginLeft: theme.spacing(1), width: 'auto', }, })); const StyledInputBase = styled(InputBase)(({ theme }) => ({ color: 'inherit', '& .MuiInputBase-input': { padding: theme.spacing(1, 1, 1, 0), // vertical padding + font size from searchIcon paddingLeft: `calc(1em + ${theme.spacing(4)})`, transition: theme.transitions.create('width'), width: '100%', [theme.breakpoints.up('sm')]: { width: '12ch', '&:focus': { width: '20ch', }, }, }, })); const closedMixin = (theme: Theme) => ({ overflowX: 'hidden', width: `calc(${theme.spacing(7)} + 1px)`, }); export const Headline = (prop: { children?: React.ReactNode, classes?: { content?: string, toolbar?: string, }, menu: React.ReactNode }) => { const [v, setv] = useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const theme = useTheme(); const toggleV = () => setv(!v); const handleProfileMenuOpen = (e: React.MouseEvent) => setAnchorEl(e.currentTarget); const handleProfileMenuClose = () => setAnchorEl(null); const isProfileMenuOpened = Boolean(anchorEl); const menuId = 'primary-search-account-menu'; const user_ctx = useContext(UserContext); const isLogin = user_ctx.username !== ""; const navigate = useNavigate(); const [search, setSearch] = useState(""); const renderProfileMenu = ( Profile { handleProfileMenuClose(); await doLogout(); user_ctx.setUsername(""); }}>Logout ); const drawer_contents = (<> {theme.direction === "ltr" ? : } {prop.menu} ); return (
Ionian
navSearch(search)} />
setSearch(e.target.value)} onKeyUp={(e) => { if (e.key === "Enter") { navSearch(search); } }} value={search}>
{ isLogin ? : }
{renderProfileMenu}
{prop.children}
); function navSearch(search: string){ let words = search.includes("&") ? search.split("&") : [search]; words = words.map(w => w.trim()) .map(w => w.includes(":") ? `allow_tag=${w}` : `word=${encodeURIComponent(w)}`); navigate(`/search?${words.join("&")}`); } }; export default Headline;