mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
refactor(web): improve document actions and metadata handling
This commit refactors the document actions and metadata components to enhance code clarity and maintainability. Key changes include: - Simplified the hook by removing unnecessary type assertions. - Streamlined the rendering logic in the component by consolidating JSX elements. - Updated dependencies in the hook to ensure proper reactivity. These adjustments aim to improve the overall structure and performance of the document management features.
This commit is contained in:
@ -73,7 +73,7 @@ export const useDocumentActions = ({
|
|||||||
return
|
return
|
||||||
|
|
||||||
const [e] = await asyncRunSafe<CommonResponse>(
|
const [e] = await asyncRunSafe<CommonResponse>(
|
||||||
opApi({ datasetId, documentIds: selectedIds }) as Promise<CommonResponse>,
|
opApi({ datasetId, documentIds: selectedIds }),
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!e) {
|
if (!e) {
|
||||||
|
|||||||
@ -37,7 +37,7 @@ export function useMetadataEditor({ docDetail }: UseMetadataEditorOptions) {
|
|||||||
metadata: (docDetail?.doc_metadata || {}) as Record<string, string>,
|
metadata: (docDetail?.doc_metadata || {}) as Record<string, string>,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, [docDetail?.doc_type, docDetail?.doc_metadata, doc_type])
|
}, [docDetail, doc_type])
|
||||||
|
|
||||||
const confirmDocType = useCallback(() => {
|
const confirmDocType = useCallback(() => {
|
||||||
if (!tempDocType)
|
if (!tempDocType)
|
||||||
|
|||||||
@ -73,17 +73,15 @@ const Metadata: FC<MetadataProps> = ({ docDetail, loading, onUpdate }) => {
|
|||||||
<>
|
<>
|
||||||
<TypeIcon iconName={metadataMap[metadataParams.documentType || 'book'].iconName || ''} className={s.iconShow} />
|
<TypeIcon iconName={metadataMap[metadataParams.documentType || 'book'].iconName || ''} className={s.iconShow} />
|
||||||
{metadataMap[metadataParams.documentType || 'book'].text}
|
{metadataMap[metadataParams.documentType || 'book'].text}
|
||||||
{editStatus && (
|
<div className="ml-1 inline-flex items-center gap-1">
|
||||||
<div className="ml-1 inline-flex items-center gap-1">
|
·
|
||||||
·
|
<div
|
||||||
<div
|
onClick={openDocTypeSelector}
|
||||||
onClick={openDocTypeSelector}
|
className="cursor-pointer hover:text-text-accent"
|
||||||
className="cursor-pointer hover:text-text-accent"
|
>
|
||||||
>
|
{t('operation.change', { ns: 'common' })}
|
||||||
{t('operation.change', { ns: 'common' })}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -77,19 +77,22 @@ vi.mock('@/app/components/workflow/constants', () => ({
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
describe('useDSL', () => {
|
describe('useDSL', () => {
|
||||||
let mockLink: { href: string, download: string, click: ReturnType<typeof vi.fn> }
|
let mockLink: { href: string, download: string, click: ReturnType<typeof vi.fn>, style: { display: string }, remove: ReturnType<typeof vi.fn> }
|
||||||
let originalCreateElement: typeof document.createElement
|
let originalCreateElement: typeof document.createElement
|
||||||
|
let originalAppendChild: typeof document.body.appendChild
|
||||||
let mockCreateObjectURL: ReturnType<typeof vi.spyOn>
|
let mockCreateObjectURL: ReturnType<typeof vi.spyOn>
|
||||||
let mockRevokeObjectURL: ReturnType<typeof vi.spyOn>
|
let mockRevokeObjectURL: ReturnType<typeof vi.spyOn>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
|
||||||
// Create a proper mock link element
|
// Create a proper mock link element with all required properties for downloadBlob
|
||||||
mockLink = {
|
mockLink = {
|
||||||
href: '',
|
href: '',
|
||||||
download: '',
|
download: '',
|
||||||
click: vi.fn(),
|
click: vi.fn(),
|
||||||
|
style: { display: '' },
|
||||||
|
remove: vi.fn(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save original and mock selectively - only intercept 'a' elements
|
// Save original and mock selectively - only intercept 'a' elements
|
||||||
@ -101,8 +104,13 @@ describe('useDSL', () => {
|
|||||||
return originalCreateElement(tagName)
|
return originalCreateElement(tagName)
|
||||||
}) as typeof document.createElement
|
}) as typeof document.createElement
|
||||||
|
|
||||||
mockCreateObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:test-url')
|
// Mock document.body.appendChild for downloadBlob
|
||||||
mockRevokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {})
|
originalAppendChild = document.body.appendChild.bind(document.body)
|
||||||
|
document.body.appendChild = vi.fn(<T extends Node>(node: T): T => node) as typeof document.body.appendChild
|
||||||
|
|
||||||
|
// downloadBlob uses window.URL, not URL
|
||||||
|
mockCreateObjectURL = vi.spyOn(window.URL, 'createObjectURL').mockReturnValue('blob:test-url')
|
||||||
|
mockRevokeObjectURL = vi.spyOn(window.URL, 'revokeObjectURL').mockImplementation(() => {})
|
||||||
|
|
||||||
// Default store state
|
// Default store state
|
||||||
mockWorkflowStoreGetState.mockReturnValue({
|
mockWorkflowStoreGetState.mockReturnValue({
|
||||||
@ -119,6 +127,7 @@ describe('useDSL', () => {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
document.createElement = originalCreateElement
|
document.createElement = originalCreateElement
|
||||||
|
document.body.appendChild = originalAppendChild
|
||||||
mockCreateObjectURL.mockRestore()
|
mockCreateObjectURL.mockRestore()
|
||||||
mockRevokeObjectURL.mockRestore()
|
mockRevokeObjectURL.mockRestore()
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
|||||||
Reference in New Issue
Block a user