"use client"; import { connectUserToDevice, signOutAction } from "@/app/actions"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { LogOut } from "lucide-react"; import GeneralUserForm from "./UserForm"; import { Slider } from "@/components/ui/slider"; import { updateUser } from "@/db/users"; import _ from "lodash"; import { createClient } from "@/utils/supabase/client"; import React, { useCallback } from "react"; import { doesUserHaveADevice, updateDevice } from "@/db/devices"; import { useToast } from "@/components/ui/use-toast"; interface AppSettingsProps { selectedUser: IUser; heading: React.ReactNode; } const skipDeviceRegistration = process.env.NEXT_PUBLIC_SKIP_DEVICE_REGISTRATION === "True"; const AppSettings: React.FC = ({ selectedUser, heading, }) => { const supabase = createClient(); const { toast } = useToast(); const [isConnected, setIsConnected] = React.useState(false); const doctorFormRef = React.useRef<{ submitForm: () => void } | null>(null); const userFormRef = React.useRef<{ submitForm: () => void } | null>(null); const [deviceCode, setDeviceCode] = React.useState(""); const [error, setError] = React.useState(""); const handleSave = () => { if (selectedUser.user_info.user_type === "doctor") { doctorFormRef.current?.submitForm(); } else { userFormRef.current?.submitForm(); } }; const checkIfUserHasDevice = useCallback(async () => { setIsConnected( await doesUserHaveADevice(supabase, selectedUser.user_id) ); }, [selectedUser.user_id, supabase]); React.useEffect(() => { checkIfUserHasDevice(); }, [checkIfUserHasDevice]); const [volume, setVolume] = React.useState([ selectedUser.device?.volume ?? 50, ]); const debouncedUpdateVolume = _.debounce(async () => { if (selectedUser.device?.device_id) { await updateDevice( supabase, { volume: volume[0] }, selectedUser.device.device_id ); } }, 1000); // Adjust the debounce delay as needed const updateVolume = (value: number[]) => { setVolume(value); debouncedUpdateVolume(); }; const onSave = async (values: any, userType: "doctor" | "user", userId: string) => { await updateUser( supabase, { supervisee_age: values.supervisee_age, supervisee_name: values.supervisee_name, supervisee_persona: values.supervisee_persona, user_info: { user_type: userType, user_metadata: values, }, }, userId); toast({ description: "Your prefereces have been saved!", }); } return ( <> handleSave()} />

Device settings

{skipDeviceRegistration &&
You don't need to register your device because NEXT_PUBLIC_SKIP_DEVICE_REGISTRATION is set to True.
}
setDeviceCode(e.target.value)} placeholder={isConnected ? "**********" : "Enter your device code"} maxLength={100} />

{isConnected ? Registered! : error ? {error}. : "Enter your device code to register it." }

{isConnected &&

{volume}%

}
); }; export default AppSettings;