Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
macwilko committed Dec 16, 2023
1 parent 0e05459 commit c406c45
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
129 changes: 61 additions & 68 deletions app/components/channel-feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default function ChannelFeed(props) {

const channelId = channel.id;

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

const [remainingMessages, setRemainingMessages] = useState(channel.remaining_messages);
Expand All @@ -40,6 +39,62 @@ export default function ChannelFeed(props) {

const [replyingTo, setReplyingTo] = useState(null);

const addMessageOptimistically = (data) => {
let newMessages = [...messages];

const { text, parentMessage, localFiles, user } = data;

const files = localFiles ?? [];

if (newMessages.length > 0) {
const idx = newMessages.length - 1;
const last = newMessages[idx];

if (last.user.handle == user.handle && !parentMessage && files.length == 0 && (!last.files || last.files.length == 0)) {
const cp = [...newMessages];

cp[idx] = {
id: last.id,
optimistic: true,
created_at: last.created_at,
updated_at: last.updated_at,
text: `${last.text}\n${text}`,
user: user,
edited: last.edited,
reactions: last.reactions,
files: files,
};

newMessages = cp;
} else {
newMessages = [...messages, {
id: Date().toString(),
optimistic: true,
text: text,
user: user,
edited: false,
parent: parentMessage,
files: files,
}];
}
} else {
newMessages = [{
id: Date().toString(),
optimistic: true,
text: text,
user: user,
edited: false,
parent: parentMessage,
files: files,
}];
}

setMessages(newMessages, () => {
const feed = feedRef.current;
feed.scrollTop = feed.scrollHeight;
});
};

useEffect(() => {
if (lastJsonMessage?.topic == channelId) {
try {
Expand All @@ -50,11 +105,11 @@ export default function ChannelFeed(props) {
const idx = messages.length - 1;
const prev = messages[idx];

if ((!!prev.optimistic && prev.user.handle == update.user.handle) || prev.id == update.id) {
if (update.updated_at == prev.updated_at) {
return;
}

if (update.updated_at == prev.updated_at) {
return;
}
if (prev.id == update.id) {

let mp = [...messages];

Expand Down Expand Up @@ -99,69 +154,6 @@ export default function ChannelFeed(props) {
});
}, [channelId]);

useEffect(() => {
const user = channel.user;

const action = channelFetcher.formData?.get("__action");

let newMessages = [...messages];

if (action === "create_message" && !!user) {

const { text, parentMessage, localFiles } = JSON.parse(channelFetcher.formData.get("input"));

const files = localFiles ?? [];

if (newMessages.length > 0) {
const idx = newMessages.length - 1;
const last = newMessages[idx];

if (last.user.handle == user.handle && !parentMessage && files.length == 0 && (!last.files || last.files.length == 0)) {
const cp = [...newMessages];

cp[idx] = {
id: last.id,
optimistic: true,
created_at: last.created_at,
updated_at: last.updated_at,
text: `${last.text}\n${text}`,
user: user,
edited: last.edited,
reactions: last.reactions,
files: files,
};
newMessages = cp;
} else {
newMessages = [...messages, {
id: Date().toString(),
optimistic: true,
text: text,
user: user,
edited: false,
parent: parentMessage,
files: files,
}];
}
} else {
newMessages = [{
id: Date().toString(),
optimistic: true,
text: text,
user: user,
edited: false,
parent: parentMessage,
files: files,
}];
}

setMessages(newMessages, () => {
const feed = feedRef.current;
feed.scrollTop = feed.scrollHeight;
});

}
}, [channelFetcher, messages]);

useEffect(() => {
if (!fetchingMore) {
return;
Expand Down Expand Up @@ -280,6 +272,7 @@ export default function ChannelFeed(props) {
<span style={{ height: 300 }} ref={end} />
<div className="wk-blur-box">
<ChannelReplybox
addMessageOptimistically={addMessageOptimistically}
community={community}
channel={channel}
replyingTo={replyingTo}
Expand Down
2 changes: 1 addition & 1 deletion app/components/channel-message.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function ChannelMessage(props) {
setEdited(message.edited);
setReactions(message.reactions ?? {});
setFiles(message.files ?? []);
}, [message.updated_at]);
}, [message]);

const rk = Object.keys(reactions);

Expand Down
12 changes: 11 additions & 1 deletion app/components/channel-replybox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getVideoCover(file, seekTo = 0.0) {

export default function ChannelReplybox(props) {

const { channel, community, replyingTo, setReplyingTo, files, setFiles, dragging } = props;
const { channel, community, replyingTo, setReplyingTo, files, setFiles, dragging, addMessageOptimistically } = props;

const [filePreviews, setFilePreviews] = useState([]);

Expand Down Expand Up @@ -102,6 +102,16 @@ export default function ChannelReplybox(props) {
fetcherKey: `channel-${channel.id}`
});

addMessageOptimistically({
user: channel.user,
text: trimmed,
channelId: channel.id,
communityHandle: community.handle,
parentId: replyingTo?.messageId,
parentMessage: replyingTo?.message,
localFiles: localFiles,
})

restoreDefaults();
}, [text, channel, community, replyingTo, files]);

Expand Down

0 comments on commit c406c45

Please sign in to comment.