Files
open-webui/src/lib/components/channel/Thread.svelte
T
Classic298 742941a436 fix: channel message stays greyed out when the send confirmation is missed or the send fails (#34772)
A channel message stayed greyed out with no actions until a reload whenever the page missed the server's confirmation, for example after switching apps on a phone right after sending, even though the server had saved it. A failed send left the same dead copy behind and the typed text was lost, and a dropped request showed no error at all. Now the message turns normal as soon as the server answers the send, and a failed send goes back to the list of waiting messages above the input, where it can be sent again, edited or deleted. Nothing is resent automatically, because a request that looked failed may still have reached the server. Thread replies get the same treatment, so a reply no longer goes missing from the thread or loses its text.

Fixes #34771
2026-11-22 19:22:59 +04:00

334 lines
8.1 KiB
Svelte

<script lang="ts">
import { getPageContext } from '$lib/utils/page';
const { goto, url: pageUrl } = getPageContext();
import { socket, user } from '$lib/stores';
import { getChannelThreadMessages, sendMessage } from '$lib/apis/channels';
import XMark from '$lib/components/icons/XMark.svelte';
import MessageInput from './MessageInput.svelte';
import Messages from './Messages.svelte';
import { onDestroy, onMount, tick, getContext } from 'svelte';
import { toast } from 'svelte-sonner';
import Spinner from '../common/Spinner.svelte';
const i18n = getContext('i18n');
export let threadId = null;
export let channel = null;
export let chatInputElement = null;
export let onClose = () => {};
export let onPin = () => {};
let messages = null;
let searchAnchor = '';
let loading = null;
let top = false;
let messagesContainerElement = null;
let replyToMessage = null;
let typingUsers = [];
let typingUsersTimeout = {};
$: if (threadId) {
initHandler(
$pageUrl.searchParams.get('thread') === threadId
? ($pageUrl.searchParams.get('message') ?? '')
: ''
);
}
const scrollToBottom = () => {
if (messagesContainerElement) {
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
}
};
const initHandler = async (anchor = '') => {
loading = null;
searchAnchor = anchor;
messages = null;
top = false;
replyToMessage = null;
typingUsers = [];
typingUsersTimeout = {};
if (channel) {
loading = getChannelThreadMessages(localStorage.token, channel.id, threadId, 0, 50, {
around: anchor
}).catch((error) => {
if (request === loading) toast.error(String(error));
return [];
});
const request = loading;
const loaded = await request;
if (request !== loading) return;
messages = loaded ?? [];
if (messages.length < 50) {
top = true;
}
await tick();
if (anchor) {
const element = threadRoot?.querySelector(`[id="message-${threadId}-${anchor}"]`);
element?.scrollIntoView({ block: 'center' });
element?.animate([{ backgroundColor: '#a5a5c544' }, { backgroundColor: 'transparent' }], {
duration: 2200
});
} else scrollToBottom();
} else {
goto('/');
}
};
const channelEventHandler = async (event) => {
console.debug(event);
if (event.channel_id === channel.id) {
const type = event?.data?.type ?? null;
const data = event?.data?.data ?? null;
if (messages === null && type !== 'typing') {
const request = loading;
await request;
if (request !== loading) return;
}
if (type === 'message') {
if ((data?.parent_id ?? null) === threadId) {
if (messages && !messages.some((message) => message.id === data.id)) {
messages = [{ ...data, temp_id: null }, ...messages];
if (typingUsers.find((user) => user.id === event.user.id)) {
typingUsers = typingUsers.filter((user) => user.id !== event.user.id);
}
}
}
} else if (type === 'message:update') {
if (messages) {
const idx = messages.findIndex((message) => message.id === data.id);
if (idx !== -1) {
messages[idx] = data;
}
}
} else if (type === 'message:delete') {
if (data.id === threadId) {
onClose();
}
if (messages) {
messages = messages
.filter((message) => message.id !== data.id)
.map((message) =>
message?.reply_to_message?.id === data.id
? { ...message, reply_to_message: null }
: message
);
}
if (replyToMessage?.id === data.id) {
replyToMessage = null;
}
} else if (type.includes('message:reaction')) {
if (messages) {
const idx = messages.findIndex((message) => message.id === data.id);
if (idx !== -1) {
messages[idx] = data;
}
}
} else if (type === 'typing' && event.message_id === threadId) {
if (event.user.id === $user?.id) {
return;
}
typingUsers = data.typing
? [
...typingUsers,
...(typingUsers.find((user) => user.id === event.user.id)
? []
: [
{
id: event.user.id,
name: event.user.name
}
])
]
: typingUsers.filter((user) => user.id !== event.user.id);
if (typingUsersTimeout[event.user.id]) {
clearTimeout(typingUsersTimeout[event.user.id]);
}
typingUsersTimeout[event.user.id] = setTimeout(() => {
typingUsers = typingUsers.filter((user) => user.id !== event.user.id);
}, 5000);
}
}
};
const submitHandler = async ({
content,
data,
channel_id,
parent_id,
reply_to_message
}: {
content: string;
data: any;
channel_id: string;
parent_id: string | null;
reply_to_message: any;
}) => {
if (!content && (data?.files ?? []).length === 0) {
return;
}
const res = await sendMessage(localStorage.token, channel_id, {
parent_id: parent_id ?? undefined,
reply_to_id: reply_to_message?.id ?? null,
content: content,
data: data
}).catch((error) => {
toast.error(`${error}`);
return null;
});
if (
res?.parent_id === threadId &&
messages &&
!messages.some((message) => message.id === res.id)
) {
messages = [{ ...res, user: $user, reply_to_message: reply_to_message ?? null }, ...messages];
}
return res;
};
const onChange = async () => {
$socket?.emit('events:channel', {
channel_id: channel.id,
message_id: threadId,
data: {
type: 'typing',
data: {
typing: true
}
}
});
};
onMount(() => {
$socket?.on('events:channel', channelEventHandler);
});
onDestroy(() => {
loading = null;
$socket?.off('events:channel', channelEventHandler);
});
let threadRoot: HTMLDivElement;
</script>
{#if channel}
<div bind:this={threadRoot} class="flex flex-col w-full h-full bg-base-0 dark:bg-base-900">
<div class="sticky top-0 flex min-h-10 items-center justify-between px-4 py-1.5">
<div class=" font-medium text-sm">{$i18n.t('Thread')}</div>
<div>
<button
type="button"
aria-label={$i18n.t('Close')}
class="flex h-7 items-center justify-center rounded-lg text-base-500 hover:text-base-700 dark:text-base-400 dark:hover:text-base-300 px-0"
on:click={() => {
onClose();
}}
>
<XMark className="size-4" />
</button>
</div>
</div>
<div
class="flex-1 min-h-0 w-full overflow-y-auto will-change-transform"
bind:this={messagesContainerElement}
>
<div>
{#if searchAnchor}<div class="px-3 py-2 text-xs text-base-500 flex justify-between">
<span>{$i18n.t('Showing search match')}</span><button
class="underline"
on:click={() => initHandler()}>{$i18n.t('Latest replies')}</button
>
</div>{/if}
{#if messages !== null}
<Messages
id={threadId}
{channel}
{top}
bind:messages
{replyToMessage}
thread={true}
{onPin}
onReply={async (message) => {
replyToMessage = message;
await tick();
chatInputElement?.focus();
}}
onLoad={async () => {
const newMessages = await getChannelThreadMessages(
localStorage.token,
channel.id,
threadId,
searchAnchor ? 0 : messages.length,
50,
searchAnchor ? { before: messages[messages.length - 1]?.id } : {}
);
messages = [
...messages,
...newMessages.filter(
(item) => !messages.some((existing) => existing.id === item.id)
)
];
if (newMessages.length < 50) {
top = true;
return;
}
}}
/>
{:else}
<div class="w-full flex justify-center pt-5 pb-10">
<Spinner />
</div>
{/if}
</div>
</div>
<div class=" pb-3 px-3 w-full">
<MessageInput
{messages}
bind:replyToMessage
bind:chatInputElement
id={threadId}
{channel}
disabled={!channel?.write_access}
placeholder={!channel?.write_access
? $i18n.t('You do not have permission to send messages in this thread.')
: $i18n.t('Reply to thread...')}
typingUsersClassName="from-base-0 dark:from-base-900"
{typingUsers}
userSuggestions={true}
channelSuggestions={true}
{onChange}
onSubmit={submitHandler}
/>
</div>
</div>
{/if}