improve migration
This commit is contained in:
parent
3acee866ff
commit
d83a8e3731
3 changed files with 6 additions and 11 deletions
Binary file not shown.
|
@ -1,4 +1,4 @@
|
||||||
import { HashRouter, Outlet, Route, Routes, Navigate } from 'react-router';
|
import { HashRouter, Outlet, Route, Routes } from 'react-router';
|
||||||
import Header from './components/Header';
|
import Header from './components/Header';
|
||||||
import Sidebar from './components/Sidebar';
|
import Sidebar from './components/Sidebar';
|
||||||
import { AppContextProvider, useAppContext } from './utils/app.context';
|
import { AppContextProvider, useAppContext } from './utils/app.context';
|
||||||
|
|
|
@ -36,6 +36,7 @@ const StorageUtils = {
|
||||||
* manage conversations
|
* manage conversations
|
||||||
*/
|
*/
|
||||||
async getAllConversations(): Promise<Conversation[]> {
|
async getAllConversations(): Promise<Conversation[]> {
|
||||||
|
await migrationLStoIDB().catch(console.error); // noop if already migrated
|
||||||
return (await db.conversations.toArray()).sort(
|
return (await db.conversations.toArray()).sort(
|
||||||
(a, b) => b.lastModified - a.lastModified
|
(a, b) => b.lastModified - a.lastModified
|
||||||
);
|
);
|
||||||
|
@ -211,7 +212,6 @@ interface LSConversation {
|
||||||
id: string; // format: `conv-{timestamp}`
|
id: string; // format: `conv-{timestamp}`
|
||||||
lastModified: number; // timestamp from Date.now()
|
lastModified: number; // timestamp from Date.now()
|
||||||
messages: LSMessage[];
|
messages: LSMessage[];
|
||||||
migrated?: boolean; // additionally, we add a flag to prevent duplicate migration
|
|
||||||
}
|
}
|
||||||
interface LSMessage {
|
interface LSMessage {
|
||||||
id: number;
|
id: number;
|
||||||
|
@ -220,6 +220,7 @@ interface LSMessage {
|
||||||
timings?: TimingReport;
|
timings?: TimingReport;
|
||||||
}
|
}
|
||||||
async function migrationLStoIDB() {
|
async function migrationLStoIDB() {
|
||||||
|
if (localStorage.getItem('migratedToIDB')) return;
|
||||||
const res: LSConversation[] = [];
|
const res: LSConversation[] = [];
|
||||||
for (const key in localStorage) {
|
for (const key in localStorage) {
|
||||||
if (key.startsWith('conv-')) {
|
if (key.startsWith('conv-')) {
|
||||||
|
@ -233,10 +234,6 @@ async function migrationLStoIDB() {
|
||||||
const { id: convId, lastModified, messages } = conv;
|
const { id: convId, lastModified, messages } = conv;
|
||||||
const firstMsg = messages[0];
|
const firstMsg = messages[0];
|
||||||
const lastMsg = messages.at(-1);
|
const lastMsg = messages.at(-1);
|
||||||
if (conv.migrated) {
|
|
||||||
console.log(`Skipping conversation ${convId} already migrated`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (messages.length < 2 || !firstMsg || !lastMsg) {
|
if (messages.length < 2 || !firstMsg || !lastMsg) {
|
||||||
console.log(
|
console.log(
|
||||||
`Skipping conversation ${convId} with ${messages.length} messages`
|
`Skipping conversation ${convId} with ${messages.length} messages`
|
||||||
|
@ -250,12 +247,12 @@ async function migrationLStoIDB() {
|
||||||
currNode: lastMsg.id,
|
currNode: lastMsg.id,
|
||||||
name,
|
name,
|
||||||
});
|
});
|
||||||
const rootId = Date.now();
|
const rootId = messages[0].id - 2;
|
||||||
await db.messages.add({
|
await db.messages.add({
|
||||||
id: rootId,
|
id: rootId,
|
||||||
convId: convId,
|
convId: convId,
|
||||||
type: 'root',
|
type: 'root',
|
||||||
timestamp: lastModified,
|
timestamp: rootId,
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: '',
|
content: '',
|
||||||
parent: -1,
|
parent: -1,
|
||||||
|
@ -272,7 +269,6 @@ async function migrationLStoIDB() {
|
||||||
children: i === messages.length - 1 ? [] : [messages[i + 1].id],
|
children: i === messages.length - 1 ? [] : [messages[i + 1].id],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
localStorage.setItem(convId, JSON.stringify({ ...conv, migrated: true }));
|
|
||||||
migratedCount++;
|
migratedCount++;
|
||||||
console.log(
|
console.log(
|
||||||
`Migrated conversation ${convId} with ${messages.length} messages`
|
`Migrated conversation ${convId} with ${messages.length} messages`
|
||||||
|
@ -281,7 +277,6 @@ async function migrationLStoIDB() {
|
||||||
console.log(
|
console.log(
|
||||||
`Migrated ${migratedCount} conversations from localStorage to IndexedDB`
|
`Migrated ${migratedCount} conversations from localStorage to IndexedDB`
|
||||||
);
|
);
|
||||||
|
localStorage.setItem('migratedToIDB', '1');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
migrationLStoIDB();
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue