Skip to content

Commit

Permalink
feat: improve auto scroll ux and edit model title
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Aug 2, 2023
1 parent cbabb93 commit b5ef552
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/components/chat.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
padding: 4px 10px;
animation: slide-in ease 0.3s;
box-shadow: var(--card-shadow);
transition: all ease 0.3s;
transition: width ease 0.3s;
align-items: center;
height: 16px;
width: var(--icon-width);
Expand Down
31 changes: 20 additions & 11 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,27 @@ function ChatAction(props: {
function useScrollToBottom() {
// for auto-scroll
const scrollRef = useRef<HTMLDivElement>(null);
const [autoScroll, setAutoScroll] = useState(true);
const autoScroll = useRef(true);
const scrollToBottom = useCallback(() => {
const dom = scrollRef.current;
if (dom) {
requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight));
}
}, []);
const setAutoScroll = (enable: boolean) => {
autoScroll.current = enable;
};

// auto scroll
useEffect(() => {
autoScroll && scrollToBottom();
});
const intervalId = setInterval(() => {
if (autoScroll.current) {
scrollToBottom();
}
}, 100);
return () => clearInterval(intervalId);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return {
scrollRef,
Expand Down Expand Up @@ -532,7 +541,7 @@ export function EditMessageModal(props: { onClose: () => void }) {
return (
<div className="modal-mask">
<Modal
title={Locale.UI.Edit}
title={Locale.Chat.EditMessage.Title}
onClose={props.onClose}
actions={[
<IconButton
Expand Down Expand Up @@ -590,10 +599,7 @@ export function Chat() {
type RenderMessage = ChatMessage & { preview?: boolean };

const chatStore = useChatStore();
const [session, sessionIndex] = useChatStore((state) => [
state.currentSession(),
state.currentSessionIndex,
]);
const session = chatStore.currentSession();
const config = useAppConfig();
const fontSize = config.fontSize;

Expand All @@ -608,9 +614,14 @@ export function Chat() {
const isMobileScreen = useMobileScreen();
const navigate = useNavigate();

const lastBodyScroolTop = useRef(0);
const onChatBodyScroll = (e: HTMLElement) => {
const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 10;
setHitBottom(isTouchBottom);

// only enable auto scroll when scroll down and touched bottom
setAutoScroll(e.scrollTop >= lastBodyScroolTop.current && isTouchBottom);
lastBodyScroolTop.current = e.scrollTop;
};

// prompt hints
Expand Down Expand Up @@ -1036,7 +1047,6 @@ export function Chat() {
ref={scrollRef}
onScroll={(e) => onChatBodyScroll(e.currentTarget)}
onMouseDown={() => inputRef.current?.blur()}
onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
onTouchStart={() => {
inputRef.current?.blur();
setAutoScroll(false);
Expand Down Expand Up @@ -1148,7 +1158,7 @@ export function Chat() {
}}
fontSize={fontSize}
parentRef={scrollRef}
defaultShow={i >= messages.length - 10}
defaultShow={i >= messages.length - 6}
/>
</div>

Expand Down Expand Up @@ -1193,7 +1203,6 @@ export function Chat() {
value={userInput}
onKeyDown={onInputKeyDown}
onFocus={() => setAutoScroll(true)}
onBlur={() => setAutoScroll(false)}
rows={inputRows}
autoFocus={autoFocus}
style={{
Expand Down
4 changes: 2 additions & 2 deletions app/components/model-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function ModelConfigList(props: {
<input
type="number"
min={100}
max={32000}
max={100000}
value={props.modelConfig.max_tokens}
onChange={(e) =>
props.updateConfig(
Expand Down Expand Up @@ -169,7 +169,7 @@ export function ModelConfigList(props: {
title={props.modelConfig.historyMessageCount.toString()}
value={props.modelConfig.historyMessageCount}
min="0"
max="32"
max="64"
step="1"
onChange={(e) =>
props.updateConfig(
Expand Down
3 changes: 2 additions & 1 deletion app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const cn = {
Chat: {
SubTitle: (count: number) => `共 ${count} 条对话`,
EditMessage: {
Title: "编辑消息记录",
Topic: {
Title: "聊天主题",
SubTitle: "更改当前聊天主题",
Expand Down Expand Up @@ -274,7 +275,7 @@ const cn = {
Context: {
Toast: (x: any) => `包含 ${x} 条预设提示词`,
Edit: "当前对话设置",
Add: "新增预设对话",
Add: "新增一条对话",
Clear: "上下文已清除",
Revert: "恢复上下文",
},
Expand Down
1 change: 1 addition & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const en: LocaleType = {
Chat: {
SubTitle: (count: number) => `${count} messages`,
EditMessage: {
Title: "Edit All Messages",
Topic: {
Title: "Topic",
SubTitle: "Change the current topic",
Expand Down
2 changes: 1 addition & 1 deletion app/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const ModalConfigValidator = {
return x as ModelType;
},
max_tokens(x: number) {
return limitNumber(x, 0, 32000, 2000);
return limitNumber(x, 0, 100000, 2000);
},
presence_penalty(x: number) {
return limitNumber(x, -2, 2, 0);
Expand Down

0 comments on commit b5ef552

Please sign in to comment.