Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions app/components/chat-list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DeleteIcon from "../icons/delete.svg";
import PinIcon from "../icons/pin.svg";

import styles from "./home.module.scss";
import {
Expand All @@ -15,18 +16,20 @@ import { useLocation, useNavigate } from "react-router-dom";
import { Path } from "../constant";
import { MaskAvatar } from "./mask";
import { Mask } from "../store/mask";
import { useRef, useEffect } from "react";
import { useRef, useEffect, useMemo } from "react";
import { showConfirm } from "./ui-lib";
import { useMobileScreen } from "../utils";
import clsx from "clsx";

export function ChatItem(props: {
onClick?: () => void;
onDelete?: () => void;
onPin?: () => void;
title: string;
count: number;
time: string;
selected: boolean;
pinned?: boolean;
id: string;
index: number;
narrow?: boolean;
Expand All @@ -50,6 +53,7 @@ export function ChatItem(props: {
[styles["chat-item-selected"]]:
props.selected &&
(currentPath === Path.Chat || currentPath === Path.Home),
[styles["chat-item-pinned"]]: props.pinned,
})}
onClick={props.onClick}
ref={(ele) => {
Expand Down Expand Up @@ -86,6 +90,22 @@ export function ChatItem(props: {
</>
)}

<div
className={styles["chat-item-pin"]}
onClickCapture={(e) => {
props.onPin?.();
e.preventDefault();
e.stopPropagation();
}}
title={
props.pinned
? Locale.ChatItem.UnpinChat
: Locale.ChatItem.PinChat
}
>
<PinIcon />
</div>

<div
className={styles["chat-item-delete"]}
onClickCapture={(e) => {
Expand Down Expand Up @@ -115,6 +135,17 @@ export function ChatList(props: { narrow?: boolean }) {
const navigate = useNavigate();
const isMobileScreen = useMobileScreen();

// Sort sessions: pinned first, then by original order
const sortedSessions = useMemo(() => {
return sessions
.map((session, originalIndex) => ({ session, originalIndex }))
.sort((a, b) => {
const aPinned = a.session.pinned ? 1 : 0;
const bPinned = b.session.pinned ? 1 : 0;
return bPinned - aPinned;
});
}, [sessions]);

const onDragEnd: OnDragEndResponder = (result) => {
const { destination, source } = result;
if (!destination) {
Expand All @@ -128,7 +159,10 @@ export function ChatList(props: { narrow?: boolean }) {
return;
}

moveSession(source.index, destination.index);
// Map sorted index back to original index for moveSession
const fromOriginal = sortedSessions[source.index].originalIndex;
const toOriginal = sortedSessions[destination.index].originalIndex;
moveSession(fromOriginal, toOriginal);
};

return (
Expand All @@ -140,29 +174,33 @@ export function ChatList(props: { narrow?: boolean }) {
ref={provided.innerRef}
{...provided.droppableProps}
>
{sessions.map((item, i) => (
{sortedSessions.map(({ session, originalIndex }, i) => (
<ChatItem
title={item.topic}
time={new Date(item.lastUpdate).toLocaleString()}
count={item.messages.length}
key={item.id}
id={item.id}
title={session.topic}
time={new Date(session.lastUpdate).toLocaleString()}
count={session.messages.length}
key={session.id}
id={session.id}
index={i}
selected={i === selectedIndex}
selected={originalIndex === selectedIndex}
pinned={session.pinned}
onClick={() => {
navigate(Path.Chat);
selectSession(i);
selectSession(originalIndex);
}}
onPin={() => {
chatStore.togglePin(originalIndex);
}}
onDelete={async () => {
if (
(!props.narrow && !isMobileScreen) ||
(await showConfirm(Locale.Home.DeleteChat))
) {
chatStore.deleteSession(i);
chatStore.deleteSession(originalIndex);
}
}}
narrow={props.narrow}
mask={item.mask}
mask={session.mask}
/>
))}
{provided.placeholder}
Expand Down
23 changes: 22 additions & 1 deletion app/components/home.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,45 @@
animation: slide-in ease 0.3s;
}

.chat-item-pin,
.chat-item-delete {
position: absolute;
top: 0;
right: 0;
transition: all ease 0.3s;
opacity: 0;
cursor: pointer;
}

.chat-item-pin {
right: 20px;
}

.chat-item-delete {
right: 0;
}

.chat-item:hover > .chat-item-pin,
.chat-item:hover > .chat-item-delete {
opacity: 0.5;
transform: translateX(-4px);
}

.chat-item:hover > .chat-item-pin:hover,
.chat-item:hover > .chat-item-delete:hover {
opacity: 1;
}

.chat-item-pinned {
.chat-item-pin {
opacity: 0.8;
color: var(--primary);
}

&:hover .chat-item-pin {
opacity: 1;
}
}

.chat-item-info {
display: flex;
justify-content: space-between;
Expand Down
23 changes: 23 additions & 0 deletions app/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MaskIcon from "../icons/mask.svg";
import McpIcon from "../icons/mcp.svg";
import DragIcon from "../icons/drag.svg";
import DiscoveryIcon from "../icons/discovery.svg";
import NewWindowIcon from "../icons/new-window.svg";

import Locale from "../locales";

Expand All @@ -32,6 +33,7 @@ import dynamic from "next/dynamic";
import { Selector, showConfirm } from "./ui-lib";
import clsx from "clsx";
import { isMcpEnabled } from "../mcp/actions";
import { openNewChatWindow } from "../utils/window";

const DISCOVERY = [
{ name: Locale.Plugin.Name, path: Path.Plugins },
Expand All @@ -48,6 +50,12 @@ export function useHotKey() {

useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === "n") {
// Ctrl+Shift+N: open a new app window with its own configuration (#4886)
e.preventDefault();
openNewChatWindow();
return;
}
if (e.altKey || e.ctrlKey) {
if (e.key === "ArrowUp") {
chatStore.nextSession(-1);
Expand Down Expand Up @@ -232,6 +240,11 @@ export function SideBar(props: { className?: string }) {
const config = useAppConfig();
const chatStore = useChatStore();
const [mcpEnabled, setMcpEnabled] = useState(false);
const [isApp, setIsApp] = useState(false);

useEffect(() => {
setIsApp(!!window.__TAURI__);
}, []);

useEffect(() => {
// 检查 MCP 是否启用
Expand Down Expand Up @@ -327,6 +340,16 @@ export function SideBar(props: { className?: string }) {
}}
/>
</div>
{isApp && (
<div className={styles["sidebar-action"]}>
<IconButton
aria={Locale.UI.NewWindow}
icon={<NewWindowIcon />}
onClick={() => openNewChatWindow()}
shadow
/>
</div>
)}
<div className={styles["sidebar-action"]}>
<Link to={Path.Settings}>
<IconButton
Expand Down
9 changes: 9 additions & 0 deletions app/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ declare interface Window {
__TAURI__?: {
writeText(text: string): Promise<void>;
invoke(command: string, payload?: Record<string, unknown>): Promise<any>;
window: {
getAll(): { label: string }[];
WebviewWindow: new (
label: string,
options?: Record<string, unknown>,
) => {
once(event: string, handler: (event: unknown) => void): void;
};
};
dialog: {
save(options?: Record<string, unknown>): Promise<string | null>;
};
Expand Down
1 change: 1 addition & 0 deletions app/icons/new-window.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const cn = {
},
ChatItem: {
ChatItemCount: (count: number) => `${count} 条对话`,
PinChat: "固定此对话",
UnpinChat: "取消固定",
},
Chat: {
SubTitle: (count: number) => `共 ${count} 条对话`,
Expand Down Expand Up @@ -795,6 +797,7 @@ const cn = {
Import: "导入",
Sync: "同步",
Config: "配置",
NewWindow: "新窗口",
},
Exporter: {
Description: {
Expand Down
3 changes: 3 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const en: LocaleType = {
},
ChatItem: {
ChatItemCount: (count: number) => `${count} messages`,
PinChat: "Pin this chat",
UnpinChat: "Unpin this chat",
},
Chat: {
SubTitle: (count: number) => `${count} messages`,
Expand Down Expand Up @@ -801,6 +803,7 @@ const en: LocaleType = {
Import: "Import",
Sync: "Sync",
Config: "Config",
NewWindow: "New Window",
},
Exporter: {
Description: {
Expand Down
14 changes: 14 additions & 0 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export interface ChatSession {
clearContextIndex?: number;

mask: Mask;

pinned?: boolean;
}

export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
Expand All @@ -116,6 +118,8 @@ function createEmptySession(): ChatSession {
lastSummarizeIndex: 0,

mask: createEmptyMask(),

pinned: false,
};
}

Expand Down Expand Up @@ -304,6 +308,16 @@ export const useChatStore = createPersistStore(
});
},

togglePin(index: number) {
set((state) => {
const sessions = [...state.sessions];
const session = { ...sessions[index] };
session.pinned = !session.pinned;
sessions[index] = session;
return { sessions };
});
},

newSession(mask?: Mask) {
const session = createEmptySession();

Expand Down
Loading