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.4 KB
Raw Blame
import type { ReactNode } from 'react'
import { ORPCError } from '@orpc/client'
import { dehydrate, HydrationBoundary, noop } from '@tanstack/react-query'
import { getQueryClient } from '@/app/get-query-client'
import {
  redirectForBootstrapError,
  serverUserProfileQueryOptions,
} from '@/features/account-profile/server'
import { headers } from '@/next/headers'
import { redirect } from '@/next/navigation'
import { consoleQuery } from '@/service/console'
import { resolveServerConsoleApiUrl } from '@/service/console/server'

const CURRENT_PATHNAME_HEADER = 'x-dify-pathname'
const CURRENT_SEARCH_HEADER = 'x-dify-search'
const ACCOUNT_PROFILE_PATH = '/account/profile'
const AUTH_REFRESH_PATH = '/auth/refresh'

const getCurrentPath = async () => {
  const requestHeaders = await headers()
  const pathname = requestHeaders.get(CURRENT_PATHNAME_HEADER) || '/'
  const search = requestHeaders.get(CURRENT_SEARCH_HEADER) || ''
  return `${pathname}${search}`
}

const redirectToAuthRefresh = async () => {
  const currentPath = await getCurrentPath()
  redirect(`${AUTH_REFRESH_PATH}?redirect_url=${encodeURIComponent(currentPath)}`)
}

const handleProfileError = async (error: unknown) => {
  if (!(error instanceof ORPCError)) throw error

  redirectForBootstrapError(error)
  if (error.status === 401) await redirectToAuthRefresh()

  throw error
}

export async function CommonLayoutHydrationBoundary({ children }: { children: ReactNode }) {
  const queryClient = getQueryClient()
  const accountProfileUrl = resolveServerConsoleApiUrl(ACCOUNT_PROFILE_PATH)

  if (accountProfileUrl) {
    try {
      await Promise.all([
        queryClient.query(serverUserProfileQueryOptions()),
        queryClient
          .query(
            consoleQuery.workspaces.current.summary.get.queryOptions({
              retry: false,
            }),
          )
          .catch(noop),
        queryClient
          .query(
            consoleQuery.workspaces.current.rbac.myPermissions.get.queryOptions({
              retry: false,
            }),
          )
          .catch(noop),
        queryClient
          .query(
            consoleQuery.features.get.queryOptions({
              retry: false,
            }),
          )
          .catch(noop),
      ])
    } catch (error) {
      await handleProfileError(error)
    }
  }

  return <HydrationBoundary state={dehydrate(queryClient)}>{children}</HydrationBoundary>
}