Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.tsx 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. const Icon: FC<{
  13. src: string
  14. name: string
  15. className: string
  16. }> = ({ src, name, className }) => {
  17. return (
  18. <NotionIcon
  19. src={src}
  20. name={name}
  21. className={className}
  22. />
  23. )
  24. }
  25. type Props = {
  26. workspaces: TDataSourceNotion[]
  27. }
  28. const DataSourceNotion: FC<Props> = ({
  29. workspaces,
  30. }) => {
  31. const { isCurrentWorkspaceManager } = useAppContext()
  32. const [canConnectNotion, setCanConnectNotion] = useState(false)
  33. const { data } = useSWR(canConnectNotion ? '/oauth/data-source/notion' : null, fetchNotionConnection)
  34. const connected = !!workspaces.length
  35. const handleConnectNotion = () => {
  36. if (!isCurrentWorkspaceManager)
  37. return
  38. setCanConnectNotion(true)
  39. }
  40. const handleAuthAgain = () => {
  41. if (data?.data)
  42. window.location.href = data.data
  43. else
  44. setCanConnectNotion(true)
  45. }
  46. useEffect(() => {
  47. if (data?.data)
  48. window.location.href = data.data
  49. }, [data])
  50. return (
  51. <Panel
  52. type={DataSourceType.notion}
  53. isConfigured={connected}
  54. onConfigure={handleConnectNotion}
  55. readOnly={!isCurrentWorkspaceManager}
  56. isSupportList
  57. configuredList={workspaces.map(workspace => ({
  58. id: workspace.id,
  59. logo: ({ className }: { className: string }) => (
  60. <Icon
  61. src={workspace.source_info.workspace_icon!}
  62. name={workspace.source_info.workspace_name}
  63. className={className}
  64. />),
  65. name: workspace.source_info.workspace_name,
  66. isActive: workspace.is_bound,
  67. notionConfig: {
  68. total: workspace.source_info.total || 0,
  69. },
  70. }))}
  71. onRemove={noop} // handled in operation/index.tsx
  72. notionActions={{
  73. onChangeAuthorizedPage: handleAuthAgain,
  74. }}
  75. />
  76. )
  77. }
  78. export default React.memo(DataSourceNotion)