1b9f63b3ad
## Context UploadFile now returns the file token which we don't want when we save the attachment in the DB due to its non-persistence so now the FE removes the token query param before saving the attachment. This is most likely not the right way to do it, we will need to refactor this part before. Also made sure we don't save the token in the DB for rich_text as well and remove it before saving.
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { useRecoilValue } from 'recoil';
|
|
|
|
import { Attachment } from '@/activities/files/types/Attachment';
|
|
import { getFileType } from '@/activities/files/utils/getFileType';
|
|
import { ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity';
|
|
import { getActivityTargetObjectFieldIdName } from '@/activities/utils/getActivityTargetObjectFieldIdName';
|
|
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
|
|
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
|
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
|
import { FileFolder, useUploadFileMutation } from '~/generated/graphql';
|
|
|
|
// Note: This is probably not the right way to do this.
|
|
export const computePathWithoutToken = (attachmentPath: string): string => {
|
|
return attachmentPath.replace(/\?token=[^&]*$/, '');
|
|
};
|
|
|
|
export const useUploadAttachmentFile = () => {
|
|
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
|
|
const [uploadFile] = useUploadFileMutation();
|
|
|
|
const { createOneRecord: createOneAttachment } =
|
|
useCreateOneRecord<Attachment>({
|
|
objectNameSingular: CoreObjectNameSingular.Attachment,
|
|
});
|
|
|
|
const uploadAttachmentFile = async (
|
|
file: File,
|
|
targetableObject: ActivityTargetableObject,
|
|
) => {
|
|
const result = await uploadFile({
|
|
variables: {
|
|
file,
|
|
fileFolder: FileFolder.Attachment,
|
|
},
|
|
});
|
|
|
|
const attachmentPath = result?.data?.uploadFile;
|
|
|
|
if (!attachmentPath) {
|
|
return;
|
|
}
|
|
|
|
const targetableObjectFieldIdName = getActivityTargetObjectFieldIdName({
|
|
nameSingular: targetableObject.targetObjectNameSingular,
|
|
});
|
|
|
|
const attachmentToCreate = {
|
|
authorId: currentWorkspaceMember?.id,
|
|
name: file.name,
|
|
fullPath: computePathWithoutToken(attachmentPath),
|
|
type: getFileType(file.name),
|
|
[targetableObjectFieldIdName]: targetableObject.id,
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
} as Partial<Attachment>;
|
|
|
|
await createOneAttachment(attachmentToCreate);
|
|
};
|
|
|
|
return { uploadAttachmentFile };
|
|
};
|