parent
0209ace221
commit
c37885e743
@ -67,7 +67,10 @@ export function ChatItem(props: {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className={styles["chat-item-delete"]} onClick={props.onDelete}>
|
<div
|
||||||
|
className={styles["chat-item-delete"]}
|
||||||
|
onClickCapture={props.onDelete}
|
||||||
|
>
|
||||||
<DeleteIcon />
|
<DeleteIcon />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -77,14 +80,14 @@ export function ChatItem(props: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ChatList(props: { narrow?: boolean }) {
|
export function ChatList(props: { narrow?: boolean }) {
|
||||||
const [sessions, selectedIndex, selectSession, removeSession, moveSession] =
|
const [sessions, selectedIndex, selectSession, moveSession] = useChatStore(
|
||||||
useChatStore((state) => [
|
(state) => [
|
||||||
state.sessions,
|
state.sessions,
|
||||||
state.currentSessionIndex,
|
state.currentSessionIndex,
|
||||||
state.selectSession,
|
state.selectSession,
|
||||||
state.removeSession,
|
|
||||||
state.moveSession,
|
state.moveSession,
|
||||||
]);
|
],
|
||||||
|
);
|
||||||
const chatStore = useChatStore();
|
const chatStore = useChatStore();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
@ -138,7 +138,11 @@ export function SideBar(props: { className?: string }) {
|
|||||||
<div className={styles["sidebar-action"] + " " + styles.mobile}>
|
<div className={styles["sidebar-action"] + " " + styles.mobile}>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon={<CloseIcon />}
|
icon={<CloseIcon />}
|
||||||
onClick={chatStore.deleteSession}
|
onClick={() => {
|
||||||
|
if (confirm(Locale.Home.DeleteChat)) {
|
||||||
|
chatStore.deleteSession(chatStore.currentSessionIndex);
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles["sidebar-action"]}>
|
<div className={styles["sidebar-action"]}>
|
||||||
|
@ -158,6 +158,7 @@ export type ToastProps = {
|
|||||||
text: string;
|
text: string;
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
};
|
};
|
||||||
|
onClose?: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function Toast(props: ToastProps) {
|
export function Toast(props: ToastProps) {
|
||||||
@ -167,7 +168,10 @@ export function Toast(props: ToastProps) {
|
|||||||
<span>{props.content}</span>
|
<span>{props.content}</span>
|
||||||
{props.action && (
|
{props.action && (
|
||||||
<button
|
<button
|
||||||
onClick={props.action.onClick}
|
onClick={() => {
|
||||||
|
props.action?.onClick?.();
|
||||||
|
props.onClose?.();
|
||||||
|
}}
|
||||||
className={styles["toast-action"]}
|
className={styles["toast-action"]}
|
||||||
>
|
>
|
||||||
{props.action.text}
|
{props.action.text}
|
||||||
@ -201,7 +205,7 @@ export function showToast(
|
|||||||
close();
|
close();
|
||||||
}, delay);
|
}, delay);
|
||||||
|
|
||||||
root.render(<Toast content={content} action={action} />);
|
root.render(<Toast content={content} action={action} onClose={close} />);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InputProps = React.HTMLProps<HTMLTextAreaElement> & {
|
export type InputProps = React.HTMLProps<HTMLTextAreaElement> & {
|
||||||
|
@ -83,11 +83,10 @@ interface ChatStore {
|
|||||||
currentSessionIndex: number;
|
currentSessionIndex: number;
|
||||||
globalId: number;
|
globalId: number;
|
||||||
clearSessions: () => void;
|
clearSessions: () => void;
|
||||||
removeSession: (index: number) => void;
|
|
||||||
moveSession: (from: number, to: number) => void;
|
moveSession: (from: number, to: number) => void;
|
||||||
selectSession: (index: number) => void;
|
selectSession: (index: number) => void;
|
||||||
newSession: (mask?: Mask) => void;
|
newSession: (mask?: Mask) => void;
|
||||||
deleteSession: (index?: number) => void;
|
deleteSession: (index: number) => void;
|
||||||
currentSession: () => ChatSession;
|
currentSession: () => ChatSession;
|
||||||
onNewMessage: (message: Message) => void;
|
onNewMessage: (message: Message) => void;
|
||||||
onUserInput: (content: string) => Promise<void>;
|
onUserInput: (content: string) => Promise<void>;
|
||||||
@ -130,31 +129,6 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
removeSession(index: number) {
|
|
||||||
set((state) => {
|
|
||||||
let nextIndex = state.currentSessionIndex;
|
|
||||||
const sessions = state.sessions;
|
|
||||||
|
|
||||||
if (sessions.length === 1) {
|
|
||||||
return {
|
|
||||||
currentSessionIndex: 0,
|
|
||||||
sessions: [createEmptySession()],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
sessions.splice(index, 1);
|
|
||||||
|
|
||||||
if (nextIndex === index) {
|
|
||||||
nextIndex -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
currentSessionIndex: nextIndex,
|
|
||||||
sessions,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
moveSession(from: number, to: number) {
|
moveSession(from: number, to: number) {
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const { sessions, currentSessionIndex: oldIndex } = state;
|
const { sessions, currentSessionIndex: oldIndex } = state;
|
||||||
@ -197,31 +171,46 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteSession(i?: number) {
|
deleteSession(index) {
|
||||||
const deletedSession = get().currentSession();
|
const deletingLastSession = get().sessions.length === 1;
|
||||||
const index = i ?? get().currentSessionIndex;
|
const deletedSession = get().sessions.at(index);
|
||||||
const isLastSession = get().sessions.length === 1;
|
|
||||||
if (!isMobileScreen() || confirm(Locale.Home.DeleteChat)) {
|
if (!deletedSession) return;
|
||||||
get().removeSession(index);
|
|
||||||
|
const sessions = get().sessions.slice();
|
||||||
|
sessions.splice(index, 1);
|
||||||
|
|
||||||
|
let nextIndex = Math.min(
|
||||||
|
get().currentSessionIndex,
|
||||||
|
sessions.length - 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (deletingLastSession) {
|
||||||
|
nextIndex = 0;
|
||||||
|
sessions.push(createEmptySession());
|
||||||
|
}
|
||||||
|
|
||||||
|
// for undo delete action
|
||||||
|
const restoreState = {
|
||||||
|
currentSessionIndex: get().currentSessionIndex,
|
||||||
|
sessions: get().sessions.slice(),
|
||||||
|
};
|
||||||
|
|
||||||
|
set(() => ({
|
||||||
|
currentSessionIndex: nextIndex,
|
||||||
|
sessions,
|
||||||
|
}));
|
||||||
|
|
||||||
showToast(
|
showToast(
|
||||||
Locale.Home.DeleteToast,
|
Locale.Home.DeleteToast,
|
||||||
{
|
{
|
||||||
text: Locale.Home.Revert,
|
text: Locale.Home.Revert,
|
||||||
onClick() {
|
onClick() {
|
||||||
set((state) => ({
|
set(() => restoreState);
|
||||||
sessions: state.sessions
|
|
||||||
.slice(0, index)
|
|
||||||
.concat([deletedSession])
|
|
||||||
.concat(
|
|
||||||
state.sessions.slice(index + Number(isLastSession)),
|
|
||||||
),
|
|
||||||
}));
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
5000,
|
5000,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
currentSession() {
|
currentSession() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user