DetailsPageHeader.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 '../../NavigationModule/NavigationMini/NavigationMini'
  19. import NotificationDropdown from '../../../ui/Dropdowns/NotificationDropdown/NotificationDropdown'
  20. import UserDropdown from '../../../ui/Dropdowns/UserDropdown/UserDropdown'
  21. import AboutModal from '../../../smart/AboutModal/AboutModal'
  22. import type { User as UserType } from '../../../../@types/User'
  23. import notificationStore from '../../../../stores/NotificationStore'
  24. import backgroundImage from './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: { label: React.ReactNode, value: string }) => void,
  57. testMode?: boolean,
  58. }
  59. @observer
  60. class DetailsPageHeader extends React.Component<Props, State> {
  61. state = {
  62. showAbout: false,
  63. }
  64. pollTimeout: number | undefined
  65. stopPolling!: boolean
  66. UNSAFE_componentWillMount() {
  67. if (this.props.testMode) {
  68. return
  69. }
  70. this.stopPolling = false
  71. this.pollData(true)
  72. }
  73. componentWillUnmount() {
  74. clearTimeout(this.pollTimeout)
  75. this.stopPolling = true
  76. }
  77. handleNotificationsClose() {
  78. notificationStore.saveSeen()
  79. }
  80. handleUserItemClick(item: { label: React.ReactNode, value: string }) {
  81. switch (item.value) {
  82. case 'about':
  83. this.setState({ showAbout: true })
  84. break
  85. default:
  86. this.props.onUserItemClick(item)
  87. }
  88. }
  89. async pollData(showLoading?: boolean) {
  90. if (this.stopPolling) {
  91. return
  92. }
  93. await notificationStore.loadData(showLoading)
  94. this.pollTimeout = setTimeout(() => { this.pollData() }, 15000)
  95. }
  96. render() {
  97. return (
  98. <Wrapper>
  99. <Menu>
  100. <NavigationMini />
  101. <Logo to="/" />
  102. </Menu>
  103. <User>
  104. <NotificationDropdown
  105. white
  106. items={notificationStore.notificationItems}
  107. onClose={() => this.handleNotificationsClose()}
  108. />
  109. <UserDropdownStyled
  110. white
  111. user={this.props.user}
  112. onItemClick={item => { this.handleUserItemClick(item) }}
  113. />
  114. </User>
  115. {this.state.showAbout ? (
  116. <AboutModal onRequestClose={() => { this.setState({ showAbout: false }) }} />
  117. ) : null}
  118. </Wrapper>
  119. )
  120. }
  121. }
  122. export default DetailsPageHeader