Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
macwilko committed Dec 19, 2023
1 parent 41454a3 commit 02837e4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
30 changes: 30 additions & 0 deletions app/components/channel-feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function ChannelFeed(props) {
const channelId = channel.id;

const messagesFetcher = useFetcher({ key: `messages-${channelId}` });
const channelFetcher = useFetcher({ key: `channel-${channelId}` });

const [remainingMessages, setRemainingMessages] = useState(channel.remaining_messages);

Expand Down Expand Up @@ -184,6 +185,35 @@ export default function ChannelFeed(props) {

}, [messagesFetcher, fetchingMore]);

useEffect(() => {
const optimisticUuid = channelFetcher.data?.optimisticUuid;

if (!optimisticUuid) {
return
}

const response = channelFetcher.data;

if (!!response?.id) {

setMessages((prev) => {
let i = 0;
const update = [...prev];
while(i < update.length) {
const msg = update[i];
if (msg.optimisticUuid == optimisticUuid) {
msg.optimistic = false;
msg.id = msg.id;
update[i] = msg;
}
i++;
}
return update;
}, () => {});
}

}, [channelFetcher]);

useEffect(() => {
if (page == 0) {
return;
Expand Down
6 changes: 6 additions & 0 deletions app/components/channel-replybox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CrossCircledIcon, CameraIcon, TrashIcon, FileIcon } from '@radix-ui/rea
import { Image } from "@unpic/react";
import useAutosizeTextArea from './useAutosizeTextArea';
import './styles.channel-replybox.css';
import { v4 as uuidv4 } from 'uuid';

function getVideoCover(file, seekTo = 0.0) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -70,6 +71,10 @@ export default function ChannelReplybox(props) {

form.append("__action", "create_message");

const optimisticUuid = uuidv4().stringify();

form.append("optimisticUuid", optimisticUuid);

const localFiles = files.map(f => {
return {
name: f.name,
Expand Down Expand Up @@ -110,6 +115,7 @@ export default function ChannelReplybox(props) {
parentId: replyingTo?.messageId,
parentMessage: replyingTo?.message,
files: localFiles ?? [],
optimisticUuid: optimisticUuid,
})

restoreDefaults();
Expand Down
5 changes: 3 additions & 2 deletions app/routes/z.create-message.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ export async function action({ request }) {
const files = body.get("files");

const input = JSON.parse(body.get("input"));
const optimisticUuid = body.get("optimisticUuid");

const { communityHandle, channelId, text, parentId } = input;

const [response, errors] = await createMessage(communityHandle, channelId, text, parentId, files, jwt);

if (!!errors) {
return json({ errors: errors });
return json({ optimisticUuid: optimisticUuid, errors: errors });
}

if (!!response?.id) {
return json({ updated: true });
return json({ optimisticUuid: optimisticUuid, ...response, });
}
} if (type == "update_profile_pic") {
const file = body.get("file");
Expand Down
21 changes: 17 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"remix-electron": "^2.0.0",
"remix-utils": "^7.3.0",
"source-map-support": "^0.5.21",
"twemoji": "^14.0.2"
"twemoji": "^14.0.2",
"uuid": "^9.0.1"
},
"devDependencies": {
"@remix-run/dev": "^2.1.0",
Expand Down

0 comments on commit 02837e4

Please sign in to comment.