@@ -5,86 +5,89 @@ import { load } from "@tauri-apps/plugin-store";
55import React , { useEffect , useRef , useState } from "react" ;
66
77export default function UserContext ( ) {
8- const [ isLoading , setIsLoading ] = useState ( false ) ;
9- const textAreaRef = useRef < HTMLTextAreaElement > ( null ) ;
10- const [ userContext , setUserContext ] = useState < { value : string } | null > ( null ) ;
8+ const [ isLoading , setIsLoading ] = useState ( false ) ;
9+ const textAreaRef = useRef < HTMLTextAreaElement > ( null ) ;
10+ const [ userContext , setUserContext ] = useState < { value : string } | null > ( null ) ;
1111
12- const showUserContextToast = ( content : string ) => {
13- toast ( {
14- id : "user-context" ,
15- title : "User Context" ,
16- content : content ,
17- dismissible : true ,
18- } ) ;
19- } ;
12+ const showUserContextToast = ( content : string ) => {
13+ toast ( {
14+ id : "user-context" ,
15+ title : "User Context" ,
16+ content : content ,
17+ dismissible : true ,
18+ } ) ;
19+ } ;
2020
21- const getStore = async ( ) => {
22- return await load ( "store.json" , { autoSave : false } ) ;
23- } ;
21+ const getStore = async ( ) => {
22+ return await load ( "store.json" , { autoSave : false } ) ;
23+ } ;
2424
25- const getUserContext = async ( ) : Promise < { value : string } | null > => {
26- let store = await getStore ( ) ;
27- let userContext = await store . get ( "user_context" ) ;
25+ const getUserContext = async ( ) : Promise < { value : string } | null > => {
26+ let store = await getStore ( ) ;
27+ let userContext = await store . get ( "user_context" ) ;
2828
29- return userContext as { value : string } | null ;
30- } ;
29+ return userContext as { value : string } | null ;
30+ } ;
3131
32- const handleSave = async ( ) => {
33- try {
34- setIsLoading ( true ) ;
35- let store = await getStore ( ) ;
32+ const handleSave = async ( ) => {
33+ try {
34+ setIsLoading ( true ) ;
35+ let store = await getStore ( ) ;
3636
37- if ( ! store ) {
38- showUserContextToast ( "Failed to retrieve user store" ) ;
39- setIsLoading ( false ) ;
40- return ;
41- }
37+ const value = textAreaRef . current ?. value ?. trim ( ) ;
4238
43- if ( ! textAreaRef ?. current ?. value ) {
44- showUserContextToast ( "Failed to save user context " ) ;
45- setIsLoading ( false ) ;
46- return ;
47- }
39+ if ( ! store ) {
40+ showUserContextToast ( "Failed to retrieve user store " ) ;
41+ setIsLoading ( false ) ;
42+ return ;
43+ }
4844
49- store . set ( "user_context" , { value : textAreaRef ?. current ?. value } ) ;
50- await store . save ( ) ;
51- showUserContextToast ( "User context saved successfully" ) ;
52- setIsLoading ( false ) ;
53- } catch ( error ) {
54- setIsLoading ( false ) ;
55- console . log ( "Failed to save user context with error " , error ) ;
56- }
57- } ;
45+ if ( ! value ) {
46+ showUserContextToast ( "Please enter some content before saving." ) ;
47+ setIsLoading ( false ) ;
48+ return ;
49+ }
5850
59- useEffect ( ( ) => {
60- getUserContext ( ) . then ( ( val ) => {
61- setUserContext ( val ) ;
62- } ) . catch ( ( e ) => {
63- console . log ( e ) ;
64- } ) ;
65- } , [ ] ) ;
51+ store . set ( "user_context" , { value } ) ;
52+ await store . save ( ) ;
53+ showUserContextToast ( "User context saved successfully" ) ;
54+ setIsLoading ( false ) ;
55+ } catch ( error ) {
56+ setIsLoading ( false ) ;
57+ console . log ( "Failed to save user context with error " , error ) ;
58+ }
59+ } ;
6660
67- return (
68- < div className = "flex-1 " >
69- < div className = "mb-2" >
70- < p className = "text-black " > User Context</ p >
71- </ div >
72- < Textarea
73- className = "h-full"
74- ref = { textAreaRef }
75- placeholder = { `${ userContext ?. value || "Enter details about yourself" } ` }
76- >
77- </ Textarea >
61+ useEffect ( ( ) => {
62+ getUserContext ( ) . then ( ( val ) => {
63+ setUserContext ( val ) ;
64+ } ) . catch ( ( e ) => {
65+ console . log ( e ) ;
66+ } ) ;
67+ } , [ ] ) ;
7868
79- < div className = "mt-2 flex flex-row w-full justify-center" >
80- < Button
81- isLoading = { isLoading }
82- className = "w-3/4"
83- onClick = { handleSave }
84- >
85- < span className = "text-white" > Save</ span >
86- </ Button >
87- </ div >
88- </ div >
89- ) ;
69+ return (
70+ < div className = "flex-1 " >
71+ < div className = "mb-2" >
72+ < p className = "text-black " > User Context</ p >
73+ </ div >
74+ < Textarea
75+ className = "h-full"
76+ ref = { textAreaRef }
77+ placeholder = "Enter details about yourself"
78+ defaultValue = { userContext ?. value ?? "" }
79+ >
80+ </ Textarea >
81+
82+ < div className = "mt-2 flex flex-row w-full justify-center" >
83+ < Button
84+ isLoading = { isLoading }
85+ className = "w-3/4"
86+ onClick = { handleSave }
87+ >
88+ < span className = "text-white" > Save</ span >
89+ </ Button >
90+ </ div >
91+ </ div >
92+ ) ;
9093}
0 commit comments