Skip to content

Commit

Permalink
feat: show history list under the command bar and fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chen authored and chen committed Apr 2, 2023
1 parent ac07586 commit 0516809
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
faTimes,
} from '@fortawesome/free-solid-svg-icons'
import { faChevronsLeft } from '@fortawesome/pro-regular-svg-icons'
import { setChatOpen, toggleChatHistory } from '../features/chat/chatSlice'
import { setChatHistoryOpen, setChatOpen, toggleChatHistory } from '../features/chat/chatSlice'
import { store } from './store'

export type Tip = [string, string, IconDefinition, () => void]
Expand All @@ -25,4 +25,10 @@ export const ActionTips: ActionTipsInterface = {
faChevronsLeft,
() => store.dispatch(toggleChatHistory()),
],
CLOSE_HISTORY_DIRECTLY: [
'Close History Directly',
'',
faTimes,
() => store.dispatch(setChatHistoryOpen(false))
]
}
47 changes: 39 additions & 8 deletions src/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ import ReactTextareaAutocomplete from '@webscopeio/react-textarea-autocomplete'

import Modal from 'react-modal'

export enum historyPosition {
COMMAND_BAR = 'COMMAND_BAR',
CHAT_POPUP ='CHAT_POPUP'
}

export function PreBlock({ children }: { children: ReactNode | ReactNode[] }) {
function getResult(child: ReactNode) {
if (React.isValidElement(child)) {
Expand Down Expand Up @@ -330,7 +335,7 @@ export function ChatPopup() {

function handleMouseDown() {
if (document.activeElement) {
;(document.activeElement as HTMLElement).blur()
(document.activeElement as HTMLElement).blur()
}
}
return (
Expand Down Expand Up @@ -370,7 +375,9 @@ export function ChatPopup() {
</div>
</div>
{isChatHistoryOpen && (
<ChatHistory onSelect={handleSelectHistory} />
<div className='w-80' >
<ChatHistory onSelect={handleSelectHistory}/>
</div>
)}
</div>
)}
Expand Down Expand Up @@ -781,18 +788,30 @@ function formatPromptPreview(prompt: string): string {
function ChatHistory(props: {
onSelect?: (id: string) => void
onClose?: () => void
position?: historyPosition

}) {
const { onSelect, position = historyPosition.CHAT_POPUP } = props
const conversationIds = useAppSelector(csel.getConversationIds)
const conversationPrompts = useAppSelector(
csel.getConversationPrompts(conversationIds, 'reverse')
)

const tips = useMemo(() => {
switch(position) {
case historyPosition.CHAT_POPUP: return ActionTips.CLOSE_HISTORY
case historyPosition.COMMAND_BAR: return ActionTips.CLOSE_HISTORY_DIRECTLY
default: return ActionTips.CLOSE_HISTORY
}
} ,[])

return (
<div className="flex flex-col items-center w-80 select-none">
<button className="w-full" onClick={props.onClose}>
<div className="flex flex-col items-center select-none mt-2.5 h-full">
{/* <button className="w-full" onClick={props.onClose}> */}
<button className="w-full">
<CommandBarActionTips
align="right"
tips={[ActionTips.CLOSE_HISTORY]}
tips={[tips]}
/>
</button>
<div className="flex flex-col w-full items-center space-y-1 mt-1 overflow-auto">
Expand All @@ -801,7 +820,7 @@ function ChatHistory(props: {
<button
key={msg.conversationId}
className="w-full bg-neutral-600 rounded-sm px-4 py-2"
onClick={() => props.onSelect?.(msg.conversationId)}
onClick={() => onSelect?.(msg.conversationId)}
>
<div
className={
Expand Down Expand Up @@ -830,6 +849,15 @@ export function CommandBar({
}) {
const dispatch = useAppDispatch()

const handleSelectHistory = (id: string) => {
dispatch(cs.toggleChatHistory())
dispatch(cs.setCurrentConversation(id))

}
const msgType = useAppSelector(csel.getMsgType)
const isChatHistoryOpen = useAppSelector<boolean>(csel.getHistoryStatus)


const customStyles = {
overlay: {
backgroundColor: 'rgba(0, 0, 0, 0.1)',
Expand All @@ -854,7 +882,7 @@ export function CommandBar({
}

const commandBarOpen = useAppSelector(csel.getIsCommandBarOpen)
const isChatHistoryAvailable = useAppSelector(
const _isChatHistoryAvailable = useAppSelector(
csel.getIsChatHistoryAvailable
)

Expand All @@ -869,7 +897,7 @@ export function CommandBar({
style={customStyles}
>
<div className="tipArea">
Previous
For Chat History Using
<div className="tipKeyCommand">
Ctrl+Shift+
<FontAwesomeIcon icon={faArrowUp} />
Expand All @@ -882,6 +910,9 @@ export function CommandBar({
</div>
</div>
</div>
{isChatHistoryOpen && msgType === 'freeform' && <div className='h-96' >
<ChatHistory onSelect={handleSelectHistory} position={historyPosition.COMMAND_BAR} />
</div>}
</Modal>
) : (
<div className="commandBar__container">
Expand Down
2 changes: 2 additions & 0 deletions src/features/chat/chatSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ export const getIsChatHistoryAvailable = createSelector(
export const getFireCommandK = (state: FullState) =>
state.chatState.fireCommandK
export const getMsgType = (state: FullState) => state.chatState.msgType

export const getHistoryStatus = (state: FullState) => state.chatState.chatHistoryIsOpen
13 changes: 10 additions & 3 deletions src/features/chat/chatSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export const chatSlice = createSlice({
openCommandBar(chatState: ChatState) {
chatState.isCommandBarOpen = true
chatState.chatIsOpen = false
chatState.chatHistoryIsOpen = true
const newConversationId = uuidv4()

chatState.currentConversationId = newConversationId
Expand Down Expand Up @@ -525,19 +526,24 @@ export const chatSlice = createSlice({
chatState.commandBarHistoryIndex

const historyMessage = chatState.userMessages.at(index)
const currentConversationId = chatState.currentConversationId
const currentDraftMessage =
chatState.draftMessages[currentConversationId]
if (historyMessage) {
// chatState.currentConversationId = historyMessage.conversationId
const currentConversationId = chatState.currentConversationId
const currentDraftMessage =
chatState.draftMessages[currentConversationId]
currentDraftMessage.message = historyMessage.message
} else {
currentDraftMessage.message = ''
}
// if (historyMessage) {
// chatState.commandBarText = historyMessage.message
// } else {
// chatState.commandBarText = ''
// }
},
setChatHistoryOpen(chatState: ChatState, action: PayloadAction<boolean>) {
chatState.chatHistoryIsOpen = action.payload
},
},
})

Expand Down Expand Up @@ -577,4 +583,5 @@ export const {
_submitCommandBar: dummySubmitCommandBar,
// Bad - I added tech debt and will fix later
setMaxOrigLine,
setChatHistoryOpen
} = chatSlice.actions
8 changes: 4 additions & 4 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2445,8 +2445,8 @@ free servers .disabled_command .shortcut__block {

.tipArea {
display: flex;
color: #999;
font-size: 12px;
color: #b3b2b2;
font-size: 14px;
align-items: center;
margin-bottom: 4px;
}
Expand All @@ -2456,7 +2456,7 @@ free servers .disabled_command .shortcut__block {
background-color: #444;
padding: 2px;
border-radius: 2px;
font-size: 10px;
font-size: 14px;
}

.leftDrag {
Expand Down Expand Up @@ -2583,4 +2583,4 @@ free servers .disabled_command .shortcut__block {

.markdownpopup p {
margin: 12px 0px;
}
}

0 comments on commit 0516809

Please sign in to comment.