Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor message queries, mutations and components

Hakan Shehu9d6d544faaab
diff --git a/apps/desktop/src/main/mutations/message-create.ts b/apps/desktop/src/main/mutations/message-create.ts
index a95b7109..9681ec99 100644
--- a/apps/desktop/src/main/mutations/message-create.ts
+++ b/apps/desktop/src/main/mutations/message-create.ts
@@ -4,7 +4,7 @@ import {
MessageCreateMutationInput,
MessageCreateMutationOutput,
} from '@/shared/mutations/message-create';
-import { mapContentsToBlocks } from '@/shared/lib/editor';
+import { mapBlocksToContents, mapContentsToBlocks } from '@/shared/lib/editor';
import { fileService } from '@/main/services/file-service';
import { Block, FileAttributes, MessageAttributes } from '@colanode/core';
import { CreateNodeInput, nodeService } from '@/main/services/node-service';
@@ -17,13 +17,35 @@ export class MessageCreateMutationHandler
): Promise<MessageCreateMutationOutput> {
const inputs: CreateNodeInput[] = [];
+ const messageContent = input.content.content ?? [];
+ if (input.referenceId) {
+ const referenceNode = await nodeService.fetchNode(
+ input.referenceId,
+ input.userId
+ );
+
+ if (!referenceNode || referenceNode.type !== 'message') {
+ throw new Error('Reference node not found');
+ }
+
+ const referenceContent = mapBlocksToContents(
+ referenceNode.id,
+ Object.values(referenceNode.attributes.content)
+ );
+
+ messageContent.unshift({
+ type: 'messageReference',
+ attrs: {
+ id: input.referenceId,
+ conversationId: input.conversationId,
+ },
+ content: referenceContent,
+ });
+ }
+
const messageId = generateId(IdType.Message);
const createdAt = new Date().toISOString();
- const blocks = mapContentsToBlocks(
- messageId,
- input.content.content ?? [],
- new Map()
- );
+ const blocks = mapContentsToBlocks(messageId, messageContent, new Map());
// check if there are nested nodes (files, pages, folders etc.)
for (const block of blocks) {
diff --git a/apps/desktop/src/main/queries/accounts-list.ts b/apps/desktop/src/main/queries/accounts-list.ts
index 7e5f9b3b..0fd6ca00 100644
--- a/apps/desktop/src/main/queries/accounts-list.ts
+++ b/apps/desktop/src/main/queries/accounts-list.ts
@@ -27,7 +27,7 @@ export class AccountListQueryHandler
}
if (event.type === 'account_updated') {
- const updatedAccounts = output.map((account) => {
+ const updatedAccounts = [...output].map((account) => {
if (account.id === event.account.id) {
return event.account;
}
@@ -41,7 +41,7 @@ export class AccountListQueryHandler
}
if (event.type === 'account_deleted') {
- const activeAccounts = output.filter(
+ const activeAccounts = [...output].filter(
(account) => account.id !== event.account.id
);
diff --git a/apps/desktop/src/main/queries/file-list.ts b/apps/desktop/src/main/queries/file-list.ts
index 3c8d538d..dfd91f50 100644
--- a/apps/desktop/src/main/queries/file-list.ts
+++ b/apps/desktop/src/main/queries/file-list.ts
@@ -56,7 +56,8 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {
event.node.type === 'file' &&
event.node.parentId === input.parentId
) {
- const file = output.find((file) => file.id === event.node.id);
+ const newOutput = [...output];
+ const file = newOutput.find((file) => file.id === event.node.id);
if (file) {
file.name = event.node.attributes.name;
file.mimeType = event.node.attributes.mimeType;
@@ -66,7 +67,7 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {
return {
hasChanges: true,
- result: output,
+ result: newOutput,
};
}
}
@@ -79,10 +80,10 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {
) {
const file = output.find((file) => file.id === event.node.id);
if (file) {
- output = output.filter((file) => file.id !== event.node.id);
+ const newOutput = output.filter((file) => file.id !== event.node.id);
return {
hasChanges: true,
- result: output,
+ result: newOutput,
};
}
}
@@ -92,13 +93,14 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {
event.type === 'download_updated') &&
event.userId === input.userId
) {
+ const newOutput = [...output];
const nodeId = event.download.nodeId;
- const file = output.find((file) => file.id === nodeId);
+ const file = newOutput.find((file) => file.id === nodeId);
if (file) {
file.downloadProgress = event.download.progress;
return {
hasChanges: true,
- result: output,
+ result: newOutput,
};
}
}
diff --git a/apps/desktop/src/main/queries/message-list.ts b/apps/desktop/src/main/queries/message-list.ts
index 071184e5..a950e745 100644
--- a/apps/desktop/src/main/queries/message-list.ts
+++ b/apps/desktop/src/main/queries/message-list.ts
@@ -2,15 +2,9 @@ import { MessageListQueryInput } from '@/shared/queries/message-list';
import { databaseService } from '@/main/data/database-service';
import { ChangeCheckResult, QueryHandler } from '@/main/types';
import { SelectNode } from '@/main/data/workspace/schema';
-import { MessageAttributes, NodeTypes } from '@colanode/core';
-import {
- MessageNode,
- MessageReactionCount,
- MessageAuthor,
-} from '@/shared/types/messages';
+import { MessageNode, NodeTypes } from '@colanode/core';
import { mapNode } from '@/main/utils';
import { compareString } from '@/shared/lib/utils';
-import { mapBlocksToContents } from '@/shared/lib/editor';
import { Event } from '@/shared/types/events';
export class MessageListQueryHandler
@@ -20,9 +14,7 @@ export class MessageListQueryHandler
input: MessageListQueryInput
): Promise<MessageNode[]> {
const messages = await this.fetchMesssages(input);
- const authors = await this.fetchAuthors(input, messages);
-
- return this.buildMessages(input.userId, messages, authors);
+ return this.buildMessages(messages);
}
public async checkForChanges(
@@ -46,77 +38,50 @@ export class MessageListQueryHandler
event.node.type === 'message' &&
event.node.parentId === input.conversationId
) {
- const messages = await this.fetchMesssages(input);
- const authors = await this.fetchAuthors(input, messages);
+ const newResult = await this.handleQuery(input);
return {
hasChanges: true,
- result: this.buildMessages(input.userId, messages, authors),
+ result: newResult,
};
}
- if (event.type === 'node_updated' && event.userId === input.userId) {
- if (
- event.node.type === 'message' &&
- event.node.parentId === input.conversationId
- ) {
- const message = output.find((message) => message.id === event.node.id);
- if (message) {
- message.content = mapBlocksToContents(
- event.node.id,
- Object.values(event.node.attributes.content ?? {})
- );
-
- message.reactionCounts = this.buildReactionCounts(
- event.node.attributes,
- input.userId
- );
-
- message.createdAt = event.node.createdAt;
- message.versionId = event.node.versionId;
-
- return {
- hasChanges: true,
- result: output,
- };
- }
- }
-
- if (event.node.type === 'user') {
- let hasChanges = false;
- for (const message of output) {
- if (message.author.id === event.node.id) {
- message.author.name = event.node.attributes.name;
- message.author.email = event.node.attributes.email;
- message.author.avatar = event.node.attributes.avatar;
-
- hasChanges = true;
+ if (
+ event.type === 'node_updated' &&
+ event.userId === input.userId &&
+ event.node.type === 'message' &&
+ event.node.parentId === input.conversationId
+ ) {
+ const message = output.find((message) => message.id === event.node.id);
+ if (message) {
+ const newResult = output.map((message) => {
+ if (message.id === event.node.id) {
+ return event.node as MessageNode;
}
- }
-
- if (hasChanges) {
- return {
- hasChanges: true,
- result: output,
- };
- }
+ return message;
+ });
+
+ return {
+ hasChanges: true,
+ result: newResult,
+ };
}
}
- if (event.type === 'node_deleted' && event.userId === input.userId) {
- if (
- event.node.type === 'message' &&
- event.node.parentId === input.conversationId
- ) {
- const message = output.find((message) => message.id === event.node.id);
-
- if (message) {
- const newOutput = await this.handleQuery(input);
- return {
- hasChanges: true,
- result: newOutput,
- };
- }
+ if (
+ event.type === 'node_deleted' &&
+ event.userId === input.userId &&
+ event.node.type === 'message' &&
+ event.node.parentId === input.conversationId
+ ) {
+ const message = output.find((message) => message.id === event.node.id);
+
+ if (message) {
+ const newOutput = await this.handleQuery(input);
+ return {
+ hasChanges: true,
+ result: newOutput,
+ };
}
}
@@ -150,99 +115,18 @@ export class MessageListQueryHandler
return messages;
}
- private async fetchAuthors(
- input: MessageListQueryInput,
- messages: SelectNode[]
- ): Promise<SelectNode[]> {
- if (messages.length === 0) {
- return [];
- }
-
- const workspaceDatabase = await databaseService.getWorkspaceDatabase(
- input.userId
- );
-
- const authorIds = messages.map((message) => message.created_by);
- const authors = await workspaceDatabase
- .selectFrom('nodes')
- .selectAll()
- .where('id', 'in', authorIds)
- .execute();
-
- return authors;
- }
-
- private buildMessages = (
- userId: string,
- messageRows: SelectNode[],
- authorRows: SelectNode[]
- ): MessageNode[] => {
- const messages: MessageNode[] = [];
- const authorMap = new Map<string, MessageAuthor>();
- for (const authorRow of authorRows) {
- const authorNode = mapNode(authorRow);
- if (authorNode.type !== 'user') {
- continue;
- }
-
- const name = authorNode.attributes.name;
- const email = authorNode.attributes.email;
- const avatar = authorNode.attributes.avatar;
-
- authorMap.set(authorRow.id, {
- id: authorRow.id,
- name: name ?? 'Unknown User',
- email,
- avatar,
- });
- }
+ private buildMessages = (rows: SelectNode[]): MessageNode[] => {
+ const nodes = rows.map(mapNode);
+ const messageNodes: MessageNode[] = [];
- for (const messageRow of messageRows) {
- const messageNode = mapNode(messageRow);
- if (messageNode.type !== 'message') {
+ for (const node of nodes) {
+ if (node.type !== 'message') {
continue;
}
- const author = authorMap.get(messageNode.createdBy);
- const reactionCounts = this.buildReactionCounts(
- messageNode.attributes,
- userId
- );
-
- const message: MessageNode = {
- id: messageNode.id,
- versionId: messageNode.versionId,
- createdAt: messageNode.createdAt,
- author: author ?? {
- id: messageNode.createdBy,
- name: 'Unknown User',
- email: '[email protected]',
- avatar: null,
- },
- content: mapBlocksToContents(
- messageNode.id,
- Object.values(messageNode.attributes.content ?? {})
- ),
- reactionCounts,
- };
-
- messages.push(message);
+ messageNodes.push(node);
}
- return messages.sort((a, b) => compareString(a.id, b.id));
+ return messageNodes.sort((a, b) => compareString(a.id, b.id));
};
-
- private buildReactionCounts(
- attributes: MessageAttributes,
- userId: string
- ): MessageReactionCount[] {
- const reactions = attributes.reactions as Record<string, string[]>;
- return Object.entries(reactions)
- .map(([reaction, users]) => ({
- reaction,
- count: users.length,
- isReactedTo: users.includes(userId),
- }))
- .filter((reactionCount) => reactionCount.count > 0);
- }
}
diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.ts
index 1405a02e..63e5b990 100644
--- a/apps/desktop/src/main/services/node-service.ts
+++ b/apps/desktop/src/main/services/node-service.ts
@@ -27,6 +27,23 @@ export type CreateNodeInput = {
};
class NodeService {
+ public async fetchNode(nodeId: string, userId: string): Promise<Node | null> {
+ const workspaceDatabase =
+ await databaseService.getWorkspaceDatabase(userId);
+
+ const nodeRow = await workspaceDatabase
+ .selectFrom('nodes')
+ .where('id', '=', nodeId)
+ .selectAll()
+ .executeTakeFirst();
+
+ if (!nodeRow) {
+ return null;
+ }
+
+ return mapNode(nodeRow);
+ }
+
public async createNode(
userId: string,
input: CreateNodeInput | CreateNodeInput[]
diff --git a/apps/desktop/src/renderer/components/messages/conversation.tsx b/apps/desktop/src/renderer/components/messages/conversation.tsx
index 37e33ac2..dbc57422 100644
--- a/apps/desktop/src/renderer/components/messages/conversation.tsx
+++ b/apps/desktop/src/renderer/components/messages/conversation.tsx
@@ -90,7 +90,7 @@ export const Conversation = ({ conversationId, role }: ConversationProps) => {
}
},
canDeleteMessage: (message) => {
- return isAdmin || message.author.id === workspace.userId;
+ return isAdmin || message.createdBy === workspace.userId;
},
}}
>
diff --git a/apps/desktop/src/renderer/components/messages/message-actions.tsx b/apps/desktop/src/renderer/components/messages/message-actions.tsx
index d99dead7..a9631fdf 100644
--- a/apps/desktop/src/renderer/components/messages/message-actions.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-actions.tsx
@@ -1,5 +1,6 @@
+import React from 'react';
import { useConversation } from '@/renderer/contexts/conversation';
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
import { MessagesSquare, Reply } from 'lucide-react';
import { MessageReactionCreatePopover } from '@/renderer/components/messages/message-reaction-create-popover';
import { useMutation } from '@/renderer/hooks/use-mutation';
@@ -7,7 +8,6 @@ import { useWorkspace } from '@/renderer/contexts/workspace';
import { MessageDeleteButton } from '@/renderer/components/messages/message-delete-button';
import { EmojiElement } from '@/renderer/components/emojis/emoji-element';
import { Separator } from '@/renderer/components/ui/separator';
-import React from 'react';
const MessageAction = ({ children }: { children: React.ReactNode }) => {
return (
diff --git a/apps/desktop/src/renderer/components/messages/message-avatar.tsx b/apps/desktop/src/renderer/components/messages/message-avatar.tsx
new file mode 100644
index 00000000..83ef934f
--- /dev/null
+++ b/apps/desktop/src/renderer/components/messages/message-avatar.tsx
@@ -0,0 +1,32 @@
+import { useWorkspace } from '@/renderer/contexts/workspace';
+import { useQuery } from '@/renderer/hooks/use-query';
+import { MessageNode, UserNode } from '@colanode/core';
+import { Avatar } from '@/renderer/components/avatars/avatar';
+
+interface MessageAvatarProps {
+ message: MessageNode;
+}
+
+export const MessageAvatar = ({ message }: MessageAvatarProps) => {
+ const workspace = useWorkspace();
+ const { data } = useQuery({
+ type: 'node_get',
+ nodeId: message.createdBy,
+ userId: workspace.userId,
+ });
+
+ if (!data || data.type !== 'user') {
+ return null;
+ }
+
+ const author = data as UserNode;
+
+ return (
+ <Avatar
+ id={author.id}
+ name={author.attributes.name}
+ avatar={author.attributes.avatar}
+ size="medium"
+ />
+ );
+};
diff --git a/apps/desktop/src/renderer/components/messages/message-content.tsx b/apps/desktop/src/renderer/components/messages/message-content.tsx
new file mode 100644
index 00000000..621cfae4
--- /dev/null
+++ b/apps/desktop/src/renderer/components/messages/message-content.tsx
@@ -0,0 +1,24 @@
+import { NodeRenderer } from '@/renderer/editor/renderers/node';
+import { mapBlocksToContents } from '@/shared/lib/editor';
+import { MessageNode } from '@colanode/core';
+
+interface MessageContentProps {
+ message: MessageNode;
+}
+
+export const MessageContent = ({ message }: MessageContentProps) => {
+ const nodeBlocks = Object.values(message.attributes.content ?? {});
+ const contents = mapBlocksToContents(message.id, nodeBlocks);
+
+ return (
+ <div className="text-foreground">
+ {contents.map((node) => (
+ <NodeRenderer
+ key={node.attrs?.id}
+ node={node}
+ keyPrefix={node.attrs?.id}
+ />
+ ))}
+ </div>
+ );
+};
diff --git a/apps/desktop/src/renderer/components/messages/message-create.tsx b/apps/desktop/src/renderer/components/messages/message-create.tsx
index 85b06ecd..bd79f309 100644
--- a/apps/desktop/src/renderer/components/messages/message-create.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-create.tsx
@@ -5,7 +5,7 @@ import {
} from '@/renderer/components/messages/message-editor';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { useWorkspace } from '@/renderer/contexts/workspace';
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
import { editorHasContent } from '@/shared/lib/editor';
import { useConversation } from '@/renderer/contexts/conversation';
import { MessageReplyBanner } from '@/renderer/components/messages/message-reply-banner';
@@ -63,23 +63,13 @@ export const MessageCreate = React.forwardRef<MessageCreateRefProps>(
return;
}
- if (replyTo) {
- content.content.unshift({
- type: 'messageReference',
- attrs: {
- id: replyTo.id,
- name: replyTo.author.name,
- },
- content: replyTo.content,
- });
- }
-
mutate({
input: {
type: 'message_create',
conversationId: conversation.id,
content: content,
userId: workspace.userId,
+ referenceId: replyTo?.id,
},
onSuccess: () => {
setReplyTo(null);
diff --git a/apps/desktop/src/renderer/components/messages/message-header.tsx b/apps/desktop/src/renderer/components/messages/message-header.tsx
new file mode 100644
index 00000000..0febbcd5
--- /dev/null
+++ b/apps/desktop/src/renderer/components/messages/message-header.tsx
@@ -0,0 +1,46 @@
+import { MessageNode, UserNode } from '@colanode/core';
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from '@/renderer/components/ui/tooltip';
+import { formatDate, timeAgo } from '@/shared/lib/utils';
+import { useWorkspace } from '@/renderer/contexts/workspace';
+import { useQuery } from '@/renderer/hooks/use-query';
+
+interface MessageHeaderProps {
+ message: MessageNode;
+}
+
+export const MessageHeader = ({ message }: MessageHeaderProps) => {
+ const workspace = useWorkspace();
+ const { data } = useQuery({
+ type: 'node_get',
+ nodeId: message.createdBy,
+ userId: workspace.userId,
+ });
+
+ if (!data || data.type !== 'user') {
+ return null;
+ }
+
+ const author = data as UserNode;
+
+ return (
+ <div className="font-medium">
+ <span>{author.attributes.name}</span>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className="ml-2 text-xs text-muted-foreground">
+ {timeAgo(message.createdAt)}
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ <span className="text-sm shadow-md">
+ {formatDate(message.createdAt)}
+ </span>
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ );
+};
diff --git a/apps/desktop/src/renderer/components/messages/message-reactions.tsx b/apps/desktop/src/renderer/components/messages/message-reactions.tsx
index 500c681d..370c4282 100644
--- a/apps/desktop/src/renderer/components/messages/message-reactions.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-reactions.tsx
@@ -1,4 +1,4 @@
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
import { EmojiElement } from '@/renderer/components/emojis/emoji-element';
import { cn } from '@/shared/lib/utils';
import { useWorkspace } from '@/renderer/contexts/workspace';
@@ -12,13 +12,21 @@ export const MessageReactions = ({ message }: MessageReactionsProps) => {
const workspace = useWorkspace();
const { mutate, isPending } = useMutation();
- if (message.reactionCounts.length === 0) {
+ const reactionCounts = Object.entries(message.attributes.reactions ?? {})
+ .map(([reaction, users]) => ({
+ reaction,
+ count: users.length,
+ isReactedTo: users.includes(workspace.userId),
+ }))
+ .filter((reactionCount) => reactionCount.count > 0);
+
+ if (reactionCounts.length === 0) {
return null;
}
return (
<div className="my-1 flex flex-row gap-2">
- {message.reactionCounts.map((reaction) => {
+ {reactionCounts.map((reaction) => {
if (reaction.count === 0) {
return null;
}
@@ -38,7 +46,7 @@ export const MessageReactions = ({ message }: MessageReactionsProps) => {
}
if (
- message.reactionCounts.some(
+ reactionCounts.some(
(reactionCount) =>
reactionCount.reaction === reaction.reaction &&
reactionCount.isReactedTo
diff --git a/apps/desktop/src/renderer/components/messages/message-reply-banner.tsx b/apps/desktop/src/renderer/components/messages/message-reply-banner.tsx
index cf60b764..57abd055 100644
--- a/apps/desktop/src/renderer/components/messages/message-reply-banner.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-reply-banner.tsx
@@ -1,4 +1,6 @@
-import { MessageNode } from '@/shared/types/messages';
+import { useWorkspace } from '@/renderer/contexts/workspace';
+import { useQuery } from '@/renderer/hooks/use-query';
+import { MessageNode } from '@colanode/core';
import { CircleX } from 'lucide-react';
interface MessageReplyBannerProps {
@@ -10,10 +12,22 @@ export const MessageReplyBanner = ({
message,
onCancel,
}: MessageReplyBannerProps) => {
+ const workspace = useWorkspace();
+ const { data } = useQuery({
+ type: 'node_get',
+ nodeId: message.createdBy,
+ userId: workspace.userId,
+ });
+
+ if (!data || data.type !== 'user') {
+ return null;
+ }
+
return (
<div className="flex flex-row items-center justify-between rounded-t-lg border-b-2 bg-gray-100 p-2 text-foreground">
<p className="text-sm">
- Replying to <span className="font-semibold">{message.author.name}</span>
+ Replying to{' '}
+ <span className="font-semibold">{data.attributes.name}</span>
</p>
<button className="cursor-pointer" onClick={onCancel}>
<CircleX className="size-4" />
diff --git a/apps/desktop/src/renderer/components/messages/message.tsx b/apps/desktop/src/renderer/components/messages/message.tsx
index 44b86072..66aa013e 100644
--- a/apps/desktop/src/renderer/components/messages/message.tsx
+++ b/apps/desktop/src/renderer/components/messages/message.tsx
@@ -1,24 +1,19 @@
-import { Avatar } from '@/renderer/components/avatars/avatar';
-import {
- Tooltip,
- TooltipContent,
- TooltipTrigger,
-} from '@/renderer/components/ui/tooltip';
-import { formatDate, timeAgo } from '@/shared/lib/utils';
import { InView } from 'react-intersection-observer';
-import { NodeRenderer } from '@/renderer/editor/renderers/node';
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
import { MessageReactions } from '@/renderer/components/messages/message-reactions';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useRadar } from '@/renderer/contexts/radar';
import { MessageActions } from '@/renderer/components/messages/message-actions';
+import { MessageAvatar } from '@/renderer/components/messages/message-avatar';
+import { MessageHeader } from '@/renderer/components/messages/message-header';
+import { MessageContent } from '@/renderer/components/messages/message-content';
interface MessageProps {
message: MessageNode;
previousMessage?: MessageNode | null;
}
-const shouldDisplayUserInfo = (
+const shouldDisplayAuthor = (
message: MessageNode,
previousMessage?: MessageNode | null
) => {
@@ -33,53 +28,28 @@ const shouldDisplayUserInfo = (
return true;
}
- return previousMessage.author.id !== message.author.id;
+ return previousMessage.createdBy !== message.createdBy;
};
export const Message = ({ message, previousMessage }: MessageProps) => {
const workspace = useWorkspace();
const radar = useRadar();
-
- const displayUserInfo = shouldDisplayUserInfo(message, previousMessage);
+ const displayAuthor = shouldDisplayAuthor(message, previousMessage);
return (
<div
id={`message-${message.id}`}
key={`message-${message.id}`}
className={`group flex flex-row px-1 hover:bg-gray-50 ${
- displayUserInfo ? 'mt-2 first:mt-0' : ''
+ displayAuthor ? 'mt-2 first:mt-0' : ''
}`}
>
<div className="mr-2 w-10 pt-1">
- {displayUserInfo && (
- <Avatar
- id={message.author.id}
- name={message.author.name}
- avatar={message.author.avatar}
- size="medium"
- />
- )}
+ {displayAuthor && <MessageAvatar message={message} />}
</div>
<div className="relative w-full">
- {displayUserInfo && (
- <div className="font-medium">
- <span>{message.author.name}</span>
- <Tooltip>
- <TooltipTrigger asChild>
- <span className="ml-2 text-xs text-muted-foreground">
- {timeAgo(message.createdAt)}
- </span>
- </TooltipTrigger>
- <TooltipContent>
- <span className="text-sm shadow-md">
- {formatDate(message.createdAt)}
- </span>
- </TooltipContent>
- </Tooltip>
- </div>
- )}
-
+ {displayAuthor && <MessageHeader message={message} />}
<InView
rootMargin="50px"
onChange={(inView) => {
@@ -89,15 +59,7 @@ export const Message = ({ message, previousMessage }: MessageProps) => {
}}
>
<MessageActions message={message} />
- <div className="text-foreground">
- {message.content.map((node) => (
- <NodeRenderer
- key={node.attrs?.id}
- node={node}
- keyPrefix={node.attrs?.id}
- />
- ))}
- </div>
+ <MessageContent message={message} />
<MessageReactions message={message} />
</InView>
</div>
diff --git a/apps/desktop/src/renderer/contexts/conversation.ts b/apps/desktop/src/renderer/contexts/conversation.ts
index e0a9eade..a4921d97 100644
--- a/apps/desktop/src/renderer/contexts/conversation.ts
+++ b/apps/desktop/src/renderer/contexts/conversation.ts
@@ -1,6 +1,6 @@
import { NodeRole } from '@colanode/core';
import { createContext, useContext } from 'react';
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
interface ConversationContext {
id: string;
diff --git a/apps/desktop/src/renderer/editor/renderers/message-reference.tsx b/apps/desktop/src/renderer/editor/renderers/message-reference.tsx
index 3c49aa3e..643ec3b0 100644
--- a/apps/desktop/src/renderer/editor/renderers/message-reference.tsx
+++ b/apps/desktop/src/renderer/editor/renderers/message-reference.tsx
@@ -12,7 +12,6 @@ export const MessageReferenceRenderer = ({
}: MessageReferenceRendererProps) => {
return (
<div className="items-top flex flex-col gap-2 border-l-4 p-2 text-sm text-foreground">
- <p className="font-medium">{node.attrs?.name ?? 'Unknown'}</p>
<NodeChildrenRenderer node={node} keyPrefix={keyPrefix} />
</div>
);
diff --git a/apps/desktop/src/shared/mutations/message-create.ts b/apps/desktop/src/shared/mutations/message-create.ts
index f3fac41f..a4030eb1 100644
--- a/apps/desktop/src/shared/mutations/message-create.ts
+++ b/apps/desktop/src/shared/mutations/message-create.ts
@@ -5,6 +5,7 @@ export type MessageCreateMutationInput = {
userId: string;
conversationId: string;
content: JSONContent;
+ referenceId?: string;
};
export type MessageCreateMutationOutput = {
diff --git a/apps/desktop/src/shared/queries/message-list.ts b/apps/desktop/src/shared/queries/message-list.ts
index 7eac8c77..dcf6ca05 100644
--- a/apps/desktop/src/shared/queries/message-list.ts
+++ b/apps/desktop/src/shared/queries/message-list.ts
@@ -1,4 +1,4 @@
-import { MessageNode } from '@/shared/types/messages';
+import { MessageNode } from '@colanode/core';
export type MessageListQueryInput = {
type: 'message_list';
diff --git a/apps/desktop/src/shared/types/messages.ts b/apps/desktop/src/shared/types/messages.ts
deleted file mode 100644
index c119f013..00000000
--- a/apps/desktop/src/shared/types/messages.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { JSONContent } from '@tiptap/core';
-
-export type MessageNode = {
- id: string;
- content: JSONContent[];
- createdAt: string;
- author: MessageAuthor;
- reactionCounts: MessageReactionCount[];
- versionId: string;
-};
-
-export type MessageReactionCount = {
- reaction: string;
- count: number;
- isReactedTo: boolean;
-};
-
-export type MessageAuthor = {
- id: string;
- name: string;
- email: string;
- avatar: string | null;
-};