MainLayout.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import React, { FC, ReactNode, useContext, useEffect, useState } from 'react';
  2. import Link from 'next/link';
  3. import Head from 'next/head';
  4. import { differenceInSeconds } from 'date-fns';
  5. import { useRouter } from 'next/router';
  6. import { Layout, Menu, Alert, Button, Space, Tooltip } from 'antd';
  7. import classNames from 'classnames';
  8. import dynamic from 'next/dynamic';
  9. import { upgradeVersionAvailable } from '../../utils/apis';
  10. import { parseSecondsToDurationString } from '../../utils/format';
  11. import { OwncastLogo } from '../common/OwncastLogo/OwncastLogo';
  12. import { ServerStatusContext } from '../../utils/server-status-context';
  13. import { AlertMessageContext } from '../../utils/alert-message-context';
  14. import { TextFieldWithSubmit } from './TextFieldWithSubmit';
  15. import { TEXTFIELD_PROPS_STREAM_TITLE } from '../../utils/config-constants';
  16. import { ComposeFederatedPost } from './ComposeFederatedPost';
  17. import { UpdateArgs } from '../../types/config-section';
  18. import { FatalErrorStateModal } from '../modals/FatalErrorStateModal/FatalErrorStateModal';
  19. // Lazy loaded components
  20. const SettingOutlined = dynamic(() => import('@ant-design/icons/SettingOutlined'), {
  21. ssr: false,
  22. }); // Lazy loaded components
  23. const HomeOutlined = dynamic(() => import('@ant-design/icons/HomeOutlined'), {
  24. ssr: false,
  25. });
  26. const LineChartOutlined = dynamic(() => import('@ant-design/icons/LineChartOutlined'), {
  27. ssr: false,
  28. });
  29. const ToolOutlined = dynamic(() => import('@ant-design/icons/ToolOutlined'), {
  30. ssr: false,
  31. });
  32. const PlayCircleFilled = dynamic(() => import('@ant-design/icons/PlayCircleFilled'), {
  33. ssr: false,
  34. });
  35. const MinusSquareFilled = dynamic(() => import('@ant-design/icons/MinusSquareFilled'), {
  36. ssr: false,
  37. });
  38. const QuestionCircleOutlined = dynamic(() => import('@ant-design/icons/QuestionCircleOutlined'), {
  39. ssr: false,
  40. });
  41. const MessageOutlined = dynamic(() => import('@ant-design/icons/MessageOutlined'), {
  42. ssr: false,
  43. });
  44. const ExperimentOutlined = dynamic(() => import('@ant-design/icons/ExperimentOutlined'), {
  45. ssr: false,
  46. });
  47. const EditOutlined = dynamic(() => import('@ant-design/icons/EditOutlined'), {
  48. ssr: false,
  49. });
  50. const FediverseOutlined = dynamic(() => import('../../assets/images/icons/fediverse.svg'), {
  51. ssr: false,
  52. });
  53. export type MainLayoutProps = {
  54. children: ReactNode;
  55. };
  56. export const MainLayout: FC<MainLayoutProps> = ({ children }) => {
  57. const context = useContext(ServerStatusContext);
  58. const { serverConfig, online, broadcaster, versionNumber, error: serverError } = context || {};
  59. const { instanceDetails, chatDisabled, federation } = serverConfig;
  60. const { enabled: federationEnabled } = federation;
  61. const [currentStreamTitle, setCurrentStreamTitle] = useState('');
  62. const [postModalDisplayed, setPostModalDisplayed] = useState(false);
  63. const alertMessage = useContext(AlertMessageContext);
  64. const router = useRouter();
  65. const { route } = router || {};
  66. const { Header, Footer, Content, Sider } = Layout;
  67. const [upgradeVersion, setUpgradeVersion] = useState('');
  68. const checkForUpgrade = async () => {
  69. try {
  70. const result = await upgradeVersionAvailable(versionNumber);
  71. setUpgradeVersion(result);
  72. } catch (error) {
  73. console.log('==== error', error);
  74. }
  75. };
  76. useEffect(() => {
  77. checkForUpgrade();
  78. }, [versionNumber]);
  79. useEffect(() => {
  80. setCurrentStreamTitle(instanceDetails.streamTitle);
  81. }, [instanceDetails]);
  82. const handleStreamTitleChanged = ({ value }: UpdateArgs) => {
  83. setCurrentStreamTitle(value);
  84. };
  85. const handleCreatePostButtonPressed = () => {
  86. setPostModalDisplayed(true);
  87. };
  88. const appClass = classNames({
  89. 'app-container': true,
  90. online,
  91. });
  92. const upgradeVersionString = `${upgradeVersion}` || '';
  93. const upgradeMessage = `Upgrade to v${upgradeVersionString}`;
  94. const openMenuItems = upgradeVersion ? ['utilities-menu'] : [];
  95. const clearAlertMessage = () => {
  96. alertMessage.setMessage(null);
  97. };
  98. const headerAlertMessage = alertMessage.message ? (
  99. <Alert message={alertMessage.message} afterClose={clearAlertMessage} banner closable />
  100. ) : null;
  101. // status indicator items
  102. const streamDurationString = broadcaster
  103. ? parseSecondsToDurationString(differenceInSeconds(new Date(), new Date(broadcaster.time)))
  104. : '';
  105. const statusIcon = online ? <PlayCircleFilled /> : <MinusSquareFilled />;
  106. const statusMessage = online ? `Online ${streamDurationString}` : 'Offline';
  107. const statusIndicator = (
  108. <div className="online-status-indicator">
  109. <span className="status-label">{statusMessage}</span>
  110. <span className="status-icon">{statusIcon}</span>
  111. </div>
  112. );
  113. const integrationsMenu = [
  114. {
  115. label: <Link href="/admin/webhooks">Webhooks</Link>,
  116. key: '/admin/webhooks',
  117. },
  118. {
  119. label: <Link href="/admin/access-tokens">Access Tokens</Link>,
  120. key: '/admin/access-tokens',
  121. },
  122. {
  123. label: <Link href="/admin/actions">External Actions</Link>,
  124. key: '/admin/actions',
  125. },
  126. ];
  127. const chatMenu = [
  128. {
  129. label: <Link href="/admin/chat/messages">Messages</Link>,
  130. key: '/admin/chat/messages',
  131. },
  132. {
  133. label: <Link href="/admin/chat/users">Users</Link>,
  134. key: '/admin/chat/users',
  135. },
  136. {
  137. label: <Link href="/admin/chat/emojis">Emojis</Link>,
  138. key: '/admin/chat/emojis',
  139. },
  140. ];
  141. const utilitiesMenu = [
  142. {
  143. label: <Link href="/admin/hardware-info">Hardware</Link>,
  144. key: '/admin/hardware-info',
  145. },
  146. {
  147. label: <Link href="/admin/stream-health">Stream Health</Link>,
  148. key: '/admin/stream-health',
  149. },
  150. {
  151. label: <Link href="/admin/logs">Logs</Link>,
  152. key: '/admin/logs',
  153. },
  154. federationEnabled && {
  155. label: <Link href="/admin/federation/actions">Social Actions</Link>,
  156. key: '/admin/federation/actions',
  157. },
  158. ];
  159. const configurationMenu = [
  160. {
  161. label: <Link href="/admin/config/general">General</Link>,
  162. key: '/admin/config/general',
  163. },
  164. {
  165. label: <Link href="/admin/config/server">Server Setup</Link>,
  166. key: '/admin/config/server',
  167. },
  168. {
  169. label: <Link href="/admin/config-video">Video</Link>,
  170. key: '/admin/config-video',
  171. },
  172. {
  173. label: <Link href="/admin/config-chat">Chat</Link>,
  174. key: '/admin/config-chat',
  175. },
  176. {
  177. label: <Link href="/admin/config-federation">Social</Link>,
  178. key: '/admin/config-federation',
  179. },
  180. {
  181. label: <Link href="/admin/config-notify">Notifications</Link>,
  182. key: '/admin/config-notify',
  183. },
  184. ];
  185. const menuItems = [
  186. { label: <Link href="/admin">Home</Link>, icon: <HomeOutlined />, key: '/admin' },
  187. {
  188. label: <Link href="/admin/viewer-info">Viewers</Link>,
  189. icon: <LineChartOutlined />,
  190. key: '/admin/viewer-info',
  191. },
  192. !chatDisabled && {
  193. label: <span>Chat &amp; Users</span>,
  194. icon: <MessageOutlined />,
  195. children: chatMenu,
  196. key: 'chat-and-users',
  197. },
  198. federationEnabled && {
  199. key: '/admin/federation/followers',
  200. label: <Link href="/admin/federation/followers">Followers</Link>,
  201. icon: (
  202. <span
  203. role="img"
  204. aria-label="message"
  205. className="anticon anticon-message ant-menu-item-icon"
  206. >
  207. {/* Wrapping the icon in span for consistency with other icons used
  208. directly from antd */}
  209. <FediverseOutlined />
  210. </span>
  211. ),
  212. },
  213. {
  214. key: 'configuration',
  215. label: 'Configuration',
  216. icon: <SettingOutlined />,
  217. children: configurationMenu,
  218. },
  219. {
  220. key: 'utilities',
  221. label: 'Utilities',
  222. icon: <ToolOutlined />,
  223. children: utilitiesMenu,
  224. },
  225. {
  226. key: 'integrations',
  227. label: 'Integrations',
  228. icon: <ExperimentOutlined />,
  229. children: integrationsMenu,
  230. },
  231. upgradeVersion && {
  232. key: '/admin/upgrade',
  233. label: <Link href="/admin/upgrade">{upgradeMessage}</Link>,
  234. },
  235. {
  236. key: '/admin/help',
  237. label: <Link href="/admin/help">Help</Link>,
  238. icon: <QuestionCircleOutlined />,
  239. },
  240. ];
  241. const [openKeys, setOpenKeys] = useState(openMenuItems);
  242. const onOpenChange = (keys: string[]) => {
  243. setOpenKeys(keys);
  244. };
  245. useEffect(() => {
  246. menuItems.forEach(
  247. item =>
  248. item?.children?.forEach(child => {
  249. if (child?.key === route) setOpenKeys([...openMenuItems, item.key]);
  250. }),
  251. );
  252. }, []);
  253. return (
  254. <Layout id="admin-page" className={appClass}>
  255. <Head>
  256. <title>Owncast Admin</title>
  257. <link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png" />
  258. </Head>
  259. {serverError?.type === 'OWNCAST_SERVICE_UNREACHABLE' && (
  260. <FatalErrorStateModal title="Server Unreachable" message={serverError.msg} />
  261. )}
  262. <Sider width={240} className="side-nav">
  263. <h1 className="owncast-title">
  264. <span className="logo-container">
  265. <OwncastLogo variant="simple" />
  266. </span>
  267. <span className="title-label">Owncast Admin</span>
  268. </h1>
  269. <Menu
  270. mode="inline"
  271. className="menu-container"
  272. items={menuItems}
  273. selectedKeys={[route || '/admin']}
  274. openKeys={openKeys}
  275. onOpenChange={onOpenChange}
  276. />
  277. </Sider>
  278. <Layout className="layout-main">
  279. <Header className="layout-header">
  280. <Space direction="horizontal">
  281. <Tooltip title="Compose post to your social followers">
  282. <Button
  283. type="link"
  284. icon={<EditOutlined />}
  285. size="small"
  286. onClick={handleCreatePostButtonPressed}
  287. style={{ display: federationEnabled ? 'block' : 'none', margin: '10px' }}
  288. >
  289. Compose Post
  290. </Button>
  291. </Tooltip>
  292. </Space>
  293. <div className="global-stream-title-container">
  294. <TextFieldWithSubmit
  295. fieldName="streamTitle"
  296. {...TEXTFIELD_PROPS_STREAM_TITLE}
  297. placeholder="What are you streaming now? (Stream title)"
  298. value={currentStreamTitle}
  299. initialValue={instanceDetails.streamTitle}
  300. onChange={handleStreamTitleChanged}
  301. />
  302. </div>
  303. <Space direction="horizontal">{statusIndicator}</Space>
  304. </Header>
  305. {headerAlertMessage}
  306. <Content className="main-content-container">{children}</Content>
  307. <Footer className="footer-container">
  308. <a href="https://owncast.online/?source=admin" target="_blank" rel="noopener noreferrer">
  309. About Owncast v{versionNumber}
  310. </a>
  311. </Footer>
  312. </Layout>
  313. <ComposeFederatedPost
  314. open={postModalDisplayed}
  315. handleClose={() => setPostModalDisplayed(false)}
  316. />
  317. </Layout>
  318. );
  319. };