优化代码格式,提升可读性
This commit is contained in:
@@ -84,7 +84,9 @@ const compareSessionsByAnchorTime = (
|
|||||||
return right.id.localeCompare(left.id);
|
return right.id.localeCompare(left.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const toLoadedChatState = (session: ChatSessionRecord | undefined): LoadedChatState => {
|
const toLoadedChatState = (
|
||||||
|
session: ChatSessionRecord | undefined,
|
||||||
|
): LoadedChatState => {
|
||||||
if (!session) return emptyLoadedChatState();
|
if (!session) return emptyLoadedChatState();
|
||||||
return {
|
return {
|
||||||
storageSessionId: session.id,
|
storageSessionId: session.id,
|
||||||
@@ -107,7 +109,9 @@ const getDb = () =>
|
|||||||
openDB<ChatDB>(CHAT_DB_NAME, CHAT_DB_VERSION, {
|
openDB<ChatDB>(CHAT_DB_NAME, CHAT_DB_VERSION, {
|
||||||
upgrade(db) {
|
upgrade(db) {
|
||||||
if (!db.objectStoreNames.contains(SESSION_STORE)) {
|
if (!db.objectStoreNames.contains(SESSION_STORE)) {
|
||||||
const sessionStore = db.createObjectStore(SESSION_STORE, { keyPath: "id" });
|
const sessionStore = db.createObjectStore(SESSION_STORE, {
|
||||||
|
keyPath: "id",
|
||||||
|
});
|
||||||
sessionStore.createIndex("by-updatedAt", "updatedAt");
|
sessionStore.createIndex("by-updatedAt", "updatedAt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,13 +277,17 @@ export const saveActiveChatState = async (
|
|||||||
const finalTitle = preferredTitle || existingSession?.title || "新对话";
|
const finalTitle = preferredTitle || existingSession?.title || "新对话";
|
||||||
const hasContentChanged =
|
const hasContentChanged =
|
||||||
!existingSession ||
|
!existingSession ||
|
||||||
serializeConversationState(existingSession) !== serializeConversationState(state);
|
(existingSession && serializeConversationState(existingSession)) !==
|
||||||
|
serializeConversationState(state);
|
||||||
const shouldAnchorCreatedAtToFirstMessage =
|
const shouldAnchorCreatedAtToFirstMessage =
|
||||||
Boolean(existingSession) && !hasChatContent(existingSession) && hasContent;
|
existingSession && !hasChatContent(existingSession) && hasContent;
|
||||||
const nextRecord: ChatSessionRecord = {
|
const nextRecord: ChatSessionRecord = {
|
||||||
id: storageSessionId,
|
id: storageSessionId,
|
||||||
title: finalTitle,
|
title: finalTitle,
|
||||||
isTitleManuallyEdited: state.isTitleManuallyEdited ?? existingSession?.isTitleManuallyEdited ?? false,
|
isTitleManuallyEdited:
|
||||||
|
state.isTitleManuallyEdited ??
|
||||||
|
existingSession?.isTitleManuallyEdited ??
|
||||||
|
false,
|
||||||
createdAt: shouldAnchorCreatedAtToFirstMessage
|
createdAt: shouldAnchorCreatedAtToFirstMessage
|
||||||
? now
|
? now
|
||||||
: existingSession?.createdAt ?? now,
|
: existingSession?.createdAt ?? now,
|
||||||
@@ -360,7 +368,9 @@ export const createEmptyChatSession = async (): Promise<LoadedChatState> => {
|
|||||||
return toLoadedChatState(session);
|
return toLoadedChatState(session);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const loadChatSessionById = async (sessionId: string): Promise<LoadedChatState> => {
|
export const loadChatSessionById = async (
|
||||||
|
sessionId: string,
|
||||||
|
): Promise<LoadedChatState> => {
|
||||||
if (typeof window === "undefined") return emptyLoadedChatState();
|
if (typeof window === "undefined") return emptyLoadedChatState();
|
||||||
|
|
||||||
await migrateLegacyLocalStorage();
|
await migrateLegacyLocalStorage();
|
||||||
@@ -380,14 +390,18 @@ export const loadChatSessionById = async (sessionId: string): Promise<LoadedChat
|
|||||||
return toLoadedChatState(session);
|
return toLoadedChatState(session);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteChatSession = async (sessionId: string): Promise<string | undefined> => {
|
export const deleteChatSession = async (
|
||||||
|
sessionId: string,
|
||||||
|
): Promise<string | undefined> => {
|
||||||
if (typeof window === "undefined") return undefined;
|
if (typeof window === "undefined") return undefined;
|
||||||
|
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
await db.delete(SESSION_STORE, sessionId);
|
await db.delete(SESSION_STORE, sessionId);
|
||||||
|
|
||||||
const remainingSessions = await db.getAll(SESSION_STORE);
|
const remainingSessions = await db.getAll(SESSION_STORE);
|
||||||
const nextActiveSession = remainingSessions.sort(compareSessionsByAnchorTime)[0];
|
const nextActiveSession = remainingSessions.sort(
|
||||||
|
compareSessionsByAnchorTime,
|
||||||
|
)[0];
|
||||||
const meta = await getMeta();
|
const meta = await getMeta();
|
||||||
|
|
||||||
await setMeta({
|
await setMeta({
|
||||||
|
|||||||
Reference in New Issue
Block a user