Ajout de l'update en temps reel du délai des messages et scroll up auto quand un chat est créé.
This commit is contained in:
parent
6f340c6b85
commit
872573d6c5
5 changed files with 55 additions and 5 deletions
|
@ -1,11 +1,32 @@
|
|||
<script lang="ts">
|
||||
import * as Card from "$lib/components/ui/card";
|
||||
import { formatDistanceToNow } from "$lib/utils/date.js";
|
||||
import { onMount, onDestroy } from "svelte";
|
||||
|
||||
export let username: string;
|
||||
export let messageContent: string;
|
||||
export let profilePicture: string | null; // Peut être null
|
||||
export let createdAt: string; // Date de création du message
|
||||
|
||||
let defaultProfilePicture = "/images/default-profile.png";
|
||||
|
||||
// Temps écoulé (calculé périodiquement)
|
||||
let timeElapsed: string;
|
||||
|
||||
// Fonction pour mettre à jour le temps écoulé
|
||||
const updateElapsed = () => {
|
||||
timeElapsed = formatDistanceToNow(createdAt);
|
||||
};
|
||||
|
||||
// Initialisation de l'intervalle
|
||||
onMount(() => {
|
||||
updateElapsed(); // Calcul initial
|
||||
const interval = setInterval(updateElapsed, 1000); // Mise à jour toutes les secondes
|
||||
|
||||
return () => {
|
||||
clearInterval(interval); // Nettoyage lors du démontage
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<Card.Root class="relative">
|
||||
|
@ -13,7 +34,7 @@
|
|||
<!-- Image de profil collée à gauche -->
|
||||
<div class="flex flex-row gap-3 items-center">
|
||||
<img
|
||||
src={`http://localhost:5173/${profilePicture}`}
|
||||
src={profilePicture || defaultProfilePicture}
|
||||
alt="Profile Picture"
|
||||
class="h-10 w-10 rounded-full border border-gray-300"
|
||||
/>
|
||||
|
@ -24,8 +45,9 @@
|
|||
</Card.Title>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Temps depuis la création -->
|
||||
<span class="text-xs sm:text-sm md:text-base text-gray-500 items-top">
|
||||
{formatDistanceToNow(createdAt)}
|
||||
{timeElapsed}
|
||||
</span>
|
||||
</Card.Header>
|
||||
|
||||
|
|
|
@ -1,10 +1,28 @@
|
|||
<script lang="ts">
|
||||
import { formatDistanceToNow } from '$lib/utils/date';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
export let id: string; // ID du chat
|
||||
export let title: string; // Nom ou titre du chat
|
||||
export let lastMessage: string; // Dernier message affiché
|
||||
export let createdAt: string; // Heure du dernier message
|
||||
|
||||
let timeElapsed: string;
|
||||
|
||||
// Fonction pour mettre à jour le temps écoulé
|
||||
const updateElapsed = () => {
|
||||
timeElapsed = formatDistanceToNow(createdAt);
|
||||
};
|
||||
|
||||
// Initialisation de l'intervalle
|
||||
onMount(() => {
|
||||
updateElapsed(); // Calcul initial
|
||||
const interval = setInterval(updateElapsed, 1000); // Mise à jour toutes les secondes
|
||||
|
||||
return () => {
|
||||
clearInterval(interval); // Nettoyage lors du démontage
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<a href={`/chats/${id}`} class="chat-item p-4 border rounded-md hover:bg-gray-100 cursor-pointer flex justify-between items-center">
|
||||
|
@ -12,7 +30,7 @@
|
|||
<p class="font-semibold text-lg">{title}</p>
|
||||
<p class="text-sm text-gray-500 truncate">{lastMessage}</p>
|
||||
</div>
|
||||
<p class="text-xs text-gray-400">{formatDistanceToNow(createdAt)}</p>
|
||||
<p class="text-xs text-gray-400">{timeElapsed}</p>
|
||||
</a>
|
||||
|
||||
<style>
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
export let show = false;
|
||||
|
||||
export let listRef: HTMLElement | null = null;
|
||||
|
||||
export let onClose: () => void; // Callback pour fermer le composant
|
||||
|
||||
let showAlert = false;
|
||||
|
@ -29,6 +31,9 @@
|
|||
alertMessage = `Le chat "${data.name}" a été créé avec succès.`;
|
||||
chatName = ""; // Réinitialiser
|
||||
socket.emit("new-channel", data);
|
||||
if (listRef) {
|
||||
listRef.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
}
|
||||
onClose?.(); // Fermer le composant après création
|
||||
} else {
|
||||
response.json().then(error => {
|
||||
|
|
|
@ -104,6 +104,7 @@ export async function POST({ request }) {
|
|||
canal = {
|
||||
...canal,
|
||||
lastMessage: null,
|
||||
lastUpdate: canal.createdAt,
|
||||
messages: undefined
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,16 @@
|
|||
import CreateChat from "$lib/components/ui/CreateChat.svelte"; // Importer le composant CreateChat
|
||||
import { initSocket } from "$lib/stores/socket";
|
||||
|
||||
let chatListRef: HTMLElement | null = null;
|
||||
|
||||
let showProfileCard = false; // État pour afficher ou masquer le ProfileCard
|
||||
let showCreateChat = false; // État pour afficher ou masquer CreateChat
|
||||
|
||||
let socket = initSocket(); // Initialiser le socket
|
||||
|
||||
socket.on("new-channel", (channel) => {
|
||||
console.log(channel);
|
||||
console.log(channels);
|
||||
channels = [channel, ...channels];
|
||||
});
|
||||
|
||||
|
@ -81,7 +85,7 @@
|
|||
</Button>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-4 overflow-y-auto">
|
||||
<div class="flex flex-col gap-4 overflow-y-auto" bind:this={chatListRef}>
|
||||
{#each channels as channel}
|
||||
<ChatItem
|
||||
id={channel.id}
|
||||
|
@ -93,7 +97,7 @@
|
|||
</div>
|
||||
|
||||
</div>
|
||||
<CreateChat show={showCreateChat} socket={socket} onClose={closeCreateChat} />
|
||||
<CreateChat show={showCreateChat} socket={socket} onClose={closeCreateChat} listRef={chatListRef} />
|
||||
<ProfileCard user={data.user} show={showProfileCard} onClose={closeProfileCard} />
|
||||
|
||||
<style>
|
||||
|
|
Loading…
Add table
Reference in a new issue