Skip to content

Commit e5e600d

Browse files
authored
Merge pull request #1589 from anyproto/feature/JS-7643-preload-files
Feature/JS-7643: Preload files
2 parents 3e7d0a3 + 5044904 commit e5e600d

File tree

9 files changed

+60
-20
lines changed

9 files changed

+60
-20
lines changed

src/ts/component/block/chat/attachment/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const ChatAttachment = observer(forwardRef<RefProps, Props>((props, ref) => {
103103

104104
if (!src.current) {
105105
if (object.isTmp && object.file) {
106-
U.File.loadPreviewBase64(object.file, { type: 'jpg', quality: 95, maxWidth: I.ImageSize.Large }, (image: string) => {
106+
U.File.loadPreviewBase64(object.file, { type: 'jpg', quality: 99, maxWidth: I.ImageSize.Large }, (image: string) => {
107107
const node = $(nodeRef.current);
108108

109109
src.current = image;

src/ts/component/block/chat/form.tsx

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
4747
highlightMessage,
4848
} = props;
4949
const [ replyingId, setReplyingId ] = useState<string>('');
50-
const counters = S.Chat.getState(subId);
50+
const [ preloading, setPreloading ] = useState(new Map<string, string>());
5151
const nodeRef = useRef(null);
5252
const dummyRef = useRef(null);
5353
const editableRef = useRef(null);
@@ -62,6 +62,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
6262
const marks = useRef<I.Mark[]>([]);
6363
const editingId = useRef<string>('');
6464
const speedLimit = useRef({ last: 0, counter: 0 });
65+
const counters = S.Chat.getState(subId);
6566
const mentionCounter = counters.mentionCounter;
6667
const messageCounter = S.Chat.counterString(counters.messageCounter);
6768
const history = useRef({ position: -1, states: [] });
@@ -672,10 +673,33 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
672673
return;
673674
};
674675

675-
setAttachments([ ...list, ...attachments ]);
676+
list.forEach(item => {
677+
if (item.isTmp && U.Object.isFileLayout(item.layout) && item.path) {
678+
preloadFile(item);
679+
};
680+
});
681+
682+
saveState([ ...list, ...attachments ]);
676683
historySaveState();
677684
};
678685

686+
const preloadFile = (item: any) => {
687+
if (preloading.has(item.id)) {
688+
return;
689+
};
690+
691+
C.FileUpload(S.Common.space, '', item.path, I.FileType.None, {}, true, '', (message: any) => {
692+
if (message.error.code) {
693+
return;
694+
};
695+
696+
if (message.preloadFileId) {
697+
preloading.set(item.id, message.preloadFileId);
698+
setPreloading(preloading);
699+
};
700+
});
701+
};
702+
679703
const addBookmark = (url: string, fromText?: boolean) => {
680704
const add = (param: any) => {
681705
const { title, description, url } = param;
@@ -744,7 +768,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
744768
});
745769

746770
if (attachments.length != filtered.length) {
747-
setAttachments(filtered);
771+
saveState(filtered);
748772
};
749773
};
750774

@@ -826,7 +850,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
826850

827851
let n = 0;
828852
for (const item of files) {
829-
C.FileUpload(S.Common.space, '', item.path, I.FileType.None, {}, (message: any) => {
853+
C.FileUpload(S.Common.space, '', item.path, I.FileType.None, {}, false, preloading.get(item.id), (message: any) => {
830854
n++;
831855

832856
if (message.objectId) {
@@ -909,8 +933,9 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
909933
updateMarkup('', { from: 0, to: 0 });
910934
clearCounter();
911935
checkSendButton();
936+
saveState([]);
937+
setPreloading(new Map());
912938
checkTextMenu();
913-
setAttachments([]);
914939
};
915940

916941
const onReply = (message: I.ChatMessage) => {
@@ -1054,10 +1079,15 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
10541079
const value = getTextValue();
10551080
const list = (attachments || []).filter(it => it.id != id);
10561081

1082+
if (preloading.has(id)) {
1083+
C.FileDiscardPreload(preloading.get(id));
1084+
preloading.delete(id);
1085+
};
1086+
10571087
if (editingId.current && !value && !attachments.length) {
10581088
onDelete(editingId.current);
10591089
} else {
1060-
setAttachments(list);
1090+
saveState(list);
10611091
analytics.event('DetachItemChat');
10621092
};
10631093
};
@@ -1138,8 +1168,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
11381168
object.isTmp = true;
11391169
object.timestamp = U.Date.now();
11401170

1141-
attachments.unshift(object);
1142-
setAttachments(attachments);
1171+
saveState([ object ]);
11431172
});
11441173
break;
11451174
};
@@ -1261,7 +1290,6 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
12611290
};
12621291
});
12631292

1264-
setAttachments(list);
12651293
saveState(list);
12661294
});
12671295
};
@@ -1422,6 +1450,7 @@ const ChatForm = observer(forwardRef<RefProps, Props>((props, ref) => {
14221450
};
14231451

14241452
const saveState = (attachments?: any[]) => {
1453+
setAttachments(attachments);
14251454
Storage.setChat(rootId, {
14261455
text: getTextValue(),
14271456
marks: marks.current,

src/ts/component/block/cover.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ const BlockCover = observer(class BlockCover extends React.Component<I.BlockComp
474474
keyboard.disableCommonDrop(true);
475475
this.setLoading(true);
476476

477-
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, (message: any) => {
477+
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, false, '', (message: any) => {
478478
this.setLoading(false);
479479
keyboard.disableCommonDrop(false);
480480

src/ts/component/menu/block/cover.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ const MenuBlockCover = observer(class MenuBlockCover extends React.Component<I.M
274274
onUploadStart();
275275
};
276276

277-
C.FileUpload(S.Common.space, '', paths[0], I.FileType.Image, {}, (message: any) => {
277+
C.FileUpload(S.Common.space, '', paths[0], I.FileType.Image, {}, false, '', (message: any) => {
278278
if (message.error.code) {
279279
return;
280280
};
@@ -384,7 +384,7 @@ const MenuBlockCover = observer(class MenuBlockCover extends React.Component<I.M
384384
keyboard.disableCommonDrop(true);
385385
this.setState({ isLoading: true });
386386

387-
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, (message: any) => {
387+
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, false, '', (message: any) => {
388388
this.setState({ isLoading: false });
389389
keyboard.disableCommonDrop(false);
390390

@@ -414,7 +414,7 @@ const MenuBlockCover = observer(class MenuBlockCover extends React.Component<I.M
414414
return;
415415
};
416416

417-
C.FileUpload(S.Common.space, '', data.files[0].path, I.FileType.Image, {}, (message: any) => {
417+
C.FileUpload(S.Common.space, '', data.files[0].path, I.FileType.Image, {}, false, '', (message: any) => {
418418
if (!message.error.code) {
419419
U.Object.setCover(rootId, I.CoverType.Upload, message.objectId);
420420
};

src/ts/component/menu/dataview/file/list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ const MenuDataviewFileList = observer(class MenuDataviewFileList extends React.C
291291

292292
onUpload () {
293293
Action.openFileDialog({}, paths => {
294-
C.FileUpload(S.Common.space, '', paths[0], I.FileType.None, {}, (message: any) => {
294+
C.FileUpload(S.Common.space, '', paths[0], I.FileType.None, {}, false, '', (message: any) => {
295295
if (!message.error.code) {
296296
this.onChange(message.objectId);
297297
this.reload();

src/ts/component/menu/smile.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ const MenuSmile = observer(class MenuSmile extends React.Component<I.Menu, State
12171217
this.setLoading(true);
12181218
keyboard.disableCommonDrop(true);
12191219

1220-
C.FileUpload(this.getSpaceId(), '', file, I.FileType.Image, {}, (message: any) => {
1220+
C.FileUpload(this.getSpaceId(), '', file, I.FileType.Image, {}, false, '', (message: any) => {
12211221
this.setLoading(false);
12221222
keyboard.disableCommonDrop(false);
12231223

@@ -1237,7 +1237,7 @@ const MenuSmile = observer(class MenuSmile extends React.Component<I.Menu, State
12371237

12381238
this.setLoading(true);
12391239

1240-
C.FileUpload(this.getSpaceId(), '', paths[0], I.FileType.Image, {}, (message: any) => {
1240+
C.FileUpload(this.getSpaceId(), '', paths[0], I.FileType.Image, {}, false, '', (message: any) => {
12411241
this.setLoading(false);
12421242

12431243
if (!message.error.code) {

src/ts/component/page/elements/head/controls.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const Controls = observer(forwardRef<RefProps, Props>((props, ref) => {
115115
keyboard.disableCommonDrop(true);
116116
setIsLoading(true);
117117

118-
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, (message: any) => {
118+
C.FileUpload(S.Common.space, '', file, I.FileType.Image, {}, false, '', (message: any) => {
119119
setIsLoading(false);
120120
keyboard.disableCommonDrop(false);
121121

src/ts/lib/api/command.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ export const FileDrop = (contextId: string, targetId: string, position: I.BlockP
308308
dispatcher.request(FileDrop.name, request, callBack);
309309
};
310310

311-
export const FileUpload = (spaceId: string, url: string, path: string, type: I.FileType, details: any, callBack?: (message: any) => void) => {
312-
if (!url && !path) {
311+
export const FileUpload = (spaceId: string, url: string, path: string, type: I.FileType, details: any, preloadOnly: boolean, preloadFileId: string, callBack?: (message: any) => void) => {
312+
if (!url && !path && !preloadFileId) {
313313
return;
314314
};
315315

@@ -320,6 +320,8 @@ export const FileUpload = (spaceId: string, url: string, path: string, type: I.F
320320
request.setLocalpath(path);
321321
request.setType(type as number);
322322
request.setDetails(Encode.struct(details));
323+
request.setPreloadfileid(preloadFileId);
324+
request.setPreloadonly(preloadOnly);
323325

324326
dispatcher.request(FileUpload.name, request, callBack);
325327
};
@@ -351,6 +353,14 @@ export const FileReconcile = (callBack?: (message: any) => void) => {
351353
dispatcher.request(FileReconcile.name, new Empty(), callBack);
352354
};
353355

356+
export const FileDiscardPreload = (fileId: string, callBack?: (message: any) => void) => {
357+
const request = new Rpc.File.DiscardPreload.Request();
358+
359+
request.setFileid(fileId);
360+
361+
dispatcher.request(FileDiscardPreload.name, request, callBack);
362+
};
363+
354364
// ---------------------- NAVIGATION ---------------------- //
355365

356366
export const NavigationGetObjectInfoWithLinks = (pageId: string, callBack?: (message: any) => void) => {

src/ts/lib/api/response.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export const FileUpload = (response: Rpc.File.Upload.Response) => {
134134
return {
135135
objectId: response.getObjectid(),
136136
details: details(response),
137+
preloadFileId: response.getPreloadfileid(),
137138
};
138139
};
139140

0 commit comments

Comments
 (0)