Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.2 KB
Raw Blame
'use client'

import type { NotionPage } from '@/models/common'
import type { CrawlResultItem } from '@/models/datasets'
import { useCallback, useRef, useState } from 'react'

type PreviewState = {
  currentFile: File | undefined
  currentNotionPage: NotionPage | undefined
  currentWebsite: CrawlResultItem | undefined
}

type PreviewActions = {
  showFilePreview: (file: File) => void
  hideFilePreview: (restoreFocus?: boolean) => void
  showNotionPagePreview: (page: NotionPage) => void
  hideNotionPagePreview: () => void
  showWebsitePreview: (website: CrawlResultItem) => void
  hideWebsitePreview: () => void
}

type UsePreviewStateReturn = PreviewState & PreviewActions

/**
 * Custom hook for managing preview state across different data source types.
 * Handles file, notion page, and website preview visibility.
 */
function usePreviewState(): UsePreviewStateReturn {
  const filePreviewTrigger = useRef<HTMLElement | null>(null)
  const [currentFile, setCurrentFile] = useState<File | undefined>()
  const [currentNotionPage, setCurrentNotionPage] = useState<NotionPage | undefined>()
  const [currentWebsite, setCurrentWebsite] = useState<CrawlResultItem | undefined>()

  const showFilePreview = useCallback((file: File) => {
    filePreviewTrigger.current =
      document.activeElement instanceof HTMLElement ? document.activeElement : null
    setCurrentFile(file)
  }, [])

  const hideFilePreview = useCallback((restoreFocus = false) => {
    setCurrentFile(undefined)
    if (restoreFocus && filePreviewTrigger.current?.isConnected) filePreviewTrigger.current.focus()
    filePreviewTrigger.current = null
  }, [])

  const showNotionPagePreview = useCallback((page: NotionPage) => {
    setCurrentNotionPage(page)
  }, [])

  const hideNotionPagePreview = useCallback(() => {
    setCurrentNotionPage(undefined)
  }, [])

  const showWebsitePreview = useCallback((website: CrawlResultItem) => {
    setCurrentWebsite(website)
  }, [])

  const hideWebsitePreview = useCallback(() => {
    setCurrentWebsite(undefined)
  }, [])

  return {
    currentFile,
    currentNotionPage,
    currentWebsite,
    showFilePreview,
    hideFilePreview,
    showNotionPagePreview,
    hideNotionPagePreview,
    showWebsitePreview,
    hideWebsitePreview,
  }
}

export default usePreviewState