|
15 | 15 | style="margin: 10px" |
16 | 16 | ></v-btn> |
17 | 17 | </v-list-item> |
18 | | - <v-list-item> |
19 | | - <v-list-item-title>{{ $t("proxy.fullSet") }}</v-list-item-title> |
20 | | - <v-btn |
21 | | - color="primary" |
22 | | - variant="outlined" |
23 | | - :text="$t('chat.backupToLocal')" |
24 | | - @click="downloadDataJson" |
25 | | - style="margin: 10px; float: left" |
26 | | - ></v-btn> |
27 | | - <!-- <pre v-if="jsonData">{{ jsonData }}</pre> --> |
28 | | - <v-file-input |
29 | | - color="primary" |
30 | | - variant="outlined" |
31 | | - :label="$t('chat.restoreFromLocal')" |
32 | | - @change="readJson" |
33 | | - style="width: 400px" |
34 | | - ></v-file-input> |
35 | | - </v-list-item> |
| 18 | + |
36 | 19 | <ConfirmModal ref="confirmModal" /> |
37 | 20 | <v-snackbar |
38 | 21 | v-model="snackbar.show" |
|
47 | 30 | import { ref, reactive } from "vue"; |
48 | 31 | import { useStore } from "vuex"; |
49 | 32 | import i18n from "@/i18n"; |
50 | | -const electron = window.require("electron"); |
51 | | -const ipcRenderer = electron.ipcRenderer; |
| 33 | +
|
52 | 34 | import ConfirmModal from "@/components/ConfirmModal.vue"; |
53 | 35 | import bots from "@/bots"; |
| 36 | +import { download_by_link } from "@/utils"; |
54 | 37 |
|
55 | 38 | const emit = defineEmits(["close-dialog"]); |
56 | 39 | const confirmModal = ref(); |
57 | 40 | const store = useStore(); |
58 | | -const jsonData = ref(null); |
| 41 | +
|
59 | 42 | const snackbar = reactive({ |
60 | 43 | show: false, |
61 | 44 | text: "", |
62 | 45 | timeout: 1500, |
63 | 46 | color: "success", |
64 | 47 | }); |
65 | | -const readJson = async (event) => { |
66 | | - const reader = new FileReader(); |
67 | | - reader.onload = (evt) => { |
68 | | - const value = JSON.parse(evt.target.result); |
69 | | - jsonData.value = value; |
70 | | - reload(value); |
71 | | - }; |
72 | | - reader.readAsText(event.target.files[0]); |
73 | | -}; |
74 | | -async function reload(value) { |
75 | | - const load = i18n.global.t("proxy.saveAndApply"); |
76 | | - const result = await confirmModal.value.showModal("", `${load}?`); |
77 | | - if (result) { |
78 | | - Object.keys(value).map((d) => (localStorage[d] = value[d])); |
79 | | - await ipcRenderer.invoke("restart-app"); |
80 | | - } |
81 | | -} |
82 | 48 |
|
83 | 49 | // This function downloads the chat history as a JSON file. |
84 | 50 |
|
@@ -122,62 +88,26 @@ function get_messages() { |
122 | 88 | } else { |
123 | 89 | const botClassname = message.className; |
124 | 90 | const bot = bots.getBotByClassName(botClassname); |
125 | | - const botName = bot.getFullname(); |
126 | | - arr.at(-1).responses.push({ |
127 | | - content, |
128 | | - botName, |
129 | | - botClassname, |
130 | | - botModel: message.model, |
131 | | - highlight: message.highlight, |
132 | | - }); |
| 91 | + if (bot) { |
| 92 | + const botName = bot.getFullname(); |
| 93 | + arr.at(-1).responses.push({ |
| 94 | + content, |
| 95 | + botName, |
| 96 | + botClassname, |
| 97 | + botModel: message.model, |
| 98 | + highlight: message.highlight, |
| 99 | + }); |
| 100 | + } |
133 | 101 | } |
134 | 102 | return arr; |
135 | 103 | }, []), |
136 | 104 | })); |
137 | 105 | return messages; |
138 | 106 | } catch (e) { |
139 | | - // debugger; |
| 107 | + // eslint-disable-next-line no-debugger |
| 108 | + debugger; |
140 | 109 | } |
141 | 110 | } |
142 | | -const downloadDataJson = () => { |
143 | | - const content = "data"; |
144 | | - const messages = localStorage; |
145 | | - download_by_link(messages, content); |
146 | | -}; |
147 | | -// Create a blob that contains the JSON data. |
148 | | -// The space parameter specifies the indentation of nested objects in the string representation. |
149 | | -function download_by_link(messages, name) { |
150 | | - const blob = new Blob([JSON.stringify(messages, null, 2)], { |
151 | | - // The type of the blob. |
152 | | - type: "application/json", |
153 | | - }); |
154 | | -
|
155 | | - const url = URL.createObjectURL(blob); |
156 | | -
|
157 | | - // Create a file name for the JSON file. |
158 | | - const date = new Date(); |
159 | | - const year = date.getFullYear(); |
160 | | - const month = String(date.getMonth() + 1).padStart(2, "0"); // months are 0-based in JavaScript |
161 | | - const day = String(date.getDate()).padStart(2, "0"); |
162 | | - const hour = String(date.getHours()).padStart(2, "0"); |
163 | | - const minute = String(date.getMinutes()).padStart(2, "0"); |
164 | | - const second = String(date.getSeconds()).padStart(2, "0"); |
165 | | - const fileName = `chatall-${name}-${year}${month}${day}-${hour}${minute}${second}`; |
166 | | -
|
167 | | - const a = document.createElement("a"); |
168 | | - a.href = url; |
169 | | - a.download = `${fileName}.json`; |
170 | | - document.body.appendChild(a); |
171 | | -
|
172 | | - // Click the anchor element to download the file. |
173 | | - a.click(); |
174 | | -
|
175 | | - // Remove the anchor element from the document body. |
176 | | - document.body.removeChild(a); |
177 | | -
|
178 | | - // Revoke the URL for the blob. |
179 | | - URL.revokeObjectURL(url); |
180 | | -} |
181 | 111 |
|
182 | 112 | async function deleteChats() { |
183 | 113 | const confirm = await confirmModal.value.showModal( |
|
0 commit comments