This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] Add temporary migration for old users
- Loading branch information
1 parent
10e5bfc
commit ad6440c
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { notifications } from "@mantine/notifications"; | ||
import { CallableFunction, Conversation } from "gpt-turbo"; | ||
import { addConversation } from "../actions/conversations/addConversation"; | ||
import { setConversationName } from "../actions/conversations/setConversationName"; | ||
import { setConversationLastEdit } from "../actions/conversations/setConversationLastEdit"; | ||
import { addCallableFunction } from "../actions/callableFunctions/addCallableFunction"; | ||
import { saveContext } from "../actions/savedContexts/saveContext"; | ||
import { savePrompt } from "../actions/savedPrompts/savePrompt"; | ||
import { setDefaultSettings } from "../actions/defaultConversationSettings/setDefaultSettings"; | ||
|
||
const notify = ( | ||
title: string, | ||
message: string, | ||
color = "blue", | ||
autoClose = true | ||
) => { | ||
setTimeout(() => { | ||
notifications.show({ | ||
color, | ||
autoClose, | ||
title, | ||
message, | ||
}); | ||
}, 100); | ||
}; | ||
|
||
// TODO: Remove this after a while. This is to migrate users from the old persistence system to the new one. | ||
// Not EVERYTHING is migrated, but the most important things are. | ||
export const migrateOldData = async () => { | ||
const oldPersistence = localStorage.getItem("gpt-turbo-persistence"); | ||
const oldSettings = localStorage.getItem("gpt-turbo-settings"); | ||
|
||
if (oldPersistence) { | ||
let deleteAfter = true; | ||
try { | ||
const { conversations, functions, contexts, prompts } = | ||
JSON.parse(oldPersistence); | ||
for (const { name, lastEdited, ...conversation } of conversations) { | ||
try { | ||
const c = await Conversation.fromJSON(conversation); | ||
addConversation(c); | ||
setConversationName(c.id, name); | ||
setConversationLastEdit(c.id, lastEdited); | ||
} catch (e) { | ||
deleteAfter = false; | ||
console.error(e); | ||
notify( | ||
"Conversation Migration Failed", | ||
"Failed to migrate one of your conversations to the new storage system.", | ||
"red", | ||
false | ||
); | ||
} | ||
} | ||
|
||
for (const { displayName, code, ...fn } of functions) { | ||
try { | ||
const f = CallableFunction.fromJSON(fn); | ||
addCallableFunction(f, displayName, code); | ||
} catch (e) { | ||
deleteAfter = false; | ||
console.error(e); | ||
notify( | ||
"Callable Function Migration Failed", | ||
"Failed to migrate one of your callable functions to the new storage system.", | ||
"red", | ||
false | ||
); | ||
} | ||
} | ||
|
||
for (const { name, value } of contexts) { | ||
saveContext(name, value); | ||
} | ||
|
||
for (const { name, value } of prompts) { | ||
savePrompt(name, value); | ||
} | ||
|
||
if (deleteAfter) { | ||
localStorage.removeItem("gpt-turbo-persistence"); | ||
notify( | ||
"Persistence Migration Complete", | ||
"Your old saved data has been migrated to the new storage system!", | ||
"green" | ||
); | ||
} else { | ||
notify( | ||
"Persistence Migration Partially Complete", | ||
"Only some of your old saved data has been migrated to the new storage system. Please check the console for more details.", | ||
"yellow" | ||
); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
notify( | ||
"Persistence Migration Failed", | ||
"Failed to migrate your old saved data to the new storage system.", | ||
"red", | ||
false | ||
); | ||
} | ||
} | ||
|
||
if (oldSettings) { | ||
try { | ||
setDefaultSettings(JSON.parse(oldSettings)); | ||
localStorage.removeItem("gpt-turbo-settings"); | ||
notify( | ||
"Settings Migration Complete", | ||
"Your old settings have been migrated to the new storage system!", | ||
"green" | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
notify( | ||
"Settings Migration Failed", | ||
"Failed to migrate your old settings to the new storage system.", | ||
"red", | ||
false | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters