|
| 1 | +import { Button } from "@hypr/ui/components/ui/button"; |
| 2 | +import { Textarea } from "@hypr/ui/components/ui/textarea"; |
| 3 | +import { toast } from "@hypr/ui/components/ui/toast"; |
| 4 | +import { load } from "@tauri-apps/plugin-store"; |
| 5 | +import React, { useEffect, useRef, useState } from "react"; |
| 6 | + |
| 7 | +export default function UserContext() { |
| 8 | + const [isLoading, setIsLoading] = useState(false); |
| 9 | + const textAreaRef = useRef<HTMLTextAreaElement>(null); |
| 10 | + const [userContext, setUserContext] = useState<{ value: string } | null>(null); |
| 11 | + |
| 12 | + const showUserContextToast = (content: string) => { |
| 13 | + toast({ |
| 14 | + id: "user-context", |
| 15 | + title: "User Context", |
| 16 | + content: content, |
| 17 | + dismissible: true, |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + const getStore = async () => { |
| 22 | + return await load("store.json", { autoSave: false }); |
| 23 | + }; |
| 24 | + |
| 25 | + const getUserContext = async (): Promise<{ value: string } | null> => { |
| 26 | + let store = await getStore(); |
| 27 | + let userContext = await store.get("user_context"); |
| 28 | + |
| 29 | + return userContext as { value: string } | null; |
| 30 | + }; |
| 31 | + |
| 32 | + const handleSave = async () => { |
| 33 | + try { |
| 34 | + setIsLoading(true); |
| 35 | + let store = await getStore(); |
| 36 | + |
| 37 | + if (!store) { |
| 38 | + showUserContextToast("Failed to retrieve user store"); |
| 39 | + setIsLoading(false); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!textAreaRef?.current?.value) { |
| 44 | + showUserContextToast("Failed to save user context"); |
| 45 | + setIsLoading(false); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 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 | + }; |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + getUserContext().then((val) => { |
| 61 | + setUserContext(val); |
| 62 | + }).catch((e) => { |
| 63 | + console.log(e); |
| 64 | + }); |
| 65 | + }, []); |
| 66 | + |
| 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> |
| 78 | + |
| 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 | + ); |
| 90 | +} |
0 commit comments