diff --git a/src/lib/components/interfaces/Message.ts b/src/lib/components/interfaces/Message.ts deleted file mode 100644 index 225481e..0000000 --- a/src/lib/components/interfaces/Message.ts +++ /dev/null @@ -1,8 +0,0 @@ -interface Message { - message: string; - date: Date; - sender: string; - type: string; -} - -export default Message; \ No newline at end of file diff --git a/src/lib/services/messages.ts b/src/lib/services/messages.ts index 3de518f..6a13cfb 100644 --- a/src/lib/services/messages.ts +++ b/src/lib/services/messages.ts @@ -6,24 +6,24 @@ function isClientSide() { } // Safely get item from localStorage -function getFromLocalStorage(key: string, defaultValue: any) { +function getFromLocalStorage(key, defaultValue) { if (isClientSide()) { return localStorage.getItem(key) - ? JSON.parse(localStorage.getItem(key)!) + ? JSON.parse(localStorage.getItem(key)) : defaultValue; } return defaultValue; } // Safely set item to localStorage -function setToLocalStorage(key: string, value: any) { +function setToLocalStorage(key, value) { if (isClientSide()) { localStorage.setItem(key, JSON.stringify(value)); } } // Define the updated Message interface -interface Message { +export interface Message { text: string; timestamp: string; sender: string; @@ -31,8 +31,9 @@ interface Message { } // Load messages from localStorage or set an empty array if not available -const initialMessages: Message[] = getFromLocalStorage('chat-messages', []); +const initialMessages = getFromLocalStorage('chat-messages', []); +// Convert the array to a writable store export const messages = writable(initialMessages); // Subscribe to messages store to watch for changes and save them to localStorage @@ -40,28 +41,14 @@ messages.subscribe(currentMessages => { setToLocalStorage('chat-messages', currentMessages); }); -// Dummy messages -const messagesList: Message[] = [ - { text: "Hello there!", timestamp: new Date().toLocaleString(), sender: "John", type: "text" }, - { text: "How are you?", timestamp: new Date().toLocaleString(), sender: "Alice", type: "text" }, - // Add more dummy messages here with the timestamp and sender properties -]; -export function addRandomMessage() { - const randomMessage = messagesList[Math.floor(Math.random() * messagesList.length)]; - messages.update(oldMessages => [ - ...oldMessages, - { ...randomMessage, timestamp: new Date().toLocaleString(), sender: "Bot", type: "chat" } - ]); -} - -export function createMessage(text: string, sender: string, type: string) { +export function createMessage(messageData) { const currentDate = new Date().toLocaleString(); - const newMessageObj: Message = { - text: text, + const newMessageObj = { + text: messageData.text, timestamp: currentDate, - sender: sender, - type: type + sender: messageData.sender, + type: messageData.type }; messages.update(oldMessages => [...oldMessages, newMessageObj]); } @@ -69,3 +56,10 @@ export function createMessage(text: string, sender: string, type: string) { export function clearMessages() { messages.set([]); } + +// Dummy messages +export const messagesList: Message[] = [ + { text: "Hello there!", timestamp: new Date().toLocaleString(), sender: "John", type: "text" }, + { text: "How are you?", timestamp: new Date().toLocaleString(), sender: "Alice", type: "text" }, + // Add more dummy messages here with the timestamp and sender properties +]; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index ff77111..dbc9df3 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -5,7 +5,7 @@ import Send from '$lib/Send.svelte'; import Balance from '$lib/Balance.svelte'; import WalletConnect from '$lib/WalletConnect.svelte'; - import GoogleAuth from '$lib/GoogleAuth.svelte'; + import GoogleAuth from '$lib/components/GoogleAuth.svelte'; import Messages from '$lib/components/Messages.svelte'; import MessageInput from '$lib/components/MessageInput.svelte'; let pkpWallet;