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.

1.3 KB
Raw Blame
import type { WorkflowOnlineUser, WorkflowOnlineUsersResponse } from '@/models/app'
import { useQuery } from '@tanstack/react-query'
import { consoleQuery } from '@/service/console'

type WorkflowOnlineUsersMap = Record<string, WorkflowOnlineUser[]>

type UseWorkflowOnlineUsersParams = {
  appIds: string[]
  enabled: boolean
}

const normalizeWorkflowOnlineUsers = (
  response?: WorkflowOnlineUsersResponse,
): WorkflowOnlineUsersMap => {
  const data = response?.data

  if (!data) return {}

  if (Array.isArray(data)) {
    return data.reduce<WorkflowOnlineUsersMap>((acc, item) => {
      if (item?.app_id) acc[item.app_id] = item.users || []
      return acc
    }, {})
  }

  return Object.entries(data).reduce<WorkflowOnlineUsersMap>((acc, [appId, users]) => {
    if (appId) acc[appId] = users || []
    return acc
  }, {})
}

export const useWorkflowOnlineUsers = ({ appIds, enabled }: UseWorkflowOnlineUsersParams) => {
  const shouldFetch = enabled && appIds.length > 0
  const { data: onlineUsersMap = {} } = useQuery(
    consoleQuery.apps.workflows.onlineUsers.post.queryOptions({
      input: { body: { app_ids: appIds } },
      enabled: shouldFetch,
      select: normalizeWorkflowOnlineUsers,
      refetchInterval: shouldFetch ? 10000 : false,
    }),
  )

  return {
    onlineUsersMap,
  }
}