index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useCallback, useState, useMemo, createRef } from 'react'
  21. import moment from 'moment'
  22. import ConfigForm from './ConfigForm'
  23. import { Modal } from 'antd'
  24. import { FormComponentProps } from 'antd/lib/form/Form'
  25. import Ctrl from './Ctrl'
  26. import { Tmode, TShareVizsType, IShareTokenParams } from './types'
  27. import { IProjectRoles } from 'containers/Organizations/component/ProjectRole'
  28. import { IOrganizationMember } from 'containers/Organizations/types'
  29. import { DEFAULT_DATETIME_FORMAT, SHARE_HOST } from 'app/globalConstants'
  30. import styles from './SharePanel.less'
  31. interface ISharePanelProps {
  32. visible: boolean
  33. id: number
  34. itemId?: number
  35. type: TShareVizsType
  36. title: string
  37. shareToken: string
  38. passwordShareToken: string
  39. password: string
  40. authorizedShareToken: string
  41. loading: boolean
  42. projectRoles: IProjectRoles[]
  43. organizationMembers: IOrganizationMember[]
  44. onLoadDashboardShareLink?: (params: IShareTokenParams) => void
  45. onLoadWidgetShareLink?: (params: IShareTokenParams) => void
  46. onLoadDisplayShareLink?: (params: IShareTokenParams) => void
  47. onClose: () => void
  48. }
  49. const SharePanel: React.FC<ISharePanelProps> = (props) => {
  50. const {
  51. id,
  52. type,
  53. title,
  54. itemId,
  55. loading,
  56. onClose,
  57. visible,
  58. passwordShareToken,
  59. password,
  60. shareToken,
  61. projectRoles,
  62. organizationMembers,
  63. authorizedShareToken,
  64. onLoadWidgetShareLink,
  65. onLoadDisplayShareLink,
  66. onLoadDashboardShareLink
  67. } = props
  68. const [mode, setMode] = useState<Tmode>('NORMAL')
  69. const [viewers, setViewers] = useState<number[]>()
  70. const [roles, setRoles] = useState<number[]>()
  71. const configForm = createRef<FormComponentProps>()
  72. const getShareToken = useCallback(() => {
  73. configForm.current.form.validateFieldsAndScroll((err, values) => {
  74. if (!err) {
  75. const { permission } = values
  76. const expired = moment(values.expired).format(DEFAULT_DATETIME_FORMAT)
  77. const params = { id, itemId, mode, expired, permission, roles, viewers }
  78. switch (type) {
  79. case 'dashboard':
  80. onLoadDashboardShareLink(params)
  81. break
  82. case 'widget':
  83. onLoadWidgetShareLink(params)
  84. break
  85. case 'display':
  86. onLoadDisplayShareLink(params)
  87. default:
  88. break
  89. }
  90. }
  91. })
  92. }, [id, itemId, mode, type, roles, viewers, configForm])
  93. const afterModalClose = () => {
  94. setMode('NORMAL')
  95. }
  96. const modeChange = useCallback((val: Tmode) => {
  97. if (val === 'AUTH') {
  98. setViewers([])
  99. setRoles([])
  100. }
  101. setMode(val)
  102. }, [])
  103. const shareUrl = useMemo(() => {
  104. let token = ''
  105. switch (mode) {
  106. case 'NORMAL':
  107. token = shareToken
  108. break
  109. case 'PASSWORD':
  110. token = passwordShareToken
  111. break
  112. case 'AUTH':
  113. token = authorizedShareToken
  114. break
  115. }
  116. if (token) {
  117. switch (type) {
  118. case 'dashboard':
  119. return `${SHARE_HOST}?shareToken=${encodeURI(token)}#share/dashboard`
  120. case 'widget':
  121. return `${SHARE_HOST}?shareToken=${encodeURI(token)}#share/dashboard`
  122. case 'display':
  123. return `${SHARE_HOST}?shareToken=${encodeURI(token)}#share/display`
  124. }
  125. } else {
  126. return ''
  127. }
  128. }, [mode, type, shareToken, passwordShareToken, authorizedShareToken])
  129. return (
  130. <Modal
  131. title={`分享-${title}`}
  132. visible={visible}
  133. wrapClassName="ant-modal-small"
  134. footer={false}
  135. onCancel={onClose}
  136. afterClose={afterModalClose}
  137. destroyOnClose
  138. >
  139. <div className={styles.sharePanel}>
  140. <Ctrl mode={mode} onModeChange={modeChange} />
  141. <ConfigForm
  142. mode={mode}
  143. shareUrl={shareUrl}
  144. password={password}
  145. loading={loading}
  146. projectRoles={projectRoles}
  147. organizationMembers={organizationMembers}
  148. onSetRoles={setRoles}
  149. onSetViewers={setViewers}
  150. onGetToken={getShareToken}
  151. wrappedComponentRef={configForm}
  152. />
  153. </div>
  154. </Modal>
  155. )
  156. }
  157. export default SharePanel