open-webui/src/lib/components/layout/Sidebar/ChannelItem.svelte

183 lines
4.7 KiB
Svelte
Raw Normal View History

2024-12-22 11:10:10 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
2024-12-23 06:09:51 +00:00
import { onMount, getContext, tick, onDestroy } from 'svelte';
2024-12-22 11:10:10 +00:00
const i18n = getContext('i18n');
2024-12-23 03:28:15 +00:00
import { page } from '$app/stores';
import { channels, mobile, showSidebar, user } from '$lib/stores';
2025-11-27 12:27:32 +00:00
import { updateChannelById, updateChannelMemberActiveStatusById } from '$lib/apis/channels';
import { WEBUI_API_BASE_URL } from '$lib/constants';
2024-12-23 06:09:51 +00:00
import Cog6 from '$lib/components/icons/Cog6.svelte';
import ChannelModal from './ChannelModal.svelte';
2025-09-17 02:41:47 +00:00
import Lock from '$lib/components/icons/Lock.svelte';
import Hashtag from '$lib/components/icons/Hashtag.svelte';
2025-11-27 12:27:32 +00:00
import Users from '$lib/components/icons/Users.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
2024-12-23 06:09:51 +00:00
export let onUpdate: Function = () => {};
2024-12-22 11:10:10 +00:00
export let className = '';
2024-12-23 06:09:51 +00:00
export let channel;
2024-12-22 11:10:10 +00:00
2024-12-23 06:09:51 +00:00
let showEditChannelModal = false;
2024-12-22 11:10:10 +00:00
let itemElement;
</script>
2024-12-23 06:09:51 +00:00
<ChannelModal
bind:show={showEditChannelModal}
{channel}
edit={true}
2024-12-23 06:15:29 +00:00
{onUpdate}
2024-12-23 06:09:51 +00:00
onSubmit={async ({ name, access_control }) => {
const res = await updateChannelById(localStorage.token, channel.id, {
name,
access_control
}).catch((error) => {
toast.error(error.message);
});
if (res) {
2025-08-14 00:15:16 +00:00
toast.success($i18n.t('Channel updated successfully'));
2024-12-23 06:09:51 +00:00
}
onUpdate();
}}
/>
2024-12-22 11:10:10 +00:00
<div
2025-09-25 16:23:58 +00:00
id="sidebar-channel-item"
2024-12-22 11:10:10 +00:00
bind:this={itemElement}
2025-09-20 06:15:27 +00:00
class=" w-full {className} rounded-xl flex relative group hover:bg-gray-100 dark:hover:bg-gray-900 {$page
2024-12-23 06:09:51 +00:00
.url.pathname === `/channels/${channel.id}`
2025-09-25 16:23:58 +00:00
? 'bg-gray-100 dark:bg-gray-900 selected'
2025-11-27 12:27:32 +00:00
: ''} {channel?.type === 'dm' ? 'px-1 py-[3px]' : 'p-1'} {channel?.unread_count > 0
? 'font-medium dark:text-white text-black'
: ' dark:text-gray-400 text-gray-600'} cursor-pointer select-none"
2024-12-22 11:10:10 +00:00
>
<a
class=" w-full flex justify-between"
2024-12-23 06:09:51 +00:00
href="/channels/{channel.id}"
2024-12-22 11:10:10 +00:00
on:click={() => {
2025-09-17 02:41:47 +00:00
console.log(channel);
if ($channels) {
channels.set(
$channels.map((ch) => {
if (ch.id === channel.id) {
ch.unread_count = 0;
}
return ch;
})
);
}
2024-12-22 11:10:10 +00:00
if ($mobile) {
showSidebar.set(false);
}
}}
draggable="false"
>
2025-11-27 12:27:32 +00:00
<div class="flex items-center gap-1">
<div>
{#if channel?.type === 'dm'}
{#if channel?.users}
<div class="flex ml-[1px] mr-0.5">
{#each channel.users.filter((u) => u.id !== $user?.id).slice(0, 2) as u, index}
<img
src={`${WEBUI_API_BASE_URL}/users/${u.id}/profile/image`}
alt={u.name}
class=" size-5.5 rounded-full border-2 border-white dark:border-gray-900 {index ===
1
? '-ml-2.5'
: ''}"
/>
{/each}
</div>
{:else}
<Users className="size-4 ml-1 mr-0.5" strokeWidth="2" />
{/if}
2025-09-17 02:41:47 +00:00
{:else}
2025-11-27 12:27:32 +00:00
<div class=" size-4 justify-center flex items-center ml-1">
{#if channel?.access_control === null}
<Hashtag className="size-3.5" strokeWidth="2.5" />
{:else}
<Lock className="size-[15px]" strokeWidth="2" />
{/if}
</div>
2025-09-17 02:41:47 +00:00
{/if}
</div>
2024-12-22 11:10:10 +00:00
2025-11-27 12:27:32 +00:00
<div class=" text-left self-center overflow-hidden w-full line-clamp-1 flex-1 pr-1">
{#if channel?.name}
{channel.name}
{:else}
{channel?.users
?.filter((u) => u.id !== $user?.id)
.map((u) => u.name)
.join(', ')}
{/if}
2024-12-22 11:10:10 +00:00
</div>
</div>
<div class="flex items-center">
{#if channel?.unread_count > 0}
<div
class="text-xs py-[1px] px-2 rounded-xl bg-gray-100 text-black dark:bg-gray-800 dark:text-white font-medium"
>
{new Intl.NumberFormat($i18n.locale, {
notation: 'compact',
compactDisplay: 'short'
}).format(channel.unread_count)}
</div>
{/if}
2025-10-27 04:06:32 +00:00
</div>
</a>
2025-11-27 12:27:32 +00:00
{#if channel?.type === 'dm'}
<div
class="ml-0.5 mr-1 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
>
<button
type="button"
class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto"
on:click={async (e) => {
e.stopImmediatePropagation();
e.stopPropagation();
channels.update((chs) =>
chs.filter((ch) => {
return ch.id !== channel.id;
})
);
await updateChannelMemberActiveStatusById(localStorage.token, channel.id, false).catch(
(error) => {
toast.error(`${error}`);
}
);
}}
>
<XMark className="size-3.5" />
</button>
</div>
{:else if $user?.role === 'admin'}
<div
class="ml-0.5 mr-1 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
>
<button
type="button"
class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto"
on:click={(e) => {
e.stopImmediatePropagation();
e.stopPropagation();
showEditChannelModal = true;
}}
>
<Cog6 className="size-3.5" />
</button>
</div>
{/if}
2024-12-22 11:10:10 +00:00
</div>