thay doi ui chebichat

This commit is contained in:
quangdn-ght
2025-06-25 14:57:12 +07:00
parent e3fc9eef8f
commit cdeb27891b
29 changed files with 621 additions and 1129 deletions

View File

@@ -97,7 +97,7 @@ export interface ChatSession {
export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
export const BOT_HELLO: ChatMessage = createMessage({
role: "assistant",
role: "system",
content: Locale.Store.BotHello,
});

View File

@@ -1,5 +1,12 @@
// Import các hàm và hằng số cần thiết từ các module khác
import { getClientConfig } from "../config/client";
import { ApiPath, STORAGE_KEY, StoreKey } from "../constant";
import {
ApiPath,
STORAGE_KEY,
StoreKey,
UPSTASH_APIKEY,
UPSTASH_ENDPOINT,
} from "../constant";
import { createPersistStore } from "../utils/store";
import {
AppState,
@@ -13,48 +20,59 @@ import { showToast } from "../components/ui-lib";
import Locale from "../locales";
import { createSyncClient, ProviderType } from "../utils/cloud";
// Định nghĩa interface cho cấu hình WebDav
export interface WebDavConfig {
server: string;
username: string;
password: string;
}
// Kiểm tra xem ứng dụng đang chạy ở chế độ app hay không
const isApp = !!getClientConfig()?.isApp;
// Định nghĩa kiểu dữ liệu cho SyncStore dựa trên useSyncStore
export type SyncStore = GetStoreState<typeof useSyncStore>;
// Trạng thái mặc định cho việc đồng bộ
const DEFAULT_SYNC_STATE = {
provider: ProviderType.WebDAV,
useProxy: true,
proxyUrl: ApiPath.Cors as string,
provider: ProviderType.UpStash as ProviderType, // Nhà cung cấp mặc định là UpStash
useProxy: true, // Sử dụng proxy mặc định
proxyUrl: ApiPath.Cors as string, // Đường dẫn proxy mặc định
// Cấu hình WebDav mặc định
webdav: {
endpoint: "",
username: "",
password: "",
},
// Cấu hình Upstash, username sẽ lấy từ STORAGE_KEY
upstash: {
endpoint: "",
endpoint: UPSTASH_ENDPOINT,
username: STORAGE_KEY,
apiKey: "",
apiKey: UPSTASH_APIKEY,
},
lastSyncTime: 0,
lastProvider: "",
lastSyncTime: 0, // Thời gian đồng bộ lần cuối
lastProvider: "", // Nhà cung cấp đồng bộ lần cuối
};
// Tạo store đồng bộ với các hàm thao tác
export const useSyncStore = createPersistStore(
DEFAULT_SYNC_STATE,
(set, get) => ({
// Kiểm tra xem đã cấu hình đầy đủ để đồng bộ cloud chưa
cloudSync() {
const config = get()[get().provider];
return Object.values(config).every((c) => c.toString().length > 0);
},
// Đánh dấu thời gian đồng bộ gần nhất
markSyncTime() {
set({ lastSyncTime: Date.now(), lastProvider: get().provider });
},
// Xuất dữ liệu ứng dụng ra file JSON
export() {
const state = getLocalAppState();
const datePart = isApp
@@ -67,6 +85,7 @@ export const useSyncStore = createPersistStore(
downloadAs(JSON.stringify(state), fileName);
},
// Nhập dữ liệu ứng dụng từ file JSON
async import() {
const rawContent = await readFromFile();
@@ -82,12 +101,14 @@ export const useSyncStore = createPersistStore(
}
},
// Lấy client đồng bộ dựa vào provider hiện tại
getClient() {
const provider = get().provider;
const client = createSyncClient(provider, get());
return client;
},
// Hàm đồng bộ dữ liệu giữa local và cloud
async sync() {
const localState = getLocalAppState();
const provider = get().provider;
@@ -97,12 +118,14 @@ export const useSyncStore = createPersistStore(
try {
const remoteState = await client.get(config.username);
if (!remoteState || remoteState === "") {
// Nếu cloud chưa có dữ liệu thì đẩy dữ liệu local lên cloud
await client.set(config.username, JSON.stringify(localState));
console.log(
"[Sync] Remote state is empty, using local state instead.",
);
return;
} else {
// Nếu cloud đã có dữ liệu thì merge với local
const parsedRemoteState = JSON.parse(
await client.get(config.username),
) as AppState;
@@ -114,20 +137,23 @@ export const useSyncStore = createPersistStore(
throw e;
}
// Đẩy dữ liệu đã merge lên cloud
await client.set(config.username, JSON.stringify(localState));
this.markSyncTime();
},
// Kiểm tra trạng thái kết nối cloud
async check() {
const client = this.getClient();
return await client.check();
},
}),
{
name: StoreKey.Sync,
version: 1.2,
name: StoreKey.Sync, // Tên store trong localStorage
version: 1.2, // Phiên bản store
// Hàm migrate để chuyển đổi dữ liệu khi nâng cấp version
migrate(persistedState, version) {
const newState = persistedState as typeof DEFAULT_SYNC_STATE;