Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(channels): fix optimistically insert attachments (#1302)

teo0fd0e2d27126
diff --git a/js/app/packages/block-channel/component/BaseInput.tsx b/js/app/packages/block-channel/component/BaseInput.tsx
index 1df3fc5bf1..7a82c60646 100644
--- a/js/app/packages/block-channel/component/BaseInput.tsx
+++ b/js/app/packages/block-channel/component/BaseInput.tsx
@@ -2,7 +2,6 @@ import {
isDraggingOverChannelSignal,
isValidChannelDragSignal,
} from '@block-channel/signal/attachment';
-import type { SendMessageArgs } from '@block-channel/signal/channel';
import { handleFileUpload } from '@block-channel/utils/inputAttachments';
import {
$convertSingleMentionToCard,
@@ -71,6 +70,7 @@ import { TaskPreviewPanel } from './TaskPreviewPanel';
import { useUserId } from '@core/context/user';
import { isMobile } from '@core/mobile/isMobile';
import { ENABLE_STATIC_DOCUMENT_CARDS } from '@core/constant/featureFlags';
+import type { SendMessageArgs } from '@block-channel/hooks/message';
false && fileFolderDrop;
diff --git a/js/app/packages/block-channel/component/ChannelInput.tsx b/js/app/packages/block-channel/component/ChannelInput.tsx
index 20528318f9..8e006de4d1 100644
--- a/js/app/packages/block-channel/component/ChannelInput.tsx
+++ b/js/app/packages/block-channel/component/ChannelInput.tsx
@@ -1,4 +1,3 @@
-import { useSendChannelMessageAction } from '@block-channel/signal/channel';
import {
clearDraftMessage,
loadDraftMessage,
@@ -15,6 +14,7 @@ import type { ChannelParticipant } from '@service-comms/generated/models/channel
import { createMemo, createSignal, onMount } from 'solid-js';
import type { SetStoreFunction } from 'solid-js/store';
import { BaseInput } from './BaseInput';
+import { useSendChannelMessage } from '@block-channel/hooks/message';
export type ChannelInputProps = {
channelId: string;
@@ -29,7 +29,7 @@ export type ChannelInputProps = {
};
export function ChannelInput(props: ChannelInputProps) {
- const sendMessage = useSendChannelMessageAction(() => props.channelId);
+ const sendMessage = useSendChannelMessage(() => props.channelId);
const typingMutation = usePostTypingUpdateMutation();
const channelUsers = createMemo<IUser[]>(() => {
diff --git a/js/app/packages/block-channel/component/ReplyInputsPortaler.tsx b/js/app/packages/block-channel/component/ReplyInputsPortaler.tsx
index 0447257891..956208ba39 100644
--- a/js/app/packages/block-channel/component/ReplyInputsPortaler.tsx
+++ b/js/app/packages/block-channel/component/ReplyInputsPortaler.tsx
@@ -1,8 +1,8 @@
import { useMessageListContext } from '@block-channel/component/MessageList/MessageList';
import {
type SendMessageArgs,
- useSendChannelMessageAction,
-} from '@block-channel/signal/channel';
+ useSendChannelMessage,
+} from '@block-channel/hooks/message';
import type { MessageWithThreadId } from '@block-channel/signal/threads';
import {
clearDraftMessage,
@@ -40,7 +40,7 @@ export type ReplyInputsPortalerProps = {
export function ReplyInputsPortaler(props: ReplyInputsPortalerProps) {
const listContext = useMessageListContext();
- const sendMessage = useSendChannelMessageAction(() => props.channelId);
+ const sendMessage = useSendChannelMessage(() => props.channelId);
const typingMutation = usePostTypingUpdateMutation();
const blockRef = blockElementSignal.get;
diff --git a/js/app/packages/block-channel/hooks/message.tsx b/js/app/packages/block-channel/hooks/message.tsx
index 6bbfcca5e4..1dafcc75ab 100644
--- a/js/app/packages/block-channel/hooks/message.tsx
+++ b/js/app/packages/block-channel/hooks/message.tsx
@@ -25,7 +25,7 @@ export type SendMessageArgs = {
mentions?: SimpleMention[];
};
-export function useSendChannelMessageAction(channelID: Accessor<string>) {
+export function useSendChannelMessage(channelID: Accessor<string>) {
const userId = useUserId();
const mutation = useSendMessageMutation({
diff --git a/js/app/packages/block-channel/signal/channel.ts b/js/app/packages/block-channel/signal/channel.ts
deleted file mode 100644
index 6bbfcca5e4..0000000000
--- a/js/app/packages/block-channel/signal/channel.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import {
- type InputAttachment,
- isStaticAttachmentType,
-} from '@core/store/cacheChannelInput';
-import { getImageDimensions, getVideoDimensions } from '@core/util/media';
-import { useSendMessageMutation } from '@queries/channel/message';
-import { invalidateListChannels } from '@queries/channel/channels';
-import type { NewAttachment } from '@service-comms/generated/models';
-import type { SimpleMention } from '@service-comms/generated/models/simpleMention';
-import { useUserId } from '@core/context/user';
-import { blockNameToItemType } from '@service-storage/client';
-import type { Accessor } from 'solid-js';
-
-function isMessageSendable(
- content: string | undefined,
- attachments: InputAttachment[]
-): boolean {
- return (content && content.trim().length > 0) || attachments.length > 0;
-}
-
-export type SendMessageArgs = {
- content: string | undefined;
- attachments: InputAttachment[];
- threadId?: string;
- mentions?: SimpleMention[];
-};
-
-export function useSendChannelMessageAction(channelID: Accessor<string>) {
- const userId = useUserId();
-
- const mutation = useSendMessageMutation({
- onSettled() {
- invalidateListChannels();
- },
- });
-
- return async ({
- content,
- attachments,
- threadId,
- mentions,
- }: SendMessageArgs) => {
- if (!userId) return;
- if (!isMessageSendable(content, attachments)) return;
-
- const channelId = channelID();
- const senderId = userId()!;
- const optimisticId = crypto.randomUUID();
-
- const attachmentsToSend = await Promise.allSettled(
- attachments.map(async (a) => {
- const attachmentType = isStaticAttachmentType(a.blockName)
- ? a.blockName
- : blockNameToItemType(a.blockName);
-
- if (!attachmentType) return;
-
- let attachment: NewAttachment = {
- entity_id: a.id,
- entity_type: attachmentType,
- };
-
- if (!a.file) return attachment;
-
- if (
- attachmentType !== 'static/image' &&
- attachmentType !== 'static/video'
- ) {
- return attachment;
- }
-
- const dimensions =
- attachmentType === 'static/image'
- ? await getImageDimensions(a.file)
- : await getVideoDimensions(a.file);
-
- attachment.width = dimensions.width;
- attachment.height = dimensions.height;
-
- return attachment;
- })
- );
-
- const filteredAttachements = attachmentsToSend
- .map((r) => (r.status === 'fulfilled' ? r.value : undefined))
- .filter((r) => r !== undefined);
-
- await mutation.mutateAsync({
- channelID: channelId,
- optimisticId,
- senderId,
- message: {
- attachments: filteredAttachements,
- content: content ?? '',
- thread_id: threadId,
- mentions: mentions ?? [],
- },
- });
- };
-}
diff --git a/js/app/packages/queries/channel/message.ts b/js/app/packages/queries/channel/message.ts
index e91e113458..b665208b3f 100644
--- a/js/app/packages/queries/channel/message.ts
+++ b/js/app/packages/queries/channel/message.ts
@@ -74,9 +74,18 @@ export function optimisticInsertChannelMessage(
edited_at: undefined,
};
+ const newAttachments: Attachment[] = vars.attachments.map((a) => ({
+ id: crypto.randomUUID(),
+ channel_id: vars.channelId,
+ created_at: String(Date.now()),
+ message_id: vars.optimisticId,
+ ...a,
+ }));
+
return {
...prev,
messages: [...prev.messages, newMessage],
+ attachments: [...prev.attachments, ...newAttachments],
};
}
);
@@ -101,6 +110,9 @@ export function rollbackInsertChannelMessage(
return {
...prev,
messages: prev.messages.filter((m) => m.id !== context.optimisticId),
+ attachments: prev.attachments.filter(
+ (a) => a.message_id !== context.optimisticId
+ ),
};
}
);
@@ -135,6 +147,11 @@ export function replaceOptimisticMessage(
return {
...prev,
messages: updatedMessages,
+ attachments: prev.attachments.map((a) =>
+ a.message_id === vars.optimisticId
+ ? { ...a, message_id: vars.realId }
+ : a
+ ),
};
}
);