Skip to content

Commit

Permalink
move data sync to general pannel
Browse files Browse the repository at this point in the history
  • Loading branch information
qcgm1978 committed Aug 13, 2023
1 parent f165b34 commit 46e92df
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 87 deletions.
102 changes: 16 additions & 86 deletions src/components/ChatSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,7 @@
style="margin: 10px"
></v-btn>
</v-list-item>
<v-list-item>
<v-list-item-title>{{ $t("proxy.fullSet") }}</v-list-item-title>
<v-btn
color="primary"
variant="outlined"
:text="$t('chat.backupToLocal')"
@click="downloadDataJson"
style="margin: 10px; float: left"
></v-btn>
<!-- <pre v-if="jsonData">{{ jsonData }}</pre> -->
<v-file-input
color="primary"
variant="outlined"
:label="$t('chat.restoreFromLocal')"
@change="readJson"
style="width: 400px"
></v-file-input>
</v-list-item>

<ConfirmModal ref="confirmModal" />
<v-snackbar
v-model="snackbar.show"
Expand All @@ -47,38 +30,21 @@
import { ref, reactive } from "vue";
import { useStore } from "vuex";
import i18n from "@/i18n";
const electron = window.require("electron");
const ipcRenderer = electron.ipcRenderer;
import ConfirmModal from "@/components/ConfirmModal.vue";
import bots from "@/bots";
import { download_by_link } from "@/utils";
const emit = defineEmits(["close-dialog"]);
const confirmModal = ref();
const store = useStore();
const jsonData = ref(null);
const snackbar = reactive({
show: false,
text: "",
timeout: 1500,
color: "success",
});
const readJson = async (event) => {
const reader = new FileReader();
reader.onload = (evt) => {
const value = JSON.parse(evt.target.result);
jsonData.value = value;
reload(value);
};
reader.readAsText(event.target.files[0]);
};
async function reload(value) {
const load = i18n.global.t("proxy.saveAndApply");
const result = await confirmModal.value.showModal("", `${load}?`);
if (result) {
Object.keys(value).map((d) => (localStorage[d] = value[d]));
await ipcRenderer.invoke("restart-app");
}
}
// This function downloads the chat history as a JSON file.
Expand Down Expand Up @@ -122,62 +88,26 @@ function get_messages() {
} else {
const botClassname = message.className;
const bot = bots.getBotByClassName(botClassname);
const botName = bot.getFullname();
arr.at(-1).responses.push({
content,
botName,
botClassname,
botModel: message.model,
highlight: message.highlight,
});
if (bot) {
const botName = bot.getFullname();
arr.at(-1).responses.push({
content,
botName,
botClassname,
botModel: message.model,
highlight: message.highlight,
});
}
}
return arr;
}, []),
}));
return messages;
} catch (e) {
// debugger;
// eslint-disable-next-line no-debugger
debugger;
}
}
const downloadDataJson = () => {
const content = "data";
const messages = localStorage;
download_by_link(messages, content);
};
// Create a blob that contains the JSON data.
// The space parameter specifies the indentation of nested objects in the string representation.
function download_by_link(messages, name) {
const blob = new Blob([JSON.stringify(messages, null, 2)], {
// The type of the blob.
type: "application/json",
});
const url = URL.createObjectURL(blob);
// Create a file name for the JSON file.
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0"); // months are 0-based in JavaScript
const day = String(date.getDate()).padStart(2, "0");
const hour = String(date.getHours()).padStart(2, "0");
const minute = String(date.getMinutes()).padStart(2, "0");
const second = String(date.getSeconds()).padStart(2, "0");
const fileName = `chatall-${name}-${year}${month}${day}-${hour}${minute}${second}`;
const a = document.createElement("a");
a.href = url;
a.download = `${fileName}.json`;
document.body.appendChild(a);
// Click the anchor element to download the file.
a.click();
// Remove the anchor element from the document body.
document.body.removeChild(a);
// Revoke the URL for the blob.
URL.revokeObjectURL(url);
}
async function deleteChats() {
const confirm = await confirmModal.value.showModal(
Expand Down
49 changes: 48 additions & 1 deletion src/components/SettingsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@
@update:model-value="setCurrentMode($event)"
></v-select>
</v-list-item>
<v-list-item>
<v-list-item-title>{{
$t("proxy.fullSet")
}}</v-list-item-title>
<v-btn
color="primary"
variant="outlined"
:text="$t('chat.backupToLocal')"
@click="downloadDataJson"
style="margin: 10px; float: left"
></v-btn>
<!-- <pre v-if="jsonData">{{ jsonData }}</pre> -->
<v-file-input
color="primary"
variant="outlined"
:label="$t('chat.restoreFromLocal')"
@change="readJson"
style="width: 400px"
></v-file-input>
</v-list-item>
</div>

<div v-if="tab == 'proxy'">
Expand All @@ -81,16 +101,19 @@
</div>
</v-card>
</v-dialog>
<ConfirmModal ref="confirmModal" />
</template>

<script setup>
import { computed, ref } from "vue";
import { useStore } from "vuex";
import { useI18n } from "vue-i18n";
import i18n from "@/i18n";
import { useTheme } from "vuetify";
import ProxySettings from "@/components/ProxySetting.vue";
import ChatSettings from "@/components/ChatSetting.vue";
import ConfirmModal from "@/components/ConfirmModal.vue";
import ChatGPTBotSettings from "@/components/BotSettings/ChatGPTBotSettings.vue";
import OpenAIAPIBotSettings from "@/components/BotSettings/OpenAIAPIBotSettings.vue";
Expand All @@ -114,7 +137,7 @@ import CharacterAIBotSettings from "./BotSettings/CharacterAIBotSettings.vue";
import ClaudeAIBotSettings from "./BotSettings/ClaudeAIBotSettings.vue";
import { resolveTheme, applyTheme, Mode } from "../theme";
import { download_by_link } from "@/utils";
const { ipcRenderer } = window.require("electron");
const { t: $t, locale } = useI18n();
const store = useStore();
Expand All @@ -124,6 +147,8 @@ const props = defineProps(["open"]);
const emit = defineEmits(["update:open", "done"]);
const tab = ref(null);
const jsonData = ref(null);
const confirmModal = ref();
const botSettings = [
{ brand: "360AiBrain", component: Qihoo360AIBrainBotSettings },
Expand Down Expand Up @@ -187,6 +212,28 @@ const closeDialog = () => {
emit("update:open", false);
emit("done");
};
const readJson = async (event) => {
const reader = new FileReader();
reader.onload = (evt) => {
const value = JSON.parse(evt.target.result);
jsonData.value = value;
reload(value);
};
reader.readAsText(event.target.files[0]);
};
async function reload(value) {
const load = i18n.global.t("proxy.saveAndApply");
const result = await confirmModal.value.showModal("", `${load}?`);
if (result) {
Object.keys(value).map((d) => (localStorage[d] = value[d]));
await ipcRenderer.invoke("restart-app");
}
}
const downloadDataJson = () => {
const content = "data";
const messages = localStorage;
download_by_link(messages, content);
};
</script>

<style scoped>
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { download_by_link } from "./setting";
34 changes: 34 additions & 0 deletions src/utils/setting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Create a blob that contains the JSON data.
// The space parameter specifies the indentation of nested objects in the string representation.
export function download_by_link(messages, name) {
const blob = new Blob([JSON.stringify(messages, null, 2)], {
// The type of the blob.
type: "application/json",
});

const url = URL.createObjectURL(blob);

// Create a file name for the JSON file.
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0"); // months are 0-based in JavaScript
const day = String(date.getDate()).padStart(2, "0");
const hour = String(date.getHours()).padStart(2, "0");
const minute = String(date.getMinutes()).padStart(2, "0");
const second = String(date.getSeconds()).padStart(2, "0");
const fileName = `chatall-${name}-${year}${month}${day}-${hour}${minute}${second}`;

const a = document.createElement("a");
a.href = url;
a.download = `${fileName}.json`;
document.body.appendChild(a);

// Click the anchor element to download the file.
a.click();

// Remove the anchor element from the document body.
document.body.removeChild(a);

// Revoke the URL for the blob.
URL.revokeObjectURL(url);
}

0 comments on commit 46e92df

Please sign in to comment.