Updated the Messages to the new abstracted composite syntax.
This commit is contained in:
parent
920e7b7ca1
commit
979251bd6b
@ -1,5 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
import { clearMessages } from '$lib/services/messages';
|
import { clearMessages } from '$lib/services/messages/messages';
|
||||||
|
|
||||||
function handleClear() {
|
function handleClear() {
|
||||||
clearMessages();
|
clearMessages();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import { isSignInRedirect, getProviderFromUrl } from '@lit-protocol/lit-auth-client';
|
import { isSignInRedirect, getProviderFromUrl } from '@lit-protocol/lit-auth-client';
|
||||||
import type { IRelayPKP } from '@lit-protocol/types';
|
import type { IRelayPKP } from '@lit-protocol/types';
|
||||||
import { ProviderType } from '@lit-protocol/constants';
|
import { ProviderType } from '@lit-protocol/constants';
|
||||||
import { createMessage } from '$lib/services/messages';
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
import { createLitSession } from '$lib/services/createLitSession/createLitSession';
|
import { createLitSession } from '$lib/services/createLitSession/createLitSession';
|
||||||
|
|
||||||
const redirectUri = 'http://localhost:5173/';
|
const redirectUri = 'http://localhost:5173/';
|
||||||
|
@ -3,34 +3,37 @@
|
|||||||
export let services;
|
export let services;
|
||||||
export let store;
|
export let store;
|
||||||
|
|
||||||
// Watch for store changes
|
let isServicesLoaded = false;
|
||||||
|
let isStoreLoaded = false;
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if (services && services.helloEarthAlert) {
|
// Check services loading
|
||||||
console.log('Alerted by HelloEarthAlert');
|
if (services.helloEarthAlert) {
|
||||||
|
// services.helloEarthAlert.alertMe();
|
||||||
|
isServicesLoaded = true;
|
||||||
} else {
|
} else {
|
||||||
console.error('Services or helloEarthAlert not loaded');
|
console.error('helloEarthAlert not loaded');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$: if ($store) isStoreLoaded = true;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if !$store}
|
{#if isStoreLoaded}
|
||||||
<div>Loading store...</div>
|
<div class="p-12 bg-blue-400 rounded-md shadow-lg">
|
||||||
{:else}
|
<p class="mb-4 text-xl font-bold text-white">Hello Earth</p>
|
||||||
<div class="p-12 bg-blue-400">
|
{#if $store.pkpWallet}
|
||||||
Hello Earth
|
<p class="mb-6 text-white">Wallet Address: {$store.pkpWallet.address}</p>
|
||||||
{#if $store.pkpWallet}{$store.pkpWallet.address}{/if}
|
{/if}
|
||||||
{#if $store.todos && $store.messages}
|
Reference TodoList
|
||||||
<ul>
|
{#if $store.todos}
|
||||||
|
<ul class="pl-5 list-decimal">
|
||||||
{#each $store.todos as todo}
|
{#each $store.todos as todo}
|
||||||
<li>{todo.name}</li>
|
<li class="p-2 mb-2 text-gray-700 bg-white rounded shadow">{todo.text}</li>
|
||||||
{/each}
|
|
||||||
</ul>
|
|
||||||
<ul>
|
|
||||||
{#each $store.messages as message}
|
|
||||||
<li>{message.text} - {message.date}</li>
|
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="p-6 text-center bg-gray-100 rounded-md shadow-lg">Loading...</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
<script>
|
<script>
|
||||||
import { messages } from '$lib/services/messages';
|
|
||||||
import { onMount, afterUpdate } from 'svelte';
|
import { onMount, afterUpdate } from 'svelte';
|
||||||
import Composite from '$lib/core/Composite.svelte';
|
import Composite from '$lib/core/Composite.svelte';
|
||||||
|
|
||||||
let latestMessages = [];
|
export let store;
|
||||||
|
|
||||||
messages.subscribe((value) => {
|
let isStoreLoaded = false;
|
||||||
latestMessages = value;
|
|
||||||
});
|
|
||||||
|
|
||||||
let messagesContainer;
|
let messagesContainer;
|
||||||
|
|
||||||
@ -24,10 +21,13 @@
|
|||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
});
|
});
|
||||||
|
$: if ($store) isStoreLoaded = true;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main bind:this={messagesContainer} class="w-full h-full p-4 overflow-y-auto">
|
{#if isStoreLoaded}
|
||||||
{#each latestMessages as message}
|
{#if $store.messages}
|
||||||
|
<main bind:this={messagesContainer} class="w-full h-full p-4 overflow-y-auto">
|
||||||
|
{#each $store.messages as message}
|
||||||
<div class="p-3 mb-2 bg-white border-b border-gray-300 rounded-lg shadow-lg">
|
<div class="p-3 mb-2 bg-white border-b border-gray-300 rounded-lg shadow-lg">
|
||||||
<div class="flex items-center justify-between">
|
<div class="flex items-center justify-between">
|
||||||
<p class="text-sm text-gray-600">
|
<p class="text-sm text-gray-600">
|
||||||
@ -37,11 +37,14 @@
|
|||||||
</div>
|
</div>
|
||||||
<p class="mt-2 text-base text-gray-800">{message.text}</p>
|
<p class="mt-2 text-base text-gray-800">{message.text}</p>
|
||||||
</div>
|
</div>
|
||||||
<!-- Render Composite Component -->
|
|
||||||
{#if message.composite}
|
{#if message.composite}
|
||||||
<div class="overflow-y-auto max-h-80vh">
|
<div class="overflow-y-auto max-h-80vh">
|
||||||
<Composite composite={message.composite} />
|
<Composite composite={message.composite} />
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
</main>
|
</main>
|
||||||
|
{/if}
|
||||||
|
{:else}
|
||||||
|
<div>Loading...</div>
|
||||||
|
{/if}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
import { createMessage } from '$lib/services/messages';
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
|
|
||||||
export function sendMessage(text) {
|
export function sendMessage(text) {
|
||||||
if (text && text.trim() !== '') {
|
if (text && text.trim() !== '') {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onDestroy, onMount } from 'svelte';
|
import { onDestroy } from 'svelte';
|
||||||
import Composite from './Composite.svelte';
|
import Composite from './Composite.svelte';
|
||||||
import components from '$lib/core/componentLoader';
|
import components from '$lib/core/componentLoader';
|
||||||
import services from '$lib/core/servicesLoader';
|
import services from '$lib/core/servicesLoader';
|
||||||
@ -66,10 +66,20 @@
|
|||||||
if (externalID === 'data') {
|
if (externalID === 'data') {
|
||||||
const unsubscribe = dataStore.subscribe((store) => {
|
const unsubscribe = dataStore.subscribe((store) => {
|
||||||
if (externalKey in store) {
|
if (externalKey in store) {
|
||||||
|
// Check if the data item is a Svelte store
|
||||||
|
if (store[externalKey] && typeof store[externalKey].subscribe === 'function') {
|
||||||
|
let innerUnsub = store[externalKey].subscribe((value) => {
|
||||||
|
localStore.update((storeValue) => {
|
||||||
|
return { ...storeValue, [localKey]: value };
|
||||||
|
});
|
||||||
|
});
|
||||||
|
onDestroy(innerUnsub);
|
||||||
|
} else {
|
||||||
localStore.update((storeValue) => {
|
localStore.update((storeValue) => {
|
||||||
return { ...storeValue, [localKey]: store[externalKey] };
|
return { ...storeValue, [localKey]: store[externalKey] };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(unsubscribe);
|
onDestroy(unsubscribe);
|
||||||
|
@ -1,28 +1,8 @@
|
|||||||
|
// dataLoader.ts
|
||||||
|
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
|
import { queryMessagesData } from '$lib/data/queryMessages';
|
||||||
// Using mock data, in a real-world scenario this could be fetched from an API.
|
import { queryTodosData } from '$lib/data/queryTodos';
|
||||||
const queryMessagesData = [
|
|
||||||
{
|
|
||||||
text: 'text1',
|
|
||||||
date: 'date1',
|
|
||||||
id: 'id1'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'text2',
|
|
||||||
date: 'date2',
|
|
||||||
id: 'id2'
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const queryTodosData = [
|
|
||||||
{
|
|
||||||
name: "todo 1"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "todo 2"
|
|
||||||
},
|
|
||||||
{ name: "todo 3" }
|
|
||||||
];
|
|
||||||
|
|
||||||
// The store that holds the data sets
|
// The store that holds the data sets
|
||||||
export const dataStore = writable({
|
export const dataStore = writable({
|
||||||
|
@ -1,9 +1,38 @@
|
|||||||
// queryMessages.ts
|
// $lib/data/queryMessages.ts
|
||||||
import { dataStore } from '$lib/core/dataLoader';
|
|
||||||
|
|
||||||
const queryMessages = [
|
import { writable } from 'svelte/store';
|
||||||
{ text: 'text', date: 'date', id: 'id' },
|
|
||||||
{ text: 'text', date: 'date', id: 'id' }
|
|
||||||
];
|
|
||||||
|
|
||||||
dataStore.registerData('messages', queryMessages);
|
export interface Message {
|
||||||
|
text: string;
|
||||||
|
timestamp: string;
|
||||||
|
sender: string;
|
||||||
|
type: string;
|
||||||
|
composite?: object | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isClientSide = typeof window !== "undefined";
|
||||||
|
|
||||||
|
function getFromLocalStorage(key, defaultValue) {
|
||||||
|
return isClientSide && localStorage.getItem(key)
|
||||||
|
? JSON.parse(localStorage.getItem(key))
|
||||||
|
: defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setToLocalStorage(key, value) {
|
||||||
|
if (isClientSide) {
|
||||||
|
localStorage.setItem(key, JSON.stringify(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialMessages: Message[] = getFromLocalStorage('chat-messages', [
|
||||||
|
{ text: "Hello there!", timestamp: new Date().toLocaleString(), sender: "John", type: "text" },
|
||||||
|
{ text: "How are you?", timestamp: new Date().toLocaleString(), sender: "Alice", type: "text" }
|
||||||
|
]);
|
||||||
|
|
||||||
|
const messages = writable(initialMessages);
|
||||||
|
|
||||||
|
messages.subscribe(currentMessages => {
|
||||||
|
setToLocalStorage('chat-messages', currentMessages);
|
||||||
|
});
|
||||||
|
|
||||||
|
export const queryMessagesData = messages;
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
// queryTodos.ts
|
// $lib/data/queryTodos.ts
|
||||||
import { dataStore } from '$lib/core/dataLoader';
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
const queryTodos = [
|
export const queryTodosData = writable([
|
||||||
{ name: "todo 1" },
|
{
|
||||||
{ name: "todo 2" }
|
id: "id1",
|
||||||
];
|
text: "todo 1"
|
||||||
|
},
|
||||||
dataStore.registerData('todos', queryTodos);
|
{
|
||||||
|
id: "id2",
|
||||||
|
text: "todo 2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "id3",
|
||||||
|
text: "todo 3"
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
import { writable } from 'svelte/store';
|
|
||||||
|
|
||||||
// Helper function to determine if we're running on the client side (browser) or server side.
|
|
||||||
function isClientSide() {
|
|
||||||
return typeof window !== "undefined";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Safely get item from localStorage
|
|
||||||
function getFromLocalStorage(key, defaultValue) {
|
|
||||||
if (isClientSide()) {
|
|
||||||
return localStorage.getItem(key)
|
|
||||||
? JSON.parse(localStorage.getItem(key))
|
|
||||||
: defaultValue;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Safely set item to localStorage
|
|
||||||
function setToLocalStorage(key, value) {
|
|
||||||
if (isClientSide()) {
|
|
||||||
localStorage.setItem(key, JSON.stringify(value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define the updated Message interface
|
|
||||||
export interface Message {
|
|
||||||
text: string;
|
|
||||||
timestamp: string;
|
|
||||||
sender: string;
|
|
||||||
type: string;
|
|
||||||
composite?: object | null; // New field
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Load messages from localStorage or set an empty array if not available
|
|
||||||
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
|
|
||||||
messages.subscribe(currentMessages => {
|
|
||||||
setToLocalStorage('chat-messages', currentMessages);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
export function createMessage(messageData) {
|
|
||||||
const currentDate = new Date().toLocaleString();
|
|
||||||
const newMessageObj = {
|
|
||||||
text: messageData.text,
|
|
||||||
timestamp: currentDate,
|
|
||||||
sender: messageData.sender,
|
|
||||||
type: messageData.type,
|
|
||||||
composite: messageData.composite || null // New field
|
|
||||||
};
|
|
||||||
messages.update(oldMessages => [...oldMessages, newMessageObj]);
|
|
||||||
}
|
|
||||||
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
|
|
||||||
];
|
|
92
src/lib/services/messages/messages.ts
Normal file
92
src/lib/services/messages/messages.ts
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// $lib/services/messages.ts
|
||||||
|
|
||||||
|
import { queryMessagesData } from '$lib/data/queryMessages';
|
||||||
|
|
||||||
|
// The createMessage function now accepts a messageData parameter.
|
||||||
|
export function createMessage(messageData) {
|
||||||
|
const currentDate = new Date().toLocaleString();
|
||||||
|
const newMessageObj = {
|
||||||
|
text: messageData.text,
|
||||||
|
timestamp: currentDate,
|
||||||
|
sender: messageData.sender,
|
||||||
|
type: messageData.type,
|
||||||
|
composite: messageData.composite || null
|
||||||
|
};
|
||||||
|
|
||||||
|
// We use the $ prefix to get the value out of a Svelte store
|
||||||
|
// and then set the new value back into the store.
|
||||||
|
queryMessagesData.update(oldMessages => [...oldMessages, newMessageObj]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearMessages() {
|
||||||
|
queryMessagesData.set([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
// // Helper function to determine if we're running on the client side (browser) or server side.
|
||||||
|
// function isClientSide() {
|
||||||
|
// return typeof window !== "undefined";
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Safely get item from localStorage
|
||||||
|
// function getFromLocalStorage(key, defaultValue) {
|
||||||
|
// if (isClientSide()) {
|
||||||
|
// return localStorage.getItem(key)
|
||||||
|
// ? JSON.parse(localStorage.getItem(key))
|
||||||
|
// : defaultValue;
|
||||||
|
// }
|
||||||
|
// return defaultValue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Safely set item to localStorage
|
||||||
|
// function setToLocalStorage(key, value) {
|
||||||
|
// if (isClientSide()) {
|
||||||
|
// localStorage.setItem(key, JSON.stringify(value));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Define the updated Message interface
|
||||||
|
// export interface Message {
|
||||||
|
// text: string;
|
||||||
|
// timestamp: string;
|
||||||
|
// sender: string;
|
||||||
|
// type: string;
|
||||||
|
// composite?: object | null; // New field
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// // Load messages from localStorage or set an empty array if not available
|
||||||
|
// 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
|
||||||
|
// messages.subscribe(currentMessages => {
|
||||||
|
// setToLocalStorage('chat-messages', currentMessages);
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
|
// export function createMessage(messageData) {
|
||||||
|
// const currentDate = new Date().toLocaleString();
|
||||||
|
// const newMessageObj = {
|
||||||
|
// text: messageData.text,
|
||||||
|
// timestamp: currentDate,
|
||||||
|
// sender: messageData.sender,
|
||||||
|
// type: messageData.type,
|
||||||
|
// composite: messageData.composite || null // New field
|
||||||
|
// };
|
||||||
|
// messages.update(oldMessages => [...oldMessages, newMessageObj]);
|
||||||
|
// }
|
||||||
|
// 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
|
||||||
|
// ];
|
@ -1,6 +1,6 @@
|
|||||||
import type { IRelayPKP } from '@lit-protocol/types';
|
import type { IRelayPKP } from '@lit-protocol/types';
|
||||||
import type { IProvider } from '$lib/services/provider/IProvider';
|
import type { IProvider } from '$lib/services/provider/IProvider';
|
||||||
import { createMessage } from '$lib/services/messages';
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
|
|
||||||
export async function mintPkp(provider: IProvider, authMethod: any): Promise<IRelayPKP> {
|
export async function mintPkp(provider: IProvider, authMethod: any): Promise<IRelayPKP> {
|
||||||
createMessage({
|
createMessage({
|
||||||
|
@ -4,7 +4,7 @@ import { publicProvider } from '@wagmi/core/providers/public';
|
|||||||
import { InjectedConnector } from '@wagmi/core/connectors/injected';
|
import { InjectedConnector } from '@wagmi/core/connectors/injected';
|
||||||
import { WalletConnectConnector } from '@wagmi/core/connectors/walletConnect';
|
import { WalletConnectConnector } from '@wagmi/core/connectors/walletConnect';
|
||||||
import { jsonRpcProvider } from '@wagmi/core/providers/jsonRpc';
|
import { jsonRpcProvider } from '@wagmi/core/providers/jsonRpc';
|
||||||
import { createMessage } from '$lib/services/messages';
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
|
|
||||||
export interface ProviderData {
|
export interface ProviderData {
|
||||||
walletConnectId: string;
|
walletConnectId: string;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { getStoredAuthSig } from '$lib/services/authWithMetamask/authWithMetamask';
|
import { getStoredAuthSig } from '$lib/services/authWithMetamask/authWithMetamask';
|
||||||
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers';
|
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers';
|
||||||
import { createMessage } from '$lib/services/messages';
|
import { createMessage } from '$lib/services/messages/messages';
|
||||||
|
|
||||||
// Use a variable to keep track of whether the wallet has been connected or not
|
// Use a variable to keep track of whether the wallet has been connected or not
|
||||||
let isWalletConnected = false;
|
let isWalletConnected = false;
|
||||||
|
@ -40,9 +40,10 @@
|
|||||||
services: ['helloEarthAlert']
|
services: ['helloEarthAlert']
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'messages',
|
id: 'me',
|
||||||
component: 'Messages',
|
component: 'Messages',
|
||||||
slot: 'main'
|
slot: 'main',
|
||||||
|
map: { messages: 'data.queryMessages' }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'terminal',
|
id: 'terminal',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user