rDrama/chat/src/features/chat/UserInput.tsx

188 lines
4.9 KiB
TypeScript
Raw Normal View History

[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
import React, {
ChangeEvent,
KeyboardEvent,
FormEvent,
useCallback,
useRef,
useMemo,
useState,
2022-09-27 02:19:48 +00:00
useEffect,
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
} from "react";
2022-09-27 22:46:29 +00:00
import cx from "classnames";
2022-09-27 22:31:45 +00:00
import { useChat, useEmojis } from "../../hooks";
import { QuickEmojis } from "../emoji";
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
import "./UserInput.css";
2022-09-27 22:31:45 +00:00
interface Props {
2022-09-27 22:46:29 +00:00
large?: boolean;
2022-09-27 22:31:45 +00:00
onFocus(): void;
onBlur(): void;
}
2022-09-27 22:46:29 +00:00
export function UserInput({ large = false, onFocus, onBlur }: Props) {
2022-09-27 05:15:22 +00:00
const { draft, userToDm, sendMessage, updateDraft } = useChat();
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
const builtChatInput = useRef<HTMLTextAreaElement>(null);
const { visible, addQuery } = useEmojis();
const form = useRef<HTMLFormElement>(null);
const [typingOffset, setTypingOffset] = useState(0);
const quickEmojis = useMemo(
() => visible.slice(0, process.env.QUICK_EMOJIS_MAX_COUNT),
[visible]
);
const handleChange = useCallback(
(event: ChangeEvent<HTMLTextAreaElement>) => {
const input = event.target.value;
const [openEmojiToken, closeEmojiToken] = locateEmojiTokens(input);
const emojiSegment = input.slice(openEmojiToken + 1, closeEmojiToken + 1);
updateDraft(input);
2022-09-24 22:05:50 +00:00
addQuery(
openEmojiToken === -1 || emojiSegment.includes(" ") ? "" : emojiSegment
);
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
setTypingOffset(
emojiSegment.length * process.env.APPROXIMATE_CHARACTER_WIDTH
);
},
[]
);
const handleSendMessage = useCallback(
(event?: FormEvent<HTMLFormElement>) => {
event?.preventDefault();
sendMessage();
},
[sendMessage]
);
const handleKeyUp = useCallback(
(event: KeyboardEvent<HTMLTextAreaElement>) => {
2022-09-25 19:23:10 +00:00
if (event.key === "Enter" && !event.shiftKey) {
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
handleSendMessage();
}
},
[handleSendMessage]
);
const handleInsertQuickEmoji = useCallback(
(emoji: string) => {
const [openEmojiToken, closeEmojiToken] = locateEmojiTokens(draft);
const toReplace = draft.slice(openEmojiToken, closeEmojiToken + 1);
updateDraft((prev) => prev.replace(toReplace, `:${emoji}: `));
addQuery("");
builtChatInput.current?.focus();
},
[draft]
);
const handleFocus = useCallback(() => {
builtChatInput.current?.scrollIntoView({ behavior: "smooth" });
2022-09-27 22:31:45 +00:00
onFocus();
}, [onFocus]);
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
2022-09-27 02:19:48 +00:00
// Listen for changes from the Emoji Modal and reflect them in draft
useEffect(() => {
const handleEmojiInsert = (event: CustomEvent<{ emoji: string }>) =>
updateDraft((prev) => `${prev} ${event.detail.emoji} `);
document.addEventListener("emojiInserted", handleEmojiInsert);
return () => {
document.removeEventListener("emojiInserted", handleEmojiInsert);
}
}, []);
2022-09-27 05:15:22 +00:00
useEffect(() => {
if (userToDm) {
builtChatInput.current?.focus();
}
}, [userToDm])
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
return (
<form ref={form} className="UserInput" onSubmit={handleSendMessage}>
{quickEmojis.length > 0 && (
<div
style={{
position: "absolute",
top: -254,
height: 250,
left: typingOffset,
display: "flex",
flexDirection: "column-reverse",
}}
>
<QuickEmojis
emojis={quickEmojis}
onSelectEmoji={handleInsertQuickEmoji}
/>
</div>
)}
<textarea
ref={builtChatInput}
id="builtChatInput"
2022-12-30 18:09:55 +00:00
className={cx("UserInput-input form-control")}
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
minLength={1}
maxLength={1000}
rows={1}
onChange={handleChange}
onKeyUp={handleKeyUp}
onFocus={handleFocus}
2022-09-27 22:31:45 +00:00
onBlur={onBlur}
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
placeholder="Message"
autoComplete="off"
value={draft}
/>
<i
role="button"
2022-09-27 01:28:39 +00:00
data-bs-toggle="modal"
data-bs-target="#emojiModal"
data-bs-placement="bottom"
title="Add Emoji"
onClick={() => {
const whatever = window as any;
whatever.loadEmojis("builtChatInput");
}}
className="UserInput-emoji fas fa-smile-beam"
/>
<button
className="btn btn-secondary"
disabled={draft.length === 0}
onClick={sendMessage}
>
<i className="UserInput-emoji fas fa-reply" />
</button>
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +00:00
</form>
);
}
function locateEmojiTokens(text: string) {
let openEmojiInputToken = -1;
let endEmojiInputToken = -1;
if (text.length <= 1) {
return [openEmojiInputToken, endEmojiInputToken];
}
for (let i = 0; i < text.length; i++) {
const character = text[i];
if (character === process.env.EMOJI_INPUT_TOKEN) {
if (openEmojiInputToken === -1) {
openEmojiInputToken = i;
} else {
openEmojiInputToken = -1;
}
}
}
if (openEmojiInputToken !== -1) {
endEmojiInputToken = openEmojiInputToken;
while (
endEmojiInputToken < text.length - 1 &&
text[endEmojiInputToken] !== " "
) {
endEmojiInputToken++;
}
}
return [openEmojiInputToken, endEmojiInputToken];
}