index.tsx 2.2 KB

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