File size: 2,192 Bytes
effb90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import localforage from 'localforage';
import { writable } from 'svelte/store';
import type { ChatData } from './types';
import { nanoid } from 'nanoid';

const intitalChatId = nanoid();
const initialData: ChatData = [{
    id: intitalChatId,
    messages: [],
    blurb: `New Chat - ${intitalChatId}`,
    timestamp: new Date().getTime()
}];


const loadingState = writable<string>('');
const chatsStore = writable<ChatData>(initialData);
const selectedChatId = writable<string>(intitalChatId);

localforage.config({
    name: 'Pix2PixChat',
    storeName: 'chatsStore'
});

localforage.getItem<ChatData>('chatsStore').then((value) => {
    if (value) {
        chatsStore.set(value);
    } else {
        chatsStore.set(initialData);
    }
});

chatsStore.subscribe((value) => localforage.setItem<ChatData>('chatsStore', value));

localforage.getItem<string>('selectedChatId').then((value) => {
    if (value) {
        selectedChatId.set(value);
    } else {
        selectedChatId.set(intitalChatId);
    }
});

selectedChatId.subscribe((value) => localforage.setItem<string>('selectedChatId', value));

export { loadingState, chatsStore, selectedChatId };

// import { writable } from 'svelte/store';
// import { browser } from '$app/environment';
// import type { ChatData } from './types';
// import { nanoid } from 'nanoid';


// const intitalChatId = nanoid();
// const initialData: ChatData = [{
//     id: intitalChatId,
//     messages: [],
//     blurb: `New Chat - ${new Date().getTime()}`
// }
// ]

// export const loadingState = writable<string>('');
// export const chatsStore = writable<ChatData>(
//     browser ? JSON.parse(localStorage['chatsStore'] || JSON.stringify(initialData)) : initialData
// );
// chatsStore.subscribe((value) => {
//     if (browser) {
//         return (localStorage['chatsStore'] = JSON.stringify(value));
//     }
// });

// export const selectedChatId = writable<string>(
//     browser ? JSON.parse(localStorage['selectedChatId'] || JSON.stringify(intitalChatId)) : intitalChatId
// );
// selectedChatId.subscribe((value) => {
//     if (browser) {
//         return (localStorage['selectedChatId'] = JSON.stringify(value));
//     }
// });