forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDescriptionRenderer.tsx
76 lines (69 loc) · 3.35 KB
/
useDescriptionRenderer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, { useState } from 'react';
import { EditableSchemaMetadata, SchemaField, SubResourceType } from '../../../../../../../types.generated';
import DescriptionField from '../../../../../dataset/profile/schema/components/SchemaDescriptionField';
import { pathMatchesExact } from '../../../../../dataset/profile/schema/utils/utils';
import { useUpdateDescriptionMutation } from '../../../../../../../graphql/mutations.generated';
import { useMutationUrn, useRefetch } from '../../../../../../entity/shared/EntityContext';
import { useSchemaRefetch } from '../SchemaContext';
import { sanitizeRichText } from '../../../Documentation/components/editor/utils';
import CompactMarkdownViewer from '../../../Documentation/components/CompactMarkdownViewer';
import useExtractFieldDescriptionInfo from './useExtractFieldDescriptionInfo';
export default function useDescriptionRenderer(
editableSchemaMetadata: EditableSchemaMetadata | null | undefined,
isCompact: boolean,
options?: {
handleShowMore?: (_: string) => void;
},
) {
const urn = useMutationUrn();
const refetch = useRefetch();
const schemaRefetch = useSchemaRefetch();
const [updateDescription] = useUpdateDescriptionMutation();
const [expandedRows, setExpandedRows] = useState({});
const extractFieldDescription = useExtractFieldDescriptionInfo(editableSchemaMetadata);
const refresh: any = () => {
refetch?.();
schemaRefetch?.();
};
return (description: string | undefined, record: SchemaField, index: number): JSX.Element => {
const editableFieldInfo = editableSchemaMetadata?.editableSchemaFieldInfo.find((candidateEditableFieldInfo) =>
pathMatchesExact(candidateEditableFieldInfo.fieldPath, record.fieldPath),
);
const { schemaFieldEntity } = record;
const { displayedDescription, sanitizedDescription, isPropagated, sourceDetail } = extractFieldDescription(
record,
description,
);
const original = record.description ? sanitizeRichText(record.description) : undefined;
const handleExpandedRows = (expanded) => setExpandedRows((prev) => ({ ...prev, [index]: expanded }));
if (isCompact) {
return <CompactMarkdownViewer content={displayedDescription} />;
}
return (
<DescriptionField
onExpanded={handleExpandedRows}
expanded={!!expandedRows[index]}
fieldPath={schemaFieldEntity?.fieldPath}
description={sanitizedDescription}
original={original}
isEdited={!!editableFieldInfo?.description}
onUpdate={(updatedDescription) =>
updateDescription({
variables: {
input: {
description: sanitizeRichText(updatedDescription),
resourceUrn: urn,
subResource: record.fieldPath,
subResourceType: SubResourceType.DatasetField,
},
},
}).then(refresh)
}
handleShowMore={options?.handleShowMore}
isReadOnly
isPropagated={isPropagated}
sourceDetail={sourceDetail}
/>
);
};
}