open-webui/src/lib/components/channel/Messages/Message.svelte

358 lines
11 KiB
Svelte
Raw Normal View History

2024-12-23 02:40:01 +00:00
<script lang="ts">
2024-12-23 04:56:51 +00:00
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import isToday from 'dayjs/plugin/isToday';
import isYesterday from 'dayjs/plugin/isYesterday';
dayjs.extend(relativeTime);
dayjs.extend(isToday);
dayjs.extend(isYesterday);
2024-12-30 10:20:09 +00:00
import { getContext, onMount } from 'svelte';
2024-12-23 04:56:51 +00:00
const i18n = getContext<Writable<i18nType>>('i18n');
2024-12-30 10:20:09 +00:00
import { settings, user, shortCodesToEmojis } from '$lib/stores';
2024-12-23 04:56:51 +00:00
import { WEBUI_BASE_URL } from '$lib/constants';
2024-12-23 02:40:01 +00:00
import Markdown from '$lib/components/chat/Messages/Markdown.svelte';
2024-12-23 04:56:51 +00:00
import ProfileImage from '$lib/components/chat/Messages/ProfileImage.svelte';
import Name from '$lib/components/chat/Messages/Name.svelte';
2024-12-23 07:53:45 +00:00
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
import Pencil from '$lib/components/icons/Pencil.svelte';
2024-12-23 08:03:14 +00:00
import Tooltip from '$lib/components/common/Tooltip.svelte';
2024-12-23 08:06:59 +00:00
import Textarea from '$lib/components/common/Textarea.svelte';
2024-12-23 21:43:58 +00:00
import Image from '$lib/components/common/Image.svelte';
import FileItem from '$lib/components/common/FileItem.svelte';
2024-12-27 07:29:33 +00:00
import ProfilePreview from './Message/ProfilePreview.svelte';
2024-12-30 07:16:18 +00:00
import ChatBubbleOvalEllipsis from '$lib/components/icons/ChatBubbleOvalEllipsis.svelte';
import FaceSmile from '$lib/components/icons/FaceSmile.svelte';
2024-12-30 10:20:09 +00:00
import ReactionPicker from './Message/ReactionPicker.svelte';
2024-12-31 10:05:11 +00:00
import ChevronRight from '$lib/components/icons/ChevronRight.svelte';
2024-12-23 02:40:01 +00:00
export let message;
2024-12-23 04:56:51 +00:00
export let showUserProfile = true;
2024-12-31 07:55:19 +00:00
export let thread = false;
2024-12-23 04:56:51 +00:00
2024-12-23 07:53:45 +00:00
export let onDelete: Function = () => {};
export let onEdit: Function = () => {};
2024-12-31 07:48:55 +00:00
export let onThread: Function = () => {};
2024-12-31 07:06:34 +00:00
export let onReaction: Function = () => {};
2024-12-30 10:20:09 +00:00
let showButtons = false;
2024-12-23 07:53:45 +00:00
let edit = false;
let editedContent = null;
let showDeleteConfirmDialog = false;
2024-12-23 04:56:51 +00:00
const formatDate = (inputDate) => {
const date = dayjs(inputDate);
const now = dayjs();
if (date.isToday()) {
return `Today at ${date.format('HH:mm')}`;
} else if (date.isYesterday()) {
return `Yesterday at ${date.format('HH:mm')}`;
} else {
return `${date.format('DD/MM/YYYY')} at ${date.format('HH:mm')}`;
}
};
2024-12-23 02:40:01 +00:00
</script>
2024-12-23 07:53:45 +00:00
<ConfirmDialog
bind:show={showDeleteConfirmDialog}
title={$i18n.t('Delete Message')}
message={$i18n.t('Are you sure you want to delete this message?')}
onConfirm={async () => {
2024-12-23 08:12:55 +00:00
await onDelete();
2024-12-23 07:53:45 +00:00
}}
/>
2024-12-23 02:40:01 +00:00
{#if message}
2024-12-23 04:56:51 +00:00
<div
class="flex flex-col justify-between px-5 {showUserProfile
2024-12-23 07:41:22 +00:00
? 'pt-1.5 pb-0.5'
2024-12-23 04:56:51 +00:00
: ''} w-full {($settings?.widescreenMode ?? null)
? 'max-w-full'
2024-12-30 07:16:18 +00:00
: 'max-w-5xl'} mx-auto group hover:bg-gray-300/5 dark:hover:bg-gray-700/5 transition relative"
2024-12-23 04:56:51 +00:00
>
2024-12-31 10:16:07 +00:00
{#if !edit}
2024-12-30 10:20:09 +00:00
<div
2024-12-31 11:57:43 +00:00
class=" absolute {showButtons ? '' : 'invisible group-hover:visible'} right-1 -top-2 z-10"
2024-12-30 10:20:09 +00:00
>
2024-12-23 08:03:59 +00:00
<div
class="flex gap-1 rounded-lg bg-white dark:bg-gray-850 shadow-md p-0.5 border border-gray-100 dark:border-gray-800"
2024-12-23 07:53:45 +00:00
>
2024-12-31 07:06:34 +00:00
<ReactionPicker
onClose={() => (showButtons = false)}
onSubmit={(name) => {
showButtons = false;
onReaction(name);
}}
>
2024-12-30 10:20:09 +00:00
<Tooltip content={$i18n.t('Add Reaction')}>
<button
class="hover:bg-gray-100 dark:hover:bg-gray-800 transition rounded-lg p-1"
on:click={() => {
showButtons = true;
}}
>
<FaceSmile />
</button>
</Tooltip>
</ReactionPicker>
2024-12-30 07:16:18 +00:00
2024-12-31 07:55:19 +00:00
{#if !thread}
2024-12-31 07:48:55 +00:00
<Tooltip content={$i18n.t('Reply in Thread')}>
<button
class="hover:bg-gray-100 dark:hover:bg-gray-800 transition rounded-lg p-1"
on:click={() => {
onThread(message.id);
}}
>
<ChatBubbleOvalEllipsis />
</button>
</Tooltip>
{/if}
2024-12-30 07:16:18 +00:00
2024-12-31 10:16:07 +00:00
{#if message.user_id === $user.id || $user.role === 'admin'}
<Tooltip content={$i18n.t('Edit')}>
<button
class="hover:bg-gray-100 dark:hover:bg-gray-800 transition rounded-lg p-1"
on:click={() => {
edit = true;
editedContent = message.content;
}}
>
<Pencil />
</button>
</Tooltip>
2024-12-30 07:16:18 +00:00
2024-12-31 10:16:07 +00:00
<Tooltip content={$i18n.t('Delete')}>
<button
class="hover:bg-gray-100 dark:hover:bg-gray-800 transition rounded-lg p-1"
on:click={() => (showDeleteConfirmDialog = true)}
>
<GarbageBin />
</button>
</Tooltip>
{/if}
2024-12-23 08:03:59 +00:00
</div>
2024-12-23 07:53:45 +00:00
</div>
2024-12-23 08:03:59 +00:00
{/if}
2024-12-23 07:53:45 +00:00
2024-12-23 04:56:51 +00:00
<div
class=" flex w-full message-{message.id}"
id="message-{message.id}"
dir={$settings.chatDirection}
>
<div
2024-12-23 08:22:04 +00:00
class={`flex-shrink-0 ${($settings?.chatDirection ?? 'LTR') === 'LTR' ? 'mr-3' : 'ml-3'} w-9`}
2024-12-23 04:56:51 +00:00
>
{#if showUserProfile}
2024-12-27 07:29:33 +00:00
<ProfilePreview user={message.user}>
<ProfileImage
src={message.user?.profile_image_url ??
($i18n.language === 'dg-DG' ? `/doge.png` : `${WEBUI_BASE_URL}/static/favicon.png`)}
className={'size-8 translate-y-1 ml-0.5'}
/>
</ProfilePreview>
2024-12-23 04:56:51 +00:00
{:else}
2024-12-23 07:41:22 +00:00
<!-- <div class="w-7 h-7 rounded-full bg-transparent" /> -->
{#if message.created_at}
2024-12-23 08:03:14 +00:00
<div
2024-12-23 08:22:04 +00:00
class="mt-1.5 flex flex-shrink-0 items-center text-xs self-center invisible group-hover:visible text-gray-500 font-medium first-letter:capitalize"
2024-12-23 07:41:22 +00:00
>
2024-12-23 08:03:14 +00:00
<Tooltip
content={dayjs(message.created_at / 1000000).format('dddd, DD MMMM YYYY HH:mm')}
>
{dayjs(message.created_at / 1000000).format('HH:mm')}
</Tooltip>
</div>
2024-12-23 07:41:22 +00:00
{/if}
2024-12-23 04:56:51 +00:00
{/if}
</div>
<div class="flex-auto w-0 pl-1">
{#if showUserProfile}
<Name>
2025-01-03 04:28:01 +00:00
<div class=" self-end text-base shrink-0 font-medium truncate">
2024-12-23 07:41:22 +00:00
{message?.user?.name}
2024-12-23 08:03:14 +00:00
</div>
2024-12-23 04:56:51 +00:00
{#if message.created_at}
2024-12-23 08:03:14 +00:00
<div
2024-12-25 03:16:02 +00:00
class=" self-center text-xs invisible group-hover:visible text-gray-400 font-medium first-letter:capitalize ml-0.5 translate-y-[1px]"
2024-12-23 04:56:51 +00:00
>
2024-12-23 08:03:14 +00:00
<Tooltip
content={dayjs(message.created_at / 1000000).format('dddd, DD MMMM YYYY HH:mm')}
>
2025-01-03 04:28:01 +00:00
<span class="line-clamp-1">{formatDate(message.created_at / 1000000)}</span>
2024-12-23 08:03:14 +00:00
</Tooltip>
</div>
2024-12-23 04:56:51 +00:00
{/if}
</Name>
{/if}
2024-12-23 21:43:58 +00:00
{#if (message?.data?.files ?? []).length > 0}
<div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
{#each message?.data?.files as file}
<div>
{#if file.type === 'image'}
<Image src={file.url} alt={file.name} imageClassName=" max-h-96 rounded-lg" />
{:else}
<FileItem
item={file}
url={file.url}
name={file.name}
type={file.type}
size={file?.size}
colorClassName="bg-white dark:bg-gray-850 "
/>
{/if}
</div>
{/each}
</div>
{/if}
2024-12-23 07:53:45 +00:00
{#if edit}
2024-12-23 08:06:59 +00:00
<div class="py-2">
<Textarea
2024-12-23 08:22:43 +00:00
className=" bg-transparent outline-none w-full resize-none"
2024-12-23 07:53:45 +00:00
bind:value={editedContent}
2024-12-23 08:06:59 +00:00
onKeydown={(e) => {
2024-12-23 07:53:45 +00:00
if (e.key === 'Escape') {
document.getElementById('close-edit-message-button')?.click();
}
const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
const isEnterPressed = e.key === 'Enter';
if (isCmdOrCtrlPressed && isEnterPressed) {
document.getElementById('confirm-edit-message-button')?.click();
}
}}
/>
<div class=" mt-2 mb-1 flex justify-end text-sm font-medium">
<div class="flex space-x-1.5">
<button
id="close-edit-message-button"
class="px-4 py-2 bg-white dark:bg-gray-900 hover:bg-gray-100 text-gray-800 dark:text-gray-100 transition rounded-3xl"
on:click={() => {
edit = false;
editedContent = null;
}}
>
{$i18n.t('Cancel')}
</button>
<button
id="confirm-edit-message-button"
class=" px-4 py-2 bg-gray-900 dark:bg-white hover:bg-gray-850 text-gray-100 dark:text-gray-800 transition rounded-3xl"
on:click={async () => {
2024-12-23 08:12:55 +00:00
onEdit(editedContent);
2024-12-23 07:53:45 +00:00
edit = false;
editedContent = null;
}}
>
{$i18n.t('Save')}
</button>
</div>
</div>
</div>
{:else}
2024-12-23 08:03:14 +00:00
<div class=" min-w-full markdown-prose">
2024-12-23 08:19:30 +00:00
<Markdown
id={message.id}
content={message.content}
/>{#if message.created_at !== message.updated_at}<span class="text-gray-500 text-[10px]"
>(edited)</span
>{/if}
2024-12-23 07:53:45 +00:00
</div>
2024-12-30 10:20:09 +00:00
2024-12-31 07:12:50 +00:00
{#if (message?.reactions ?? []).length > 0}
2024-12-30 10:20:09 +00:00
<div>
2024-12-31 07:12:50 +00:00
<div class="flex items-center flex-wrap gap-y-1.5 gap-1 mt-1 mb-2">
2024-12-31 07:06:34 +00:00
{#each message.reactions as reaction}
<Tooltip content={`:${reaction.name}:`}>
<button
class="flex items-center gap-1.5 transition rounded-xl px-2 py-1 cursor-pointer {reaction.user_ids.includes(
$user.id
)
2024-12-31 07:16:50 +00:00
? ' bg-blue-300/10 outline outline-blue-500/50 outline-1'
: 'bg-gray-300/10 dark:bg-gray-500/10 hover:outline hover:outline-gray-700/30 dark:hover:outline-gray-300/30 hover:outline-1'}"
2024-12-31 07:06:34 +00:00
on:click={() => {
onReaction(reaction.name);
}}
>
{#if $shortCodesToEmojis[reaction.name]}
<img
src="/assets/emojis/{$shortCodesToEmojis[
reaction.name
].toLowerCase()}.svg"
alt={reaction.name}
class=" size-4"
2024-12-31 07:48:55 +00:00
loading="lazy"
2024-12-31 07:06:34 +00:00
/>
{:else}
<div>
{reaction.name}
</div>
{/if}
2024-12-30 10:20:09 +00:00
2024-12-31 07:06:34 +00:00
{#if reaction.user_ids.length > 0}
<div class="text-xs font-medium text-gray-500 dark:text-gray-400">
{reaction.user_ids?.length}
</div>
{/if}
</button>
</Tooltip>
2024-12-30 10:20:09 +00:00
{/each}
2024-12-31 07:06:34 +00:00
<ReactionPicker
onSubmit={(name) => {
onReaction(name);
}}
>
2024-12-30 10:20:09 +00:00
<Tooltip content={$i18n.t('Add Reaction')}>
<div
class="flex items-center gap-1.5 bg-gray-500/10 hover:outline hover:outline-gray-700/30 dark:hover:outline-gray-300/30 hover:outline-1 transition rounded-xl px-1 py-1 cursor-pointer text-gray-500 dark:text-gray-400"
>
<FaceSmile />
</div>
</Tooltip>
</ReactionPicker>
</div>
</div>
{/if}
2024-12-31 10:05:11 +00:00
2024-12-31 10:16:07 +00:00
{#if !thread && message.reply_count > 0}
2024-12-31 10:05:11 +00:00
<div class="flex items-center gap-1.5 -mt-0.5 mb-1.5">
<button
class="flex items-center text-xs py-1 text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 transition"
on:click={() => {
onThread(message.id);
}}
>
<span class="font-medium mr-1">
{$i18n.t('{{COUNT}} Replies', { COUNT: message.reply_count })}</span
><span>
{' - '}{$i18n.t('Last reply')}
{dayjs.unix(message.latest_reply_at / 1000000000).fromNow()}</span
>
<span class="ml-1">
<ChevronRight className="size-2.5" strokeWidth="3" />
</span>
<!-- {$i18n.t('View Replies')} -->
</button>
</div>
{/if}
2024-12-23 07:53:45 +00:00
{/if}
2024-12-23 04:56:51 +00:00
</div>
2024-12-23 02:40:01 +00:00
</div>
</div>
{/if}