mirror of
https://github.com/langgenius/dify.git
synced 2026-04-15 01:38:19 +08:00
feat: use query to handle api and remove old api service
This commit is contained in:
parent
6e38b523c9
commit
6748b50bf1
@ -33,7 +33,6 @@ import type { ColorMap, IndicatorProps } from '@/app/components/header/indicator
|
||||
import Indicator from '@/app/components/header/indicator'
|
||||
import { asyncRunSafe } from '@/utils'
|
||||
import { formatNumber } from '@/utils/format'
|
||||
import { deleteDocument, disableDocument, enableDocument, syncDocument, syncWebsite, unArchiveDocument } from '@/service/datasets'
|
||||
import NotionIcon from '@/app/components/base/notion-icon'
|
||||
import ProgressBar from '@/app/components/base/progress-bar'
|
||||
import { ChuckingMode, DataSourceType, type DocumentDisplayStatus, type SimpleDocumentDetail } from '@/models/datasets'
|
||||
@ -43,8 +42,7 @@ import { useDatasetDetailContextWithSelector as useDatasetDetailContext } from '
|
||||
import type { Props as PaginationProps } from '@/app/components/base/pagination'
|
||||
import Pagination from '@/app/components/base/pagination'
|
||||
import Checkbox from '@/app/components/base/checkbox'
|
||||
import { useDocumentBatchAction } from '@/service/knowledge/use-document'
|
||||
import { BatchActionType } from '@/models/datasets'
|
||||
import { useDocumentArchive, useDocumentDelete, useDocumentDisable, useDocumentEnable, useDocumentUnArchive, useSyncDocument, useSyncWebsite } from '@/service/knowledge/use-document'
|
||||
|
||||
export const useIndexStatus = () => {
|
||||
const { t } = useTranslation()
|
||||
@ -90,34 +88,18 @@ export const StatusItem: FC<{
|
||||
const { enabled = false, archived = false, id = '' } = detail || {}
|
||||
const { notify } = useContext(ToastContext)
|
||||
const { t } = useTranslation()
|
||||
const { mutateAsync: documentBatchActon } = useDocumentBatchAction()
|
||||
const { mutateAsync: enableDocument } = useDocumentEnable()
|
||||
const { mutateAsync: disableDocument } = useDocumentDisable()
|
||||
const { mutateAsync: deleteDocument } = useDocumentDelete()
|
||||
|
||||
const onOperate = async (operationName: OperationName) => {
|
||||
let opApi = deleteDocument
|
||||
switch (operationName) {
|
||||
case 'enable':
|
||||
opApi = async ({
|
||||
datasetId,
|
||||
documentId,
|
||||
}) => {
|
||||
return documentBatchActon({
|
||||
action: BatchActionType.enable,
|
||||
datasetId,
|
||||
documentIds: [documentId],
|
||||
})
|
||||
}
|
||||
opApi = enableDocument
|
||||
break
|
||||
case 'disable':
|
||||
opApi = async ({
|
||||
datasetId,
|
||||
documentId,
|
||||
}) => {
|
||||
return documentBatchActon({
|
||||
action: BatchActionType.disable,
|
||||
datasetId,
|
||||
documentIds: [documentId],
|
||||
})
|
||||
}
|
||||
opApi = disableDocument
|
||||
break
|
||||
}
|
||||
const [e] = await asyncRunSafe<CommonResponse>(opApi({ datasetId, documentId: id }) as Promise<CommonResponse>)
|
||||
@ -201,24 +183,20 @@ export const OperationAction: FC<{
|
||||
const { notify } = useContext(ToastContext)
|
||||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
const { mutateAsync: documentBatchActon } = useDocumentBatchAction()
|
||||
|
||||
const { mutateAsync: archiveDocument } = useDocumentArchive()
|
||||
const { mutateAsync: unArchiveDocument } = useDocumentUnArchive()
|
||||
const { mutateAsync: enableDocument } = useDocumentEnable()
|
||||
const { mutateAsync: disableDocument } = useDocumentDisable()
|
||||
const { mutateAsync: deleteDocument } = useDocumentDelete()
|
||||
const { mutateAsync: syncDocument } = useSyncDocument()
|
||||
const { mutateAsync: syncWebsite } = useSyncWebsite()
|
||||
const isListScene = scene === 'list'
|
||||
|
||||
const onOperate = async (operationName: OperationName) => {
|
||||
let opApi = deleteDocument
|
||||
switch (operationName) {
|
||||
case 'archive':
|
||||
opApi = async ({
|
||||
datasetId,
|
||||
documentId,
|
||||
}) => {
|
||||
return documentBatchActon({
|
||||
action: BatchActionType.archive,
|
||||
datasetId,
|
||||
documentIds: [documentId],
|
||||
})
|
||||
}
|
||||
opApi = archiveDocument
|
||||
break
|
||||
case 'un_archive':
|
||||
opApi = unArchiveDocument
|
||||
@ -232,10 +210,8 @@ export const OperationAction: FC<{
|
||||
case 'sync':
|
||||
if (data_source_type === 'notion_import')
|
||||
opApi = syncDocument
|
||||
|
||||
else
|
||||
opApi = syncWebsite
|
||||
|
||||
break
|
||||
default:
|
||||
opApi = deleteDocument
|
||||
|
||||
@ -625,10 +625,11 @@ export enum BatchActionType {
|
||||
enable = 'enable',
|
||||
disable = 'disable',
|
||||
archive = 'archive',
|
||||
unArchive = 'un_archive',
|
||||
}
|
||||
|
||||
export type UpdateDocumentBatchParams = {
|
||||
datasetId: string
|
||||
documentIds: string[]
|
||||
action: BatchActionType
|
||||
documentId?: string
|
||||
documentIds?: string[] | string
|
||||
}
|
||||
|
||||
@ -171,34 +171,6 @@ export const resumeDocIndexing: Fetcher<CommonResponse, CommonDocReq> = ({ datas
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/processing/resume`)
|
||||
}
|
||||
|
||||
export const deleteDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return del<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}`)
|
||||
}
|
||||
|
||||
export const archiveDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/archive`)
|
||||
}
|
||||
|
||||
export const unArchiveDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/un_archive`)
|
||||
}
|
||||
|
||||
export const enableDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/enable`)
|
||||
}
|
||||
|
||||
export const disableDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/status/disable`)
|
||||
}
|
||||
|
||||
export const syncDocument: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/notion/sync`)
|
||||
}
|
||||
|
||||
export const syncWebsite: Fetcher<CommonResponse, CommonDocReq> = ({ datasetId, documentId }) => {
|
||||
return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/website-sync`)
|
||||
}
|
||||
|
||||
export const preImportNotionPages: Fetcher<{ notion_info: DataSourceNotionWorkspace[] }, { url: string; datasetId?: string }> = ({ url, datasetId }) => {
|
||||
return get<{ notion_info: DataSourceNotionWorkspace[] }>(url, { params: { dataset_id: datasetId } })
|
||||
}
|
||||
|
||||
@ -2,8 +2,9 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from '@tanstack/react-query'
|
||||
import { get, patch } from '../base'
|
||||
import { del, get, patch } from '../base'
|
||||
import type { SimpleDocumentDetail, UpdateDocumentBatchParams } from '@/models/datasets'
|
||||
import { BatchActionType } from '@/models/datasets'
|
||||
import type { CommonResponse } from '@/models/common'
|
||||
|
||||
const NAME_SPACE = 'knowledge/document'
|
||||
@ -26,14 +27,55 @@ export const useDocumentList = (payload: {
|
||||
})
|
||||
}
|
||||
|
||||
const toBatchDocumentsIdParams = (documentIds: string[]) => {
|
||||
return documentIds.map(id => `document_id=${id}`).join('=')
|
||||
const toBatchDocumentsIdParams = (documentIds: string[] | string) => {
|
||||
const ids = Array.isArray(documentIds) ? documentIds : [documentIds]
|
||||
return ids.map(id => `document_id=${id}`).join('=')
|
||||
}
|
||||
|
||||
export const useDocumentBatchAction = () => {
|
||||
export const useDocumentBatchAction = (action: BatchActionType) => {
|
||||
return useMutation({
|
||||
mutationFn: ({ action, datasetId, documentIds }: UpdateDocumentBatchParams) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/status/${action}?${toBatchDocumentsIdParams(documentIds)}`)
|
||||
mutationFn: ({ datasetId, documentIds, documentId }: UpdateDocumentBatchParams) => {
|
||||
return patch<CommonResponse>(`/datasets/${datasetId}/documents/status/${action}?${toBatchDocumentsIdParams(documentId || documentIds!)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useDocumentEnable = () => {
|
||||
return useDocumentBatchAction(BatchActionType.enable)
|
||||
}
|
||||
|
||||
export const useDocumentDisable = () => {
|
||||
return useDocumentBatchAction(BatchActionType.disable)
|
||||
}
|
||||
|
||||
export const useDocumentArchive = () => {
|
||||
return useDocumentBatchAction(BatchActionType.archive)
|
||||
}
|
||||
|
||||
export const useDocumentUnArchive = () => {
|
||||
return useDocumentBatchAction(BatchActionType.unArchive)
|
||||
}
|
||||
|
||||
export const useDocumentDelete = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({ datasetId, documentIds, documentId }: UpdateDocumentBatchParams) => {
|
||||
return del<CommonResponse>(`/datasets/${datasetId}/documents?${toBatchDocumentsIdParams(documentId || documentIds!)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useSyncDocument = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({ datasetId, documentId }: UpdateDocumentBatchParams) => {
|
||||
return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/notion/sync`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useSyncWebsite = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({ datasetId, documentId }: UpdateDocumentBatchParams) => {
|
||||
return get<CommonResponse>(`/datasets/${datasetId}/documents/${documentId}/website-sync`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user