Skip to content

Commit 1fdcde3

Browse files
improve(uploadFiles): move upload files related props to its own oject
1 parent 0fecda5 commit 1fdcde3

File tree

9 files changed

+59
-45
lines changed

9 files changed

+59
-45
lines changed

datahub-web-react/src/alchemy-components/components/Editor/Editor.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ export const Editor = forwardRef((props: EditorProps, ref) => {
5454
dataTestId,
5555
onKeyDown,
5656
hideBorder,
57-
uploadFile,
58-
onFileUploadAttempt,
59-
onFileUploadFailed,
60-
onFileUploadSucceeded,
61-
onFileDownloadView,
57+
uploadFileProps,
6258
} = props;
6359
const { manager, state, getContext } = useRemirror({
6460
extensions: () => [
@@ -77,11 +73,7 @@ export const Editor = forwardRef((props: EditorProps, ref) => {
7773
new HistoryExtension({}),
7874
new HorizontalRuleExtension({}),
7975
new FileDragDropExtension({
80-
onFileUpload: uploadFile,
81-
onFileUploadAttempt,
82-
onFileUploadFailed,
83-
onFileUploadSucceeded,
84-
onFileDownloadView,
76+
uploadFileProps,
8577
}),
8678
new GapCursorExtension(), // required to allow cursor placement next to non-editable inline elements
8779
new ImageExtension({ enableResizing: !readOnly }),

datahub-web-react/src/alchemy-components/components/Editor/extensions/fileDragDrop/FileDragDropExtension.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ import {
3131
import { FileUploadFailureType, FileUploadSource } from '@components/components/Editor/types';
3232
import { notification } from '@components/components/Notification/notification';
3333

34-
interface FileDragDropOptions {
34+
interface FileUploadProps {
3535
onFileUpload?: (file: File) => Promise<string>;
36-
supportedTypes?: string[];
3736
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
3837
onFileUploadFailed?: (
3938
fileType: string,
@@ -46,6 +45,11 @@ interface FileDragDropOptions {
4645
onFileDownloadView?: (fileType: string, fileSize: number) => void;
4746
}
4847

48+
interface FileDragDropOptions {
49+
uploadFileProps?: FileUploadProps;
50+
supportedTypes?: string[];
51+
}
52+
4953
/**
5054
* The FileDragDrop extension allows users to drag and drop files into the editor.
5155
* It creates file nodes that render differently based on file type (images, PDFs, etc.)
@@ -141,12 +145,12 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
141145
// Process each file
142146
const fileArray = Array.from(files);
143147
const processPromises = fileArray.map(async (file) => {
144-
this.options.onFileUploadAttempt?.(file.type, file.size, 'drag-and-drop');
148+
this.options.uploadFileProps?.onFileUploadAttempt?.(file.type, file.size, 'drag-and-drop');
145149

146150
const validation = validateFile(file, { allowedTypes: supportedTypes });
147151
if (!validation.isValid) {
148152
console.error(validation.error);
149-
this.options.onFileUploadFailed?.(
153+
this.options.uploadFileProps?.onFileUploadFailed?.(
150154
file.type,
151155
file.size,
152156
'drag-and-drop',
@@ -181,14 +185,14 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
181185
view.dispatch(transaction);
182186

183187
// Upload file if handler is provided
184-
if (this.options.onFileUpload) {
188+
if (this.options.uploadFileProps?.onFileUpload) {
185189
try {
186-
const finalUrl = await this.options.onFileUpload(file);
190+
const finalUrl = await this.options.uploadFileProps.onFileUpload(file);
187191
this.updateNodeWithUrl(view, placeholderAttrs.id, finalUrl);
188-
this.options.onFileUploadSucceeded?.(file.type, file.size, 'drag-and-drop');
192+
this.options.uploadFileProps.onFileUploadSucceeded?.(file.type, file.size, 'drag-and-drop');
189193
} catch (uploadError) {
190194
console.error(uploadError);
191-
this.options.onFileUploadFailed?.(
195+
this.options.uploadFileProps.onFileUploadFailed?.(
192196
file.type,
193197
file.size,
194198
'drag-and-drop',
@@ -202,7 +206,7 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
202206
});
203207
}
204208
} else {
205-
this.options.onFileUploadFailed?.(
209+
this.options.uploadFileProps?.onFileUploadFailed?.(
206210
file.type,
207211
file.size,
208212
'drag-and-drop',
@@ -215,7 +219,7 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
215219
}
216220
} catch (error) {
217221
console.error(error);
218-
this.options.onFileUploadFailed?.(
222+
this.options.uploadFileProps?.onFileUploadFailed?.(
219223
file.type,
220224
file.size,
221225
'drag-and-drop',
@@ -382,7 +386,9 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
382386
ReactComponent: ComponentType<NodeViewComponentProps> = (props) => (
383387
<FileNodeView
384388
{...props}
385-
onFileDownloadView={(fileType, fileSize) => this.options.onFileDownloadView?.(fileType, fileSize)}
389+
onFileDownloadView={(fileType, fileSize) =>
390+
this.options.uploadFileProps?.onFileDownloadView?.(fileType, fileSize)
391+
}
386392
/>
387393
);
388394

@@ -405,12 +411,14 @@ const decoratedExt = extension<FileDragDropOptions>({
405411
handlerKeys: [],
406412
customHandlerKeys: [],
407413
defaultOptions: {
408-
onFileUpload: async (_file: File) => '',
414+
uploadFileProps: {
415+
onFileUpload: async (_file: File) => '',
416+
onFileUploadAttempt: () => {},
417+
onFileUploadFailed: () => {},
418+
onFileUploadSucceeded: () => {},
419+
onFileDownloadView: () => {},
420+
},
409421
supportedTypes: SUPPORTED_FILE_TYPES,
410-
onFileUploadAttempt: () => {},
411-
onFileUploadFailed: () => {},
412-
onFileUploadSucceeded: () => {},
413-
onFileDownloadView: () => {},
414422
},
415423
})(FileDragDropExtension);
416424

datahub-web-react/src/alchemy-components/components/Editor/toolbar/FileUploadButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const FileUploadButton = () => {
2626
const [showDropdown, setShowDropdown] = useState(false);
2727

2828
// Hide the button when uploading of files is disabled
29-
if (!fileExtension.options.onFileUpload) return null;
29+
if (!fileExtension.options.uploadFileProps?.onFileUpload) return null;
3030

3131
return (
3232
<Dropdown

datahub-web-react/src/alchemy-components/components/Editor/toolbar/FileUploadContent.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export const FileUploadContent = ({ hideDropdown }: Props) => {
5454
if (files.length === 0) return;
5555

5656
const supportedTypes = SUPPORTED_FILE_TYPES;
57-
const { onFileUpload, onFileUploadAttempt, onFileUploadFailed, onFileUploadSucceeded } = fileExtension.options;
57+
const { onFileUpload, onFileUploadAttempt, onFileUploadFailed, onFileUploadSucceeded } =
58+
fileExtension.options.uploadFileProps || {};
5859

5960
try {
6061
// Process files concurrently

datahub-web-react/src/alchemy-components/components/Editor/toolbar/Toolbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const Toolbar = ({ styles }: Props) => {
6767
const remirrorContext = useRemirrorContext();
6868
const fileExtension = remirrorContext.getExtension(FileDragDropExtension);
6969

70-
const shouldShowImageButtonV2 = documentationFileUploadV1 && fileExtension.options.onFileUpload;
70+
const shouldShowImageButtonV2 = documentationFileUploadV1 && fileExtension.options.uploadFileProps?.onFileUpload;
7171

7272
return (
7373
<Container style={styles}>

datahub-web-react/src/alchemy-components/components/Editor/types.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ export enum FileUploadFailureType {
77
UNKNOWN = 'unknown',
88
}
99

10+
export interface FileUploadProps {
11+
onFileUpload?: (file: File) => Promise<string>;
12+
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
13+
onFileUploadFailed?: (
14+
fileType: string,
15+
fileSize: number,
16+
source: FileUploadSource,
17+
failureType: FileUploadFailureType,
18+
comment?: string,
19+
) => void;
20+
onFileUploadSucceeded?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
21+
onFileDownloadView?: (fileType: string, fileSize: number) => void;
22+
}
23+
1024
export type EditorProps = {
1125
readOnly?: boolean;
1226
content?: string;
@@ -19,15 +33,5 @@ export type EditorProps = {
1933
dataTestId?: string;
2034
onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
2135
hideBorder?: boolean;
22-
uploadFile?: (file: File) => Promise<string>;
23-
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
24-
onFileUploadFailed?: (
25-
fileType: string,
26-
fileSize: number,
27-
source: FileUploadSource,
28-
failureType: FileUploadFailureType,
29-
comment?: string,
30-
) => void;
31-
onFileUploadSucceeded?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
32-
onFileDownloadView?: (fileType: string, fileSize: number) => void;
36+
uploadFileProps?: FileUploadProps;
3337
};

datahub-web-react/src/app/entityV2/shared/tabs/Dataset/Schema/components/SchemaFieldDrawer/AboutFieldTab.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ export function AboutFieldTab({ properties }: AboutFieldTabProps) {
103103
<FieldDescription
104104
expandedField={expandedField}
105105
editableFieldInfo={editableFieldInfo}
106-
editorProps={{ uploadFile, ...uploadFileAnalyticsCallbacks }}
106+
editorProps={{
107+
uploadFileProps: {
108+
onFileUpload: uploadFile,
109+
...uploadFileAnalyticsCallbacks,
110+
},
111+
}}
107112
/>
108113
<FieldTags
109114
expandedField={expandedField}

datahub-web-react/src/app/entityV2/shared/tabs/Documentation/components/DescriptionEditor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ export const DescriptionEditor = ({ onComplete }: DescriptionEditorProps) => {
214214
content={updatedDescription}
215215
onChange={handleEditorChange}
216216
placeholder="Describe this asset to make it more discoverable. Tag @user or reference @asset to make your docs come to life!"
217-
uploadFile={uploadFile}
218-
{...uploadFileAnalyticsCallbacks}
217+
uploadFileProps={{
218+
onFileUpload: uploadFile,
219+
...uploadFileAnalyticsCallbacks,
220+
}}
219221
hideBorder
220222
/>
221223
</EditorContainer>

datahub-web-react/src/app/entityV2/summary/documentation/EditDescriptionModal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ export default function EditDescriptionModal({
8989
onChange={(description) => setUpdatedDescription(description)}
9090
toolbarStyles={toolbarStyles}
9191
dataTestId="description-editor"
92-
uploadFile={uploadFile}
93-
{...uploadFileAnalyticsCallbacks}
92+
uploadFileProps={{
93+
onFileUpload: uploadFile,
94+
...uploadFileAnalyticsCallbacks,
95+
}}
9496
/>
9597
</Modal>
9698
);

0 commit comments

Comments
 (0)