owncast/web/pages/components/main-layout.tsx

177 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-11-04 03:15:38 +01:00
import React, { useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Link from 'next/link';
import Head from 'next/head'
2020-11-03 02:23:32 +01:00
import { differenceInSeconds } from "date-fns";
import { useRouter } from 'next/router';
2020-11-12 07:54:27 +01:00
import { Layout, Menu, Popover } from 'antd';
2020-11-04 03:15:38 +01:00
import {
SettingOutlined,
HomeOutlined,
LineChartOutlined,
ToolOutlined,
PlayCircleFilled,
2020-10-23 02:16:28 +02:00
MinusSquareFilled,
} from '@ant-design/icons';
2020-10-23 02:16:28 +02:00
import classNames from 'classnames';
2020-11-04 03:15:38 +01:00
import { upgradeVersionAvailable } from "../../utils/apis";
import { parseSecondsToDurationString } from '../../utils/format'
import OwncastLogo from './logo';
import { ServerStatusContext } from '../../utils/server-status-context';
import adminStyles from '../../styles/styles.module.css';
let performedUpgradeCheck = false;
export default function MainLayout(props) {
const { children } = props;
const context = useContext(ServerStatusContext);
const { online, broadcaster, versionNumber } = context || {};
const router = useRouter();
const { route } = router || {};
const { Header, Footer, Content, Sider } = Layout;
const { SubMenu } = Menu;
const streamDurationString = online ? parseSecondsToDurationString(differenceInSeconds(new Date(), new Date(broadcaster.time))) : "";
2020-11-03 02:23:32 +01:00
2020-11-12 07:54:27 +01:00
const content = (
<div>
<img src="/thumbnail.jpg" width="200px" />
</div>
);
const statusIcon = online ? <PlayCircleFilled /> : <MinusSquareFilled />;
const statusMessage = online ? `Online ${streamDurationString}` : "Offline";
2020-11-04 03:15:38 +01:00
const [upgradeVersion, setUpgradeVersion] = useState(null);
const checkForUpgrade = async () => {
try {
const result = await upgradeVersionAvailable(versionNumber);
2020-11-04 03:15:38 +01:00
setUpgradeVersion(result);
} catch (error) {
console.log("==== error", error);
}
};
useEffect(() => {
if (!performedUpgradeCheck && !context.disableUpgradeChecks) {
checkForUpgrade();
performedUpgradeCheck = true
}
});
2020-11-04 03:15:38 +01:00
const appClass = classNames({
"owncast-layout": true,
[adminStyles.online]: online,
});
2020-11-04 03:15:38 +01:00
const upgradeMenuItemStyle = upgradeVersion ? 'block' : 'none';
const upgradeVersionString = upgradeVersion || '';
return (
<Layout className={appClass}>
<Head>
<title>Owncast Admin</title>
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png"/>
</Head>
<Sider
width={240}
style={{
2020-10-29 18:16:13 +01:00
overflow: "auto",
height: "100vh",
}}
>
<Menu
theme="dark"
defaultSelectedKeys={[route.substring(1) || "home"]}
defaultOpenKeys={["current-stream-menu", "utilities-menu", "configuration"]}
mode="inline"
>
<h1 className={adminStyles.owncastTitleContainer}>
<span className={adminStyles.logoContainer}>
<OwncastLogo />
</span>
<span className={adminStyles.owncastTitle}>Owncast Admin</span>
</h1>
<Menu.Item key="home" icon={<HomeOutlined />}>
2020-10-23 02:16:28 +02:00
<Link href="/">Home</Link>
</Menu.Item>
2020-10-29 18:16:13 +01:00
<SubMenu
key="current-stream-menu"
icon={<LineChartOutlined />}
2020-11-01 03:29:06 +01:00
title="Current stream"
2020-10-29 18:16:13 +01:00
>
<Menu.Item key="viewer-info">
<Link href="/viewer-info">Viewers</Link>
</Menu.Item>
{/* {online ? (
<Menu.Item key="disconnect-stream" icon={<CloseCircleOutlined />}>
<Link href="/disconnect-stream">Disconnect Stream...</Link>
</Menu.Item>
) : null} */}
</SubMenu>
2020-10-29 18:16:13 +01:00
<SubMenu
2020-11-01 03:29:06 +01:00
key="configuration"
title="Configuration"
2020-10-29 18:16:13 +01:00
icon={<SettingOutlined />}
>
<Menu.Item key="update-server-config">
<Link href="/update-server-config">Server</Link>
</Menu.Item>
2020-10-29 18:16:13 +01:00
<Menu.Item key="video-config">
<Link href="/video-config">Video</Link>
2020-10-29 18:16:13 +01:00
</Menu.Item>
<Menu.Item key="storage">
<Link href="/storage">Storage</Link>
</Menu.Item>
2020-11-01 03:29:06 +01:00
</SubMenu>
<SubMenu
key="utilities-menu"
icon={<ToolOutlined />}
2020-11-01 03:29:06 +01:00
title="Utilities"
>
<Menu.Item key="hardware-info">
<Link href="/hardware-info">Hardware</Link>
</Menu.Item>
2020-10-30 02:01:38 +01:00
<Menu.Item key="logs">
<Link href="/logs">Logs</Link>
</Menu.Item>
2020-11-04 03:15:38 +01:00
<Menu.Item key="upgrade" style={{ display: upgradeMenuItemStyle }}>
<Link href="/upgrade">
<a>Upgrade to v{upgradeVersionString}</a>
</Link>
</Menu.Item>
</SubMenu>
</Menu>
</Sider>
<Layout>
<Header className={adminStyles.header}>
2020-11-12 07:54:27 +01:00
<Popover content={content} title="Thumbnail" trigger="hover">
<div className={adminStyles.statusIndicatorContainer}>
2020-10-29 18:16:13 +01:00
<span className={adminStyles.statusLabel}>{statusMessage}</span>
<span className={adminStyles.statusIcon}>{statusIcon}</span>
</div>
2020-11-12 07:54:27 +01:00
</Popover>
</Header>
2020-10-29 18:16:13 +01:00
<Content className={adminStyles.contentMain}>{children}</Content>
<Footer style={{ textAlign: "center" }}>
<a href="https://owncast.online/">About Owncast v{versionNumber}</a>
2020-10-29 18:16:13 +01:00
</Footer>
</Layout>
</Layout>
);
}
MainLayout.propTypes = {
children: PropTypes.element.isRequired,
};