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

index.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import type { FC } from 'react'
  2. import { memo, useEffect, useRef } from 'react'
  3. import { useNodes } from 'reactflow'
  4. import type { CommonNodeType } from '../types'
  5. import { Panel as NodePanel } from '../nodes'
  6. import { useStore } from '../store'
  7. import EnvPanel from './env-panel'
  8. import cn from '@/utils/classnames'
  9. export type PanelProps = {
  10. components?: {
  11. left?: React.ReactNode
  12. right?: React.ReactNode
  13. }
  14. }
  15. const Panel: FC<PanelProps> = ({
  16. components,
  17. }) => {
  18. const nodes = useNodes<CommonNodeType>()
  19. const selectedNode = nodes.find(node => node.data.selected)
  20. const showEnvPanel = useStore(s => s.showEnvPanel)
  21. const isRestoring = useStore(s => s.isRestoring)
  22. const rightPanelRef = useRef<HTMLDivElement>(null)
  23. const setRightPanelWidth = useStore(s => s.setRightPanelWidth)
  24. // get right panel width
  25. useEffect(() => {
  26. if (rightPanelRef.current) {
  27. const resizeRightPanelObserver = new ResizeObserver((entries) => {
  28. for (const entry of entries) {
  29. const { inlineSize } = entry.borderBoxSize[0]
  30. setRightPanelWidth(inlineSize)
  31. }
  32. })
  33. resizeRightPanelObserver.observe(rightPanelRef.current)
  34. return () => {
  35. resizeRightPanelObserver.disconnect()
  36. }
  37. }
  38. }, [setRightPanelWidth])
  39. const otherPanelRef = useRef<HTMLDivElement>(null)
  40. const setOtherPanelWidth = useStore(s => s.setOtherPanelWidth)
  41. // get other panel width
  42. useEffect(() => {
  43. if (otherPanelRef.current) {
  44. const resizeOtherPanelObserver = new ResizeObserver((entries) => {
  45. for (const entry of entries) {
  46. const { inlineSize } = entry.borderBoxSize[0]
  47. setOtherPanelWidth(inlineSize)
  48. }
  49. })
  50. resizeOtherPanelObserver.observe(otherPanelRef.current)
  51. return () => {
  52. resizeOtherPanelObserver.disconnect()
  53. }
  54. }
  55. }, [setOtherPanelWidth])
  56. return (
  57. <div
  58. ref={rightPanelRef}
  59. tabIndex={-1}
  60. className={cn('absolute bottom-1 right-0 top-14 z-10 flex outline-none')}
  61. key={`${isRestoring}`}
  62. >
  63. {
  64. components?.left
  65. }
  66. {
  67. !!selectedNode && (
  68. <NodePanel {...selectedNode!} />
  69. )
  70. }
  71. <div
  72. className='relative'
  73. ref={otherPanelRef}
  74. >
  75. {
  76. components?.right
  77. }
  78. {
  79. showEnvPanel && (
  80. <EnvPanel />
  81. )
  82. }
  83. </div>
  84. </div>
  85. )
  86. }
  87. export default memo(Panel)