DetailsPageHeader.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from "react";
  15. import { Link } from "react-router-dom";
  16. import styled from "styled-components";
  17. import { observer } from "mobx-react";
  18. import NavigationMini from "@src/components/modules/NavigationModule/NavigationMini";
  19. import NotificationDropdown from "@src/components/ui/Dropdowns/NotificationDropdown";
  20. import UserDropdown from "@src/components/ui/Dropdowns/UserDropdown";
  21. import AboutModal from "@src/components/smart/AboutModal";
  22. import type { User as UserType } from "@src/@types/User";
  23. import notificationStore from "@src/stores/NotificationStore";
  24. import backgroundImage from "@src/components/ui/Images/star-bg.jpg";
  25. import logoImage from "./images/logo.svg";
  26. const Wrapper = styled.div<any>`
  27. display: flex;
  28. height: 64px;
  29. background: url("${backgroundImage}");
  30. align-items: center;
  31. padding-right: 22px;
  32. justify-content: space-between;
  33. `;
  34. const Logo = styled(Link)`
  35. width: 240px;
  36. height: 48px;
  37. background: url("${logoImage}") no-repeat;
  38. cursor: pointer;
  39. `;
  40. const UserDropdownStyled = styled(UserDropdown)`
  41. margin-left: 16px;
  42. `;
  43. const Menu = styled.div<any>`
  44. display: flex;
  45. align-items: center;
  46. `;
  47. const User = styled.div<any>`
  48. display: flex;
  49. align-items: center;
  50. `;
  51. type State = {
  52. showAbout: boolean;
  53. };
  54. type Props = {
  55. user?: UserType | null;
  56. onUserItemClick: (userItem: {
  57. label: React.ReactNode;
  58. value: string;
  59. }) => void;
  60. testMode?: boolean;
  61. };
  62. @observer
  63. class DetailsPageHeader extends React.Component<Props, State> {
  64. state = {
  65. showAbout: false,
  66. };
  67. pollTimeout: number | undefined;
  68. stopPolling!: boolean;
  69. UNSAFE_componentWillMount() {
  70. if (this.props.testMode) {
  71. return;
  72. }
  73. this.stopPolling = false;
  74. this.pollData(true);
  75. }
  76. componentWillUnmount() {
  77. clearTimeout(this.pollTimeout);
  78. this.stopPolling = true;
  79. }
  80. handleNotificationsClose() {
  81. notificationStore.saveSeen();
  82. }
  83. handleUserItemClick(item: { label: React.ReactNode; value: string }) {
  84. switch (item.value) {
  85. case "about":
  86. this.setState({ showAbout: true });
  87. break;
  88. default:
  89. this.props.onUserItemClick(item);
  90. }
  91. }
  92. async pollData(showLoading?: boolean) {
  93. if (this.stopPolling) {
  94. return;
  95. }
  96. await notificationStore.loadData(showLoading);
  97. this.pollTimeout = window.setTimeout(() => {
  98. this.pollData();
  99. }, 15000);
  100. }
  101. render() {
  102. return (
  103. <Wrapper>
  104. <Menu>
  105. <NavigationMini />
  106. <Logo to="/" />
  107. </Menu>
  108. <User>
  109. <NotificationDropdown
  110. white
  111. items={notificationStore.notificationItems}
  112. onClose={() => this.handleNotificationsClose()}
  113. />
  114. <UserDropdownStyled
  115. white
  116. user={this.props.user}
  117. onItemClick={item => {
  118. this.handleUserItemClick(item);
  119. }}
  120. />
  121. </User>
  122. {this.state.showAbout ? (
  123. <AboutModal
  124. onRequestClose={() => {
  125. this.setState({ showAbout: false });
  126. }}
  127. />
  128. ) : null}
  129. </Wrapper>
  130. );
  131. }
  132. }
  133. export default DetailsPageHeader;