Skip to content

Commit 46e92df

Browse files
committed
move data sync to general pannel
1 parent f165b34 commit 46e92df

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

src/components/ChatSetting.vue

Lines changed: 16 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,7 @@
1515
style="margin: 10px"
1616
></v-btn>
1717
</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+
3619
<ConfirmModal ref="confirmModal" />
3720
<v-snackbar
3821
v-model="snackbar.show"
@@ -47,38 +30,21 @@
4730
import { ref, reactive } from "vue";
4831
import { useStore } from "vuex";
4932
import i18n from "@/i18n";
50-
const electron = window.require("electron");
51-
const ipcRenderer = electron.ipcRenderer;
33+
5234
import ConfirmModal from "@/components/ConfirmModal.vue";
5335
import bots from "@/bots";
36+
import { download_by_link } from "@/utils";
5437
5538
const emit = defineEmits(["close-dialog"]);
5639
const confirmModal = ref();
5740
const store = useStore();
58-
const jsonData = ref(null);
41+
5942
const snackbar = reactive({
6043
show: false,
6144
text: "",
6245
timeout: 1500,
6346
color: "success",
6447
});
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-
}
8248
8349
// This function downloads the chat history as a JSON file.
8450
@@ -122,62 +88,26 @@ function get_messages() {
12288
} else {
12389
const botClassname = message.className;
12490
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+
}
133101
}
134102
return arr;
135103
}, []),
136104
}));
137105
return messages;
138106
} catch (e) {
139-
// debugger;
107+
// eslint-disable-next-line no-debugger
108+
debugger;
140109
}
141110
}
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-
}
181111
182112
async function deleteChats() {
183113
const confirm = await confirmModal.value.showModal(

src/components/SettingsModal.vue

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@
5959
@update:model-value="setCurrentMode($event)"
6060
></v-select>
6161
</v-list-item>
62+
<v-list-item>
63+
<v-list-item-title>{{
64+
$t("proxy.fullSet")
65+
}}</v-list-item-title>
66+
<v-btn
67+
color="primary"
68+
variant="outlined"
69+
:text="$t('chat.backupToLocal')"
70+
@click="downloadDataJson"
71+
style="margin: 10px; float: left"
72+
></v-btn>
73+
<!-- <pre v-if="jsonData">{{ jsonData }}</pre> -->
74+
<v-file-input
75+
color="primary"
76+
variant="outlined"
77+
:label="$t('chat.restoreFromLocal')"
78+
@change="readJson"
79+
style="width: 400px"
80+
></v-file-input>
81+
</v-list-item>
6282
</div>
6383

6484
<div v-if="tab == 'proxy'">
@@ -81,16 +101,19 @@
81101
</div>
82102
</v-card>
83103
</v-dialog>
104+
<ConfirmModal ref="confirmModal" />
84105
</template>
85106

86107
<script setup>
87108
import { computed, ref } from "vue";
88109
import { useStore } from "vuex";
89110
import { useI18n } from "vue-i18n";
111+
import i18n from "@/i18n";
90112
import { useTheme } from "vuetify";
91113
92114
import ProxySettings from "@/components/ProxySetting.vue";
93115
import ChatSettings from "@/components/ChatSetting.vue";
116+
import ConfirmModal from "@/components/ConfirmModal.vue";
94117
95118
import ChatGPTBotSettings from "@/components/BotSettings/ChatGPTBotSettings.vue";
96119
import OpenAIAPIBotSettings from "@/components/BotSettings/OpenAIAPIBotSettings.vue";
@@ -114,7 +137,7 @@ import CharacterAIBotSettings from "./BotSettings/CharacterAIBotSettings.vue";
114137
import ClaudeAIBotSettings from "./BotSettings/ClaudeAIBotSettings.vue";
115138
116139
import { resolveTheme, applyTheme, Mode } from "../theme";
117-
140+
import { download_by_link } from "@/utils";
118141
const { ipcRenderer } = window.require("electron");
119142
const { t: $t, locale } = useI18n();
120143
const store = useStore();
@@ -124,6 +147,8 @@ const props = defineProps(["open"]);
124147
const emit = defineEmits(["update:open", "done"]);
125148
126149
const tab = ref(null);
150+
const jsonData = ref(null);
151+
const confirmModal = ref();
127152
128153
const botSettings = [
129154
{ brand: "360AiBrain", component: Qihoo360AIBrainBotSettings },
@@ -187,6 +212,28 @@ const closeDialog = () => {
187212
emit("update:open", false);
188213
emit("done");
189214
};
215+
const readJson = async (event) => {
216+
const reader = new FileReader();
217+
reader.onload = (evt) => {
218+
const value = JSON.parse(evt.target.result);
219+
jsonData.value = value;
220+
reload(value);
221+
};
222+
reader.readAsText(event.target.files[0]);
223+
};
224+
async function reload(value) {
225+
const load = i18n.global.t("proxy.saveAndApply");
226+
const result = await confirmModal.value.showModal("", `${load}?`);
227+
if (result) {
228+
Object.keys(value).map((d) => (localStorage[d] = value[d]));
229+
await ipcRenderer.invoke("restart-app");
230+
}
231+
}
232+
const downloadDataJson = () => {
233+
const content = "data";
234+
const messages = localStorage;
235+
download_by_link(messages, content);
236+
};
190237
</script>
191238

192239
<style scoped>

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { download_by_link } from "./setting";

src/utils/setting.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Create a blob that contains the JSON data.
2+
// The space parameter specifies the indentation of nested objects in the string representation.
3+
export function download_by_link(messages, name) {
4+
const blob = new Blob([JSON.stringify(messages, null, 2)], {
5+
// The type of the blob.
6+
type: "application/json",
7+
});
8+
9+
const url = URL.createObjectURL(blob);
10+
11+
// Create a file name for the JSON file.
12+
const date = new Date();
13+
const year = date.getFullYear();
14+
const month = String(date.getMonth() + 1).padStart(2, "0"); // months are 0-based in JavaScript
15+
const day = String(date.getDate()).padStart(2, "0");
16+
const hour = String(date.getHours()).padStart(2, "0");
17+
const minute = String(date.getMinutes()).padStart(2, "0");
18+
const second = String(date.getSeconds()).padStart(2, "0");
19+
const fileName = `chatall-${name}-${year}${month}${day}-${hour}${minute}${second}`;
20+
21+
const a = document.createElement("a");
22+
a.href = url;
23+
a.download = `${fileName}.json`;
24+
document.body.appendChild(a);
25+
26+
// Click the anchor element to download the file.
27+
a.click();
28+
29+
// Remove the anchor element from the document body.
30+
document.body.removeChild(a);
31+
32+
// Revoke the URL for the blob.
33+
URL.revokeObjectURL(url);
34+
}

0 commit comments

Comments
 (0)