您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import useSWR from 'swr'
  5. import Panel from '../panel'
  6. import { DataSourceType } from '../panel/types'
  7. import type { DataSourceNotion as TDataSourceNotion } from '@/models/common'
  8. import { useAppContext } from '@/context/app-context'
  9. import { fetchNotionConnection } from '@/service/common'
  10. import NotionIcon from '@/app/components/base/notion-icon'
  11. import { noop } from 'lodash-es'
  12. import { useTranslation } from 'react-i18next'
  13. import Toast from '@/app/components/base/toast'
  14. const Icon: FC<{
  15. src: string
  16. name: string
  17. className: string
  18. }> = ({ src, name, className }) => {
  19. return (
  20. <NotionIcon
  21. src={src}
  22. name={name}
  23. className={className}
  24. />
  25. )
  26. }
  27. type Props = {
  28. workspaces: TDataSourceNotion[]
  29. }
  30. const DataSourceNotion: FC<Props> = ({
  31. workspaces,
  32. }) => {
  33. const { isCurrentWorkspaceManager } = useAppContext()
  34. const [canConnectNotion, setCanConnectNotion] = useState(false)
  35. const { data } = useSWR(canConnectNotion ? '/oauth/data-source/notion' : null, fetchNotionConnection)
  36. const { t } = useTranslation()
  37. const connected = !!workspaces.length
  38. const handleConnectNotion = () => {
  39. if (!isCurrentWorkspaceManager)
  40. return
  41. setCanConnectNotion(true)
  42. }
  43. const handleAuthAgain = () => {
  44. if (data?.data)
  45. window.location.href = data.data
  46. else
  47. setCanConnectNotion(true)
  48. }
  49. useEffect(() => {
  50. if (data && 'data' in data) {
  51. if (data.data && typeof data.data === 'string' && data.data.startsWith('http')) {
  52. window.location.href = data.data
  53. }
  54. else if (data.data === 'internal') {
  55. Toast.notify({
  56. type: 'info',
  57. message: t('common.dataSource.notion.integratedAlert'),
  58. })
  59. }
  60. }
  61. }, [data, t])
  62. return (
  63. <Panel
  64. type={DataSourceType.notion}
  65. isConfigured={connected}
  66. onConfigure={handleConnectNotion}
  67. readOnly={!isCurrentWorkspaceManager}
  68. isSupportList
  69. configuredList={workspaces.map(workspace => ({
  70. id: workspace.id,
  71. logo: ({ className }: { className: string }) => (
  72. <Icon
  73. src={workspace.source_info.workspace_icon!}
  74. name={workspace.source_info.workspace_name}
  75. className={className}
  76. />),
  77. name: workspace.source_info.workspace_name,
  78. isActive: workspace.is_bound,
  79. notionConfig: {
  80. total: workspace.source_info.total || 0,
  81. },
  82. }))}
  83. onRemove={noop} // handled in operation/index.tsx
  84. notionActions={{
  85. onChangeAuthorizedPage: handleAuthAgain,
  86. }}
  87. />
  88. )
  89. }
  90. export default React.memo(DataSourceNotion)