File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { FormProvider , useForm } from "react-hook-form" ;
2
+ import { getCachedChatForm , useCacheChatForm } from "src/hooks/chat/useCacheConfig" ;
2
3
import { ChatConfigForm } from "src/types/Chat" ;
3
4
4
5
import { ChatConfigSummary } from "./ChatConfigSummary" ;
@@ -10,12 +11,16 @@ export const ChatSection = ({ chatId }: { chatId: string }) => {
10
11
11
12
console . assert ( modelInfos . length > 0 , "No model config was found" ) ;
12
13
const form = useForm < ChatConfigForm > ( {
13
- defaultValues : {
14
+ //NOTE: we should at some point validate the cache, in case models were added or deleted
15
+ // or certain parameters disabled
16
+ defaultValues : getCachedChatForm ( ) ?? {
14
17
...modelInfos [ 0 ] . parameter_configs [ 0 ] . sampling_parameters ,
15
18
model_config_name : modelInfos [ 0 ] . name ,
16
19
} ,
17
20
} ) ;
18
21
22
+ useCacheChatForm ( form ) ;
23
+
19
24
return (
20
25
< FormProvider { ...form } >
21
26
< ChatConversation chatId = { chatId } />
Original file line number Diff line number Diff line change
1
+ import { useEffect } from "react" ;
2
+ import { UseFormReturn } from "react-hook-form" ;
3
+ import { ChatConfigForm } from "src/types/Chat" ;
4
+
5
+ const CHAT_CONFIG_KEY = "CHAT_CONFIG" ;
6
+
7
+ export const getCachedChatForm = ( ) : ChatConfigForm | null => {
8
+ if ( typeof localStorage !== "undefined" ) {
9
+ const oldConfig = localStorage . getItem ( CHAT_CONFIG_KEY ) ;
10
+ if ( oldConfig ) {
11
+ return JSON . parse ( oldConfig ) ;
12
+ }
13
+ }
14
+ return null ;
15
+ } ;
16
+
17
+ export const useCacheChatForm = ( form : UseFormReturn < ChatConfigForm > ) => {
18
+ const config = form . watch ( ) ;
19
+
20
+ useEffect ( ( ) => {
21
+ if ( typeof localStorage !== "undefined" ) {
22
+ localStorage . setItem ( CHAT_CONFIG_KEY , JSON . stringify ( config ) ) ;
23
+ }
24
+ } , [ config ] ) ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments