2024-04-27 22:24:59 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { toast } from 'svelte-sonner';
|
2025-05-24 21:44:53 +00:00
|
|
|
import { getContext } from 'svelte';
|
|
|
|
|
|
2024-04-27 22:24:59 +00:00
|
|
|
import dayjs from 'dayjs';
|
2025-02-03 21:37:29 +00:00
|
|
|
import localizedFormat from 'dayjs/plugin/localizedFormat';
|
2024-04-27 22:24:59 +00:00
|
|
|
|
2025-02-03 21:37:29 +00:00
|
|
|
dayjs.extend(localizedFormat);
|
2024-04-27 22:24:59 +00:00
|
|
|
|
|
|
|
|
import { getChatListByUserId, deleteChatById, getArchivedChatList } from '$lib/apis/chats';
|
2024-11-13 05:51:42 +00:00
|
|
|
|
|
|
|
|
import Modal from '$lib/components/common/Modal.svelte';
|
2024-04-27 22:24:59 +00:00
|
|
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
2024-12-01 07:33:19 +00:00
|
|
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
2025-05-24 21:44:53 +00:00
|
|
|
import ChatsModal from '$lib/components/layout/ChatsModal.svelte';
|
2024-04-27 22:24:59 +00:00
|
|
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
|
|
export let show = false;
|
|
|
|
|
export let user;
|
|
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
let chatList = null;
|
|
|
|
|
let page = 1;
|
2024-04-27 22:24:59 +00:00
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
let query = '';
|
|
|
|
|
let orderBy = 'updated_at';
|
|
|
|
|
let direction = 'desc';
|
2024-04-27 22:24:59 +00:00
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
let filter = {};
|
|
|
|
|
$: filter = {
|
|
|
|
|
...(query ? { query } : {}),
|
|
|
|
|
...(orderBy ? { order_by: orderBy } : {}),
|
|
|
|
|
...(direction ? { direction } : {})
|
2024-04-27 22:24:59 +00:00
|
|
|
};
|
|
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
$: if (filter !== null) {
|
|
|
|
|
searchHandler();
|
2024-04-27 22:24:59 +00:00
|
|
|
}
|
2024-06-13 23:24:52 +00:00
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
let allChatsLoaded = false;
|
|
|
|
|
let chatListLoading = false;
|
|
|
|
|
|
|
|
|
|
let searchDebounceTimeout;
|
|
|
|
|
|
|
|
|
|
const searchHandler = async () => {
|
2025-09-18 22:47:32 +00:00
|
|
|
if (!show) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
if (searchDebounceTimeout) {
|
|
|
|
|
clearTimeout(searchDebounceTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page = 1;
|
|
|
|
|
chatList = null;
|
|
|
|
|
|
|
|
|
|
if (query === '') {
|
|
|
|
|
chatList = await getChatListByUserId(localStorage.token, user.id, page, filter);
|
2024-06-13 23:24:52 +00:00
|
|
|
} else {
|
2025-05-24 21:44:53 +00:00
|
|
|
searchDebounceTimeout = setTimeout(async () => {
|
|
|
|
|
chatList = await getChatListByUserId(localStorage.token, user.id, page, filter);
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((chatList ?? []).length === 0) {
|
|
|
|
|
allChatsLoaded = true;
|
|
|
|
|
} else {
|
|
|
|
|
allChatsLoaded = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const loadMoreChats = async () => {
|
|
|
|
|
chatListLoading = true;
|
|
|
|
|
page += 1;
|
|
|
|
|
|
|
|
|
|
let newChatList = [];
|
|
|
|
|
|
|
|
|
|
newChatList = await getChatListByUserId(localStorage.token, user.id, page, filter);
|
|
|
|
|
|
|
|
|
|
// once the bottom of the list has been reached (no results) there is no need to continue querying
|
|
|
|
|
allChatsLoaded = newChatList.length === 0;
|
|
|
|
|
|
|
|
|
|
if (newChatList.length > 0) {
|
|
|
|
|
chatList = [...chatList, ...newChatList];
|
2024-06-13 23:24:52 +00:00
|
|
|
}
|
2025-05-24 21:44:53 +00:00
|
|
|
|
|
|
|
|
chatListLoading = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const init = async () => {
|
|
|
|
|
chatList = await getChatListByUserId(localStorage.token, user.id, page, filter);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$: if (show) {
|
|
|
|
|
init();
|
|
|
|
|
} else {
|
|
|
|
|
chatList = null;
|
|
|
|
|
page = 1;
|
|
|
|
|
|
|
|
|
|
allChatsLoaded = false;
|
|
|
|
|
chatListLoading = false;
|
2024-06-13 23:24:52 +00:00
|
|
|
}
|
2024-04-27 22:24:59 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-05-24 21:44:53 +00:00
|
|
|
<ChatsModal
|
|
|
|
|
bind:show
|
|
|
|
|
bind:query
|
|
|
|
|
bind:orderBy
|
|
|
|
|
bind:direction
|
2025-09-26 22:25:17 +00:00
|
|
|
title={$i18n.t("{{user}}'s Chats", {
|
|
|
|
|
user: user.name.length > 32 ? `${user.name.slice(0, 32)}...` : user.name
|
|
|
|
|
})}
|
2025-05-24 21:44:53 +00:00
|
|
|
emptyPlaceholder={$i18n.t('No chats found for this user.')}
|
2025-05-24 21:59:48 +00:00
|
|
|
shareUrl={true}
|
2025-05-24 21:44:53 +00:00
|
|
|
{chatList}
|
|
|
|
|
{allChatsLoaded}
|
|
|
|
|
{chatListLoading}
|
|
|
|
|
onUpdate={() => {
|
|
|
|
|
init();
|
2025-03-08 18:42:24 +00:00
|
|
|
}}
|
2025-05-24 21:44:53 +00:00
|
|
|
loadHandler={loadMoreChats}
|
|
|
|
|
></ChatsModal>
|