chat-with-gpt/app/src/components/header.tsx

215 lines
6.6 KiB
TypeScript
Raw Normal View History

2023-03-06 13:30:58 +00:00
import styled from '@emotion/styled';
import Helmet from 'react-helmet';
2023-03-14 11:00:40 +00:00
import { FormattedMessage, useIntl } from 'react-intl';
2023-03-06 13:30:58 +00:00
import { useSpotlight } from '@mantine/spotlight';
2023-03-14 11:00:40 +00:00
import { Burger, Button, ButtonProps } from '@mantine/core';
2023-03-10 22:00:37 +00:00
import { useCallback, useMemo, useState } from 'react';
2023-03-08 21:30:11 +00:00
import { Link, useNavigate } from 'react-router-dom';
2023-03-06 13:30:58 +00:00
import { useAppContext } from '../context';
import { backend } from '../backend';
2023-03-14 11:00:40 +00:00
import { MenuItem, secondaryMenu } from '../menus';
2023-03-10 22:00:37 +00:00
import { useAppDispatch, useAppSelector } from '../store';
import { selectOpenAIApiKey } from '../store/api-keys';
import { setTab } from '../store/settings-ui';
2023-03-14 11:00:40 +00:00
import { selectSidebarOpen, toggleSidebar } from '../store/sidebar';
import { openLoginModal } from '../store/ui';
2023-03-06 13:30:58 +00:00
2023-03-08 21:30:11 +00:00
const HeaderContainer = styled.div`
2023-03-06 13:30:58 +00:00
display: flex;
2023-03-08 21:30:11 +00:00
flex-shrink: 0;
2023-03-06 13:30:58 +00:00
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
min-height: 2.618rem;
2023-03-14 11:00:40 +00:00
background: rgba(0, 0, 0, 0.0);
font-family: "Work Sans", sans-serif;
&.shaded {
background: rgba(0, 0, 0, 0.2);
}
2023-03-06 13:30:58 +00:00
h1 {
@media (max-width: 40em) {
width: 100%;
order: -1;
}
font-family: "Work Sans", sans-serif;
font-size: 1rem;
line-height: 1.3;
animation: fadein 0.5s;
animation-fill-mode: forwards;
strong {
font-weight: bold;
white-space: nowrap;
}
span {
display: block;
font-size: 70%;
white-space: nowrap;
}
@keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
}
2023-03-14 11:00:40 +00:00
h2 {
margin: 0 0.5rem;
font-size: 1rem;
}
2023-03-06 13:30:58 +00:00
.spacer {
2023-03-14 11:00:40 +00:00
flex-grow: 1;
2023-03-06 13:30:58 +00:00
}
i {
font-size: 90%;
}
2023-03-08 21:30:11 +00:00
i + span, .mantine-Button-root span.hide-on-mobile {
@media (max-width: 40em) {
position: absolute;
left: -9999px;
top: -9999px;
}
}
.mantine-Button-root {
@media (max-width: 40em) {
padding: 0.5rem;
}
}
`;
const SubHeaderContainer = styled.div`
display: flex;
flex-direction: row;
font-family: "Work Sans", sans-serif;
line-height: 1.7;
opacity: 0.7;
2023-03-14 11:00:40 +00:00
margin: 0.5rem 0.5rem 0 0.5rem;
2023-03-08 21:30:11 +00:00
.spacer {
flex-grow: 1;
}
a {
color: white;
}
.fa + span {
2023-03-14 11:00:40 +00:00
position: absolute;
left: -9999px;
top: -9999px;
2023-03-06 13:30:58 +00:00
}
`;
function HeaderButton(props: ButtonProps & { icon?: string, onClick?: any, children?: any }) {
return (
<Button size='xs'
2023-03-08 21:30:11 +00:00
variant={props.variant || 'subtle'}
onClick={props.onClick}>
2023-03-06 13:30:58 +00:00
{props.icon && <i className={'fa fa-' + props.icon} />}
{props.children && <span>
{props.children}
</span>}
</Button>
)
}
2023-03-08 21:30:11 +00:00
export interface HeaderProps {
title?: any;
onShare?: () => void;
share?: boolean;
canShare?: boolean;
}
export default function Header(props: HeaderProps) {
2023-03-06 13:30:58 +00:00
const context = useAppContext();
const navigate = useNavigate();
const spotlight = useSpotlight();
const [loading, setLoading] = useState(false);
2023-03-10 22:00:37 +00:00
const openAIApiKey = useAppSelector(selectOpenAIApiKey);
const dispatch = useAppDispatch();
2023-03-14 11:00:40 +00:00
const intl = useIntl();
const sidebarOpen = useAppSelector(selectSidebarOpen);
const onBurgerClick = useCallback(() => dispatch(toggleSidebar()), [dispatch]);
const burgerLabel = sidebarOpen
? intl.formatMessage({ defaultMessage: "Close sidebar" })
: intl.formatMessage({ defaultMessage: "Open sidebar" });
2023-03-06 13:30:58 +00:00
const onNewChat = useCallback(async () => {
setLoading(true);
navigate(`/`);
setLoading(false);
}, [navigate]);
const openSettings = useCallback(() => {
2023-03-10 22:00:37 +00:00
dispatch(setTab(openAIApiKey ? 'options' : 'user'));
2023-03-14 11:00:40 +00:00
}, [openAIApiKey, dispatch]);
2023-03-10 22:00:37 +00:00
const header = useMemo(() => (
2023-03-14 11:00:40 +00:00
<HeaderContainer className={context.isHome ? 'shaded' : ''}>
2023-03-10 22:00:37 +00:00
<Helmet>
2023-03-14 11:00:40 +00:00
<title>
{props.title ? `${props.title} - ` : ''}
{intl.formatMessage({ defaultMessage: "Chat with GPT - Unofficial ChatGPT app" })}
</title>
2023-03-10 22:00:37 +00:00
</Helmet>
2023-03-14 11:00:40 +00:00
{!sidebarOpen && <Burger opened={sidebarOpen} onClick={onBurgerClick} aria-label={burgerLabel} transitionDuration={0} />}
{context.isHome && <h2>{intl.formatMessage({ defaultMessage: "Chat with GPT" })}</h2>}
2023-03-10 22:00:37 +00:00
<div className="spacer" />
<HeaderButton icon="search" onClick={spotlight.openSpotlight} />
<HeaderButton icon="gear" onClick={openSettings} />
2023-03-14 11:00:40 +00:00
{backend.current && !props.share && props.canShare && typeof navigator.share !== 'undefined' && <HeaderButton icon="share" onClick={props.onShare}>
<FormattedMessage defaultMessage="Share" />
2023-03-10 22:00:37 +00:00
</HeaderButton>}
2023-03-14 11:00:40 +00:00
{backend.current && !context.authenticated && (
<HeaderButton onClick={() => {
if (process.env.REACT_APP_AUTH_PROVIDER !== 'local') {
backend.current?.signIn();
} else {
dispatch(openLoginModal());
}
}}>
<FormattedMessage defaultMessage="Sign in <h>to sync</h>"
values={{
h: (chunks: any) => <span className="hide-on-mobile">{chunks}</span>
}} />
</HeaderButton>
2023-03-10 22:00:37 +00:00
)}
<HeaderButton icon="plus" onClick={onNewChat} loading={loading} variant="light">
2023-03-14 11:00:40 +00:00
<FormattedMessage defaultMessage="New Chat" />
2023-03-10 22:00:37 +00:00
</HeaderButton>
</HeaderContainer>
2023-03-14 11:00:40 +00:00
), [sidebarOpen, onBurgerClick, props.title, props.share, props.canShare, props.onShare, openSettings, onNewChat, loading, context.authenticated, context.isHome, context.isShare, spotlight.openSpotlight]);
2023-03-10 22:00:37 +00:00
return header;
2023-03-08 21:30:11 +00:00
}
function SubHeaderMenuItem(props: { item: MenuItem }) {
return (
2023-03-14 11:00:40 +00:00
<Button variant="subtle" size="sm" compact component={Link} to={props.item.link} target="_blank" key={props.item.link}>
2023-03-08 21:30:11 +00:00
{props.item.icon && <i className={'fa fa-' + props.item.icon} />}
<span>{props.item.label}</span>
</Button>
);
}
export function SubHeader(props: any) {
2023-03-10 22:00:37 +00:00
const elem = useMemo(() => (
<SubHeaderContainer>
<div className="spacer" />
{secondaryMenu.map(item => <SubHeaderMenuItem item={item} key={item.link} />)}
</SubHeaderContainer>
), []);
return elem;
2023-03-06 13:30:58 +00:00
}