Skip to content

Commit

Permalink
feat: allow cmd+enter to send message
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Aug 28, 2024
1 parent 0c60f67 commit 4a556a7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import { useDraft } from '@/src/stores/draft-store';
import { Editor } from '@/src/components/editor';
import { type TypeId } from '@u22n/utils/typeid';
import { useDebounce } from '@uidotdev/usehooks';
import { useAtom, useAtomValue } from 'jotai';
import { platform } from '@/src/lib/trpc';
import { cn } from '@/src/lib/utils';
import { ms } from '@u22n/utils/ms';
import { useAtom } from 'jotai';
import { toast } from 'sonner';

type ReplyBoxProps = {
Expand All @@ -58,7 +58,8 @@ export function ReplyBox({
const [editorText, setEditorText] = useState(draft.content);
const orgShortcode = useOrgShortcode();
const spaceShortcode = useSpaceShortcode(false);
const replyTo = useAtomValue(replyToMessageAtom);
const [replyTo] = useAtom(replyToMessageAtom);
const [emailIdentity, setEmailIdentity] = useAtom(emailIdentityAtom);
const addConvoToCache = useUpdateConvoMessageList$Cache();
const updateConvoData = useUpdateConvoData$Cache();
const editorRef = useRef<EditorFunctions>(null);
Expand Down Expand Up @@ -116,8 +117,6 @@ export function ReplyBox({
}
);

const [emailIdentity, setEmailIdentity] = useAtom(emailIdentityAtom);

const {
attachments,
openFilePicker,
Expand Down Expand Up @@ -148,7 +147,11 @@ export function ReplyBox({

const handleReply = useCallback(
async (type: 'comment' | 'message') => {
if (!replyTo) return;
const currentReplyTo = replyTo;
if (!currentReplyTo) {
console.error('ReplyBox: replyTo is null');
return;
}
if (hasExternalParticipants && emailIdentity === null) {
toast.error('Please select an email identity to send the message as.');
return;
Expand All @@ -158,7 +161,7 @@ export function ReplyBox({
attachments: getTrpcUploadFormat(),
orgShortcode,
message: editorText,
replyToMessagePublicId: replyTo,
replyToMessagePublicId: currentReplyTo,
messageType: type,
sendAsEmailIdentityPublicId: emailIdentity ?? undefined
});
Expand Down Expand Up @@ -190,6 +193,7 @@ export function ReplyBox({
onReply();
},
[
replyTo,
addConvoToCache,
convoId,
editorText,
Expand All @@ -198,13 +202,45 @@ export function ReplyBox({
hasExternalParticipants,
onReply,
orgShortcode,
replyTo,
replyToConvo,
spaceShortcode,
updateConvoData
]
);

const handleSendMessage = useCallback(() => {
if (
!replyTo ||
isEditorEmpty ||
(hasExternalParticipants && !emailIdentity) ||
replyToConvoLoading
) {
return;
}
return handleReply('message');
}, [
replyTo,
isEditorEmpty,
hasExternalParticipants,
emailIdentity,
replyToConvoLoading,
handleReply
]);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.preventDefault();
void handleSendMessage();
}
};

document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [handleSendMessage]);

return (
<div className="flex min-h-32 flex-col p-4">
<div
Expand Down Expand Up @@ -328,10 +364,7 @@ export function ReplyBox({
replyToConvoLoading
}
size={isMobile ? 'icon' : 'sm'}
onPointerDown={(e) => {
e.preventDefault();
return handleReply('message');
}}>
onClick={handleSendMessage}>
{isMobile ? <PaperPlaneTilt size={16} /> : <span>Send</span>}
</Button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,26 @@ export default function CreateConvoForm({
}
});

const handleSendMessage = useCallback(() => {
if (isFormValid && !isCreating) {
createConvo('message');
}
}, [isFormValid, isCreating, createConvo]);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.preventDefault();
handleSendMessage();
}
};

document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [handleSendMessage]);

return (
<div className="flex h-full w-full min-w-0 flex-col gap-3 p-3">
<div className="flex w-full flex-col gap-2 text-sm">
Expand Down Expand Up @@ -685,7 +705,7 @@ export default function CreateConvoForm({
size={'sm'}
loading={isCreating && messageType === 'message'}
disabled={!isFormValid || isCreating}
onClick={() => createConvo('message')}>
onClick={handleSendMessage}>
{isMobile ? <PaperPlaneTilt size={16} /> : <span>Send</span>}
</Button>
</div>
Expand Down

0 comments on commit 4a556a7

Please sign in to comment.