forked from drzo/PandoraAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppData.vue
73 lines (67 loc) · 2.31 KB
/
AppData.vue
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
<script setup>
import { storeToRefs } from 'pinia';
const conversationsStore = useConversationsStore();
const {
processingController,
} = storeToRefs(conversationsStore);
const importAppData = () => {
if (processingController.value) {
return;
}
// get JSON file from user and turn it into localStorage
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/json';
input.onchange = (e) => {
if (processingController.value) {
return;
}
const file = e.target.files[0];
const reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = (readerEvent) => {
if (processingController.value) {
return;
}
const content = readerEvent.target.result;
localStorage.clear();
Object.assign(localStorage, JSON.parse(content));
window.location.reload();
};
};
input.click();
};
const exportAppData = () => {
if (processingController.value) {
return;
}
// turn localStorage into JSON and save it as a file
const dataStr = JSON.stringify(localStorage);
const dataUri = `data:application/json;charset=utf-8,${encodeURIComponent(dataStr)}`;
const exportFileDefaultName = `PandoraAI-${Date.now()}.data.json`;
const linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', exportFileDefaultName);
linkElement.click();
};
</script>
<template>
<button
@click="importAppData"
class="flex items-center gap-1 text-white/60 transition duration-300 hover:text-white/80"
:disabled="!!processingController"
:class="{ 'cursor-not-allowed': !!processingController }"
>
<Icon class="w-4 h-4" name="bx:bx-import" />
Import
</button>
<button
@click="exportAppData"
class="flex items-center gap-1 text-white/60 transition duration-300 hover:text-white/80"
:disabled="!!processingController"
:class="{ 'cursor-not-allowed': !!processingController }"
>
<Icon class="w-4 h-4" name="bx:bx-export" />
Export
</button>
</template>