Skip to content

Commit 29bf4b8

Browse files
committed
feat: prompt selector to side bar
1 parent a84f3d6 commit 29bf4b8

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

src/client/components/ChatV2/ChatV2.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ArrowDownward, ChevronLeft, MenuBookTwoTone, Tune } from '@mui/icons-material'
1+
import { ArrowDownward, ChevronLeft, MenuBookTwoTone, Tune, WidthFull } from '@mui/icons-material'
22
import HelpIcon from '@mui/icons-material/Help'
33
import RestartAltIcon from '@mui/icons-material/RestartAlt'
4+
45
import { Alert, Box, Drawer, Fab, FormControlLabel, Paper, Switch, Typography, useMediaQuery, useTheme } from '@mui/material'
56
import type { TFunction } from 'i18next'
67
import { enqueueSnackbar } from 'notistack'
@@ -32,9 +33,13 @@ import ToolResult from './ToolResult'
3233
import { OutlineButtonBlack } from './general/Buttons'
3334
import { ChatInfo } from './general/ChatInfo'
3435
import RagSelector from './RagSelector'
35-
import { SettingsModal } from './SettingsModal'
36+
import { SettingsModal, useUrlPromptId } from './SettingsModal'
3637
import { useChatStream } from './useChatStream'
3738
import { getCompletionStreamV3 } from './util'
39+
import PromptSelector from './PromptSelector'
40+
import { useQuery } from '@tanstack/react-query'
41+
import { useMutation } from '@tanstack/react-query'
42+
import apiClient from '../../util/apiClient'
3843

3944
function useLocalStorageStateWithURLDefault(key: string, defaultValue: string, urlKey: string) {
4045
const [value, setValue] = useLocalStorageState(key, defaultValue)
@@ -384,6 +389,8 @@ export const ChatV2 = () => {
384389
setRagIndexId={setRagIndexId}
385390
ragIndices={ragIndices}
386391
messages={messages}
392+
activePrompt={activePrompt}
393+
setActivePrompt={setActivePrompt}
387394
/>
388395
</Drawer>
389396
) : (
@@ -403,6 +410,8 @@ export const ChatV2 = () => {
403410
setRagIndexId={setRagIndexId}
404411
ragIndices={ragIndices}
405412
messages={messages}
413+
activePrompt={activePrompt}
414+
setActivePrompt={setActivePrompt}
406415
/>
407416
))}
408417

@@ -594,6 +603,8 @@ const LeftMenu = ({
594603
setRagIndexId,
595604
ragIndices,
596605
messages,
606+
activePrompt,
607+
setActivePrompt,
597608
}: {
598609
sx?: object
599610
course?: Course
@@ -607,7 +618,15 @@ const LeftMenu = ({
607618
setRagIndexId: React.Dispatch<React.SetStateAction<number | undefined>>
608619
ragIndices?: RagIndexAttributes[]
609620
messages: Message[]
621+
activePrompt: Prompt | undefined
622+
setActivePrompt: (prompt: Prompt | undefined) => void
610623
}) => {
624+
const urlPromptId = useUrlPromptId()
625+
const { data: myPrompts, refetch } = useQuery<Prompt[]>({
626+
queryKey: ['/prompts/my-prompts'],
627+
initialData: [],
628+
})
629+
611630
return (
612631
<Box
613632
sx={[
@@ -637,6 +656,15 @@ const LeftMenu = ({
637656
<OutlineButtonBlack startIcon={<HelpIcon />} onClick={() => setDisclaimerStatus(true)} data-testid="help-button">
638657
{t('info:title')}
639658
</OutlineButtonBlack>
659+
<PromptSelector
660+
sx={{ width: '100%' }}
661+
coursePrompts={course?.prompts ?? []}
662+
myPrompts={myPrompts}
663+
activePrompt={activePrompt}
664+
setActivePrompt={setActivePrompt}
665+
mandatoryPrompt={course?.prompts.find((p) => p.mandatory)}
666+
urlPrompt={course?.prompts.find((p) => p.id === urlPromptId)}
667+
/>
640668
{course && showRagSelector && (
641669
<>
642670
<Typography variant="h6" sx={{ mb: 1, display: 'flex', gap: 1, alignItems: 'center' }} fontWeight="bold">

src/client/components/ChatV2/PromptSelector.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { DeleteOutline, KeyboardArrowDown, Lock } from '@mui/icons-material'
2-
import { Box, Button, Divider, IconButton, ListSubheader, Menu, MenuItem } from '@mui/material'
1+
import { DeleteOutline, KeyboardArrowDown, Lock, AutoAwesome } from '@mui/icons-material'
2+
import { Box, Divider, IconButton, ListSubheader, Menu, MenuItem } from '@mui/material'
33
import { useState } from 'react'
44
import { useTranslation } from 'react-i18next'
55
import type { Prompt } from '../../types'
66
import { OutlineButtonBlack } from './general/Buttons'
77

88
const PromptSelector = ({
9+
sx = {},
910
coursePrompts,
1011
myPrompts,
1112
activePrompt,
1213
setActivePrompt,
1314
handleDeletePrompt,
1415
mandatoryPrompt,
1516
}: {
17+
sx?: object
1618
coursePrompts: Prompt[]
1719
myPrompts: Prompt[]
1820
activePrompt?: Prompt
1921
setActivePrompt: (prompt: Prompt | undefined) => void
20-
handleDeletePrompt: (prompt: Prompt) => void
22+
handleDeletePrompt?: ((prompt: Prompt) => void)
2123
mandatoryPrompt?: Prompt
2224
urlPrompt?: Prompt
2325
}) => {
@@ -32,12 +34,15 @@ const PromptSelector = ({
3234

3335
const handleDelete = (event: React.MouseEvent<HTMLButtonElement>, prompt: Prompt) => {
3436
event.stopPropagation()
35-
if (confirm(t('settings:confirmDeletePrompt', { name: prompt.name }))) handleDeletePrompt(prompt)
37+
if (handleDeletePrompt)
38+
if (confirm(t('settings:confirmDeletePrompt', { name: prompt.name }))) handleDeletePrompt(prompt)
3639
}
3740

3841
return (
39-
<Box mb={'0.5rem'}>
42+
<Box sx={{ marginBottom: '0.5rem' }}>
4043
<OutlineButtonBlack
44+
sx={sx}
45+
startIcon={<AutoAwesome />}
4146
data-testid="prompt-selector-button"
4247
disabled={!!mandatoryPrompt}
4348
onClick={(event) => {
@@ -48,8 +53,17 @@ const PromptSelector = ({
4853
{activePrompt?.name ?? t('settings:choosePrompt')}
4954
{!!mandatoryPrompt && ` - ${t('settings:promptLocked')}`}
5055
</OutlineButtonBlack>
51-
52-
<Menu anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={() => setAnchorEl(null)}>
56+
<Menu
57+
anchorEl={anchorEl}
58+
open={Boolean(anchorEl)}
59+
onClose={() => setAnchorEl(null)}
60+
slotProps={{
61+
paper: {
62+
style: {
63+
minWidth: anchorEl?.offsetWidth || 200,
64+
},
65+
},
66+
}}>
5367
<MenuItem onClick={() => handleSelect(undefined)}>{t('settings:default')}</MenuItem>
5468
{coursePrompts.length > 0 && (
5569
<>
@@ -70,9 +84,11 @@ const PromptSelector = ({
7084
<MenuItem key={prompt.id} selected={prompt.id === activePrompt?.id} onClick={() => handleSelect(prompt)}>
7185
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%' }}>
7286
{prompt.name}
73-
<IconButton onClick={(event) => handleDelete(event, prompt)} size="small" sx={{ ml: 'auto' }}>
74-
<DeleteOutline fontSize="small" />
75-
</IconButton>
87+
{handleDeletePrompt &&
88+
<IconButton onClick={(event) => handleDelete(event, prompt)} size="small" sx={{ ml: 'auto' }}>
89+
<DeleteOutline fontSize="small" />
90+
</IconButton>
91+
}
7692
</Box>
7793
</MenuItem>
7894
))}

src/client/components/ChatV2/SettingsModal.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Add, Close, Settings } from '@mui/icons-material'
1+
import { Add, Close } from '@mui/icons-material'
22
import { Box, IconButton, Modal, Slider, Typography } from '@mui/material'
3-
import { useEffect, useRef, useState } from 'react'
3+
import { useEffect, useState } from 'react'
44
import { useTranslation } from 'react-i18next'
55
import { DEFAULT_ASSISTANT_INSTRUCTIONS, DEFAULT_MODEL_TEMPERATURE } from '../../../config'
66
import type { Course, Prompt } from '../../types'
@@ -12,11 +12,10 @@ import apiClient from '../../util/apiClient'
1212
import { useSearchParams } from 'react-router-dom'
1313
import { BlueButton, OutlineButtonBlack } from './general/Buttons'
1414
import { useAnalyticsDispatch } from '../../stores/analytics'
15-
import useLocalStorageState from '../../hooks/useLocalStorageState'
1615
import { isAxiosError } from 'axios'
1716
import { SendPreferenceConfiguratorButton } from './SendPreferenceConfigurator'
1817

19-
const useUrlPromptId = () => {
18+
export const useUrlPromptId = () => {
2019
const [searchParams] = useSearchParams()
2120
const promptId = searchParams.get('promptId')
2221
return promptId

0 commit comments

Comments
 (0)