refator: sd

This commit is contained in:
Dogtiti
2024-07-23 00:51:58 +08:00
parent e468fecf12
commit 9d55adbaf2
12 changed files with 579 additions and 441 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useMemo, useState } from "react";
import { useEffect, useRef, useMemo, useState } from "react";
import styles from "./home.module.scss";
@@ -15,7 +15,7 @@ import DragIcon from "../icons/drag.svg";
import Locale from "../locales";
import { ModelType, useAppConfig, useChatStore } from "../store";
import { useAppConfig, useChatStore } from "../store";
import {
DEFAULT_SIDEBAR_WIDTH,
@@ -27,20 +27,16 @@ import {
REPO_URL,
} from "../constant";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { Link, useNavigate } from "react-router-dom";
import { isIOS, useMobileScreen } from "../utils";
import dynamic from "next/dynamic";
import { Selector, showConfirm, showToast } from "./ui-lib";
import { showConfirm, Selector } from "./ui-lib";
const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
loading: () => null,
});
const SdPanel = dynamic(async () => (await import("./sd-panel")).SdPanel, {
loading: () => null,
});
function useHotKey() {
export function useHotKey() {
const chatStore = useChatStore();
useEffect(() => {
@@ -59,7 +55,7 @@ function useHotKey() {
});
}
function useDragSideBar() {
export function useDragSideBar() {
const limit = (x: number) => Math.min(MAX_SIDEBAR_WIDTH, x);
const config = useAppConfig();
@@ -132,39 +128,21 @@ function useDragSideBar() {
shouldNarrow,
};
}
export function SideBar(props: { className?: string }) {
const chatStore = useChatStore();
// drag side bar
const { onDragStart, shouldNarrow } = useDragSideBar();
const navigate = useNavigate();
const config = useAppConfig();
export function SideBarContainer(props: {
children: React.ReactNode;
onDragStart: (e: MouseEvent) => void;
shouldNarrow: boolean;
className?: string;
}) {
const isMobileScreen = useMobileScreen();
const isIOSMobile = useMemo(
() => isIOS() && isMobileScreen,
[isMobileScreen],
);
const [showPluginSelector, setShowPluginSelector] = useState(false);
const location = useLocation();
useHotKey();
let bodyComponent: React.JSX.Element;
let isChat: boolean = false;
switch (location.pathname) {
case Path.Sd:
case Path.SdPanel:
bodyComponent = <SdPanel />;
break;
default:
isChat = true;
bodyComponent = <ChatList narrow={shouldNarrow} />;
}
// @ts-ignore
const { children, className, onDragStart, shouldNarrow } = props;
return (
<div
className={`${styles.sidebar} ${props.className} ${
className={`${styles.sidebar} ${className} ${
shouldNarrow && styles["narrow-sidebar"]
}`}
style={{
@@ -172,6 +150,25 @@ export function SideBar(props: { className?: string }) {
transition: isMobileScreen && isIOSMobile ? "none" : undefined,
}}
>
{children}
<div
className={styles["sidebar-drag"]}
onPointerDown={(e) => onDragStart(e as any)}
>
<DragIcon />
</div>
</div>
);
}
export function SideBarHeader(props: { shouldNarrow: boolean }) {
const navigate = useNavigate();
const config = useAppConfig();
const { shouldNarrow } = props;
const [showPluginSelector, setShowPluginSelector] = useState(false);
return (
<>
<div className={styles["sidebar-header"]} data-tauri-drag-region>
<div className={styles["sidebar-title"]} data-tauri-drag-region>
NextChat
@@ -206,68 +203,6 @@ export function SideBar(props: { className?: string }) {
shadow
/>
</div>
<div
className={styles["sidebar-body"]}
onClick={(e) => {
if (isChat && e.target === e.currentTarget) {
navigate(Path.Home);
}
}}
>
{bodyComponent}
</div>
<div className={styles["sidebar-tail"]}>
<div className={styles["sidebar-actions"]}>
{isChat && (
<div className={styles["sidebar-action"] + " " + styles.mobile}>
<IconButton
icon={<DeleteIcon />}
onClick={async () => {
if (await showConfirm(Locale.Home.DeleteChat)) {
chatStore.deleteSession(chatStore.currentSessionIndex);
}
}}
/>
</div>
)}
<div className={styles["sidebar-action"]}>
<Link to={Path.Settings}>
<IconButton icon={<SettingsIcon />} shadow />
</Link>
</div>
<div className={styles["sidebar-action"]}>
<a href={REPO_URL} target="_blank" rel="noopener noreferrer">
<IconButton icon={<GithubIcon />} shadow />
</a>
</div>
</div>
{isChat && (
<div>
<IconButton
icon={<AddIcon />}
text={shouldNarrow ? undefined : Locale.Home.NewChat}
onClick={() => {
if (config.dontShowMaskSplashScreen) {
chatStore.newSession();
navigate(Path.Chat);
} else {
navigate(Path.NewChat);
}
}}
shadow
/>
</div>
)}
</div>
<div
className={styles["sidebar-drag"]}
onPointerDown={(e) => onDragStart(e as any)}
>
<DragIcon />
</div>
{showPluginSelector && (
<Selector
items={[
@@ -289,6 +224,92 @@ export function SideBar(props: { className?: string }) {
}}
/>
)}
</>
);
}
export function SideBarBody(props: {
children: React.ReactNode;
onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
}) {
const { onClick, children } = props;
return (
<div className={styles["sidebar-body"]} onClick={onClick}>
{children}
</div>
);
}
export function SideBarTail(props: { shouldNarrow: boolean }) {
const { shouldNarrow } = props;
const chatStore = useChatStore();
const navigate = useNavigate();
const config = useAppConfig();
return (
<div className={styles["sidebar-tail"]}>
<div className={styles["sidebar-actions"]}>
<div className={styles["sidebar-action"] + " " + styles.mobile}>
<IconButton
icon={<DeleteIcon />}
onClick={async () => {
if (await showConfirm(Locale.Home.DeleteChat)) {
chatStore.deleteSession(chatStore.currentSessionIndex);
}
}}
/>
</div>
<div className={styles["sidebar-action"]}>
<Link to={Path.Settings}>
<IconButton icon={<SettingsIcon />} shadow />
</Link>
</div>
<div className={styles["sidebar-action"]}>
<a href={REPO_URL} target="_blank" rel="noopener noreferrer">
<IconButton icon={<GithubIcon />} shadow />
</a>
</div>
</div>
<div>
<IconButton
icon={<AddIcon />}
text={shouldNarrow ? undefined : Locale.Home.NewChat}
onClick={() => {
if (config.dontShowMaskSplashScreen) {
chatStore.newSession();
navigate(Path.Chat);
} else {
navigate(Path.NewChat);
}
}}
shadow
/>
</div>
</div>
);
}
export function SideBar(props: { className?: string }) {
useHotKey();
const { onDragStart, shouldNarrow } = useDragSideBar();
const navigate = useNavigate();
return (
<SideBarContainer
onDragStart={onDragStart}
shouldNarrow={shouldNarrow}
{...props}
>
<SideBarHeader shouldNarrow={shouldNarrow} />
<SideBarBody
onClick={(e) => {
if (e.target === e.currentTarget) {
navigate(Path.Home);
}
}}
>
<ChatList narrow={shouldNarrow} />
</SideBarBody>
<SideBarTail shouldNarrow={shouldNarrow} />
</SideBarContainer>
);
}