'use client' import type { UseTRPCQueryResult } from "node_modules/@trpc/react-query/dist/getQueryKey.d-CruH3ncI.mjs" import { useEffect, useRef, useState } from "react" function useRelationShipSuccess & {id:string},K extends keyof T>( relationShipData: T[] | undefined, relationShipNameIndex: K, setRelationShipId: (arg0: string | undefined) => void, setRelationShipName: (arg0: string | undefined) => void, realtionShipDataSuccess: boolean, id: string|undefined, nameCallBack?: (arg0:T[K]) => string, ){ useEffect(() => { if (id === undefined) { return } setRelationShipId(relationShipData?.at(0)?.id) if (relationShipData !== undefined && relationShipData[0] !==undefined) { if (relationShipData[0][relationShipNameIndex] !== null) { if (nameCallBack !== undefined) { setRelationShipName(nameCallBack(relationShipData[0][relationShipNameIndex])) return } setRelationShipName(relationShipData[0][relationShipNameIndex]) } } }, [realtionShipDataSuccess]) } export function useRelationShip & {id: string},TE>( queryResult: UseTRPCQueryResult, relationShipNameIndex: keyof T, dependsOnId:string|undefined, ) { const [id, setRelationShipId] = useState() const [name, setRealtionShipName] = useState() const { data: data, isSuccess: success } = queryResult useRelationShipSuccess(data,relationShipNameIndex,setRelationShipId,setRealtionShipName,success,dependsOnId) return {data,id,name,success} } export function makeUseRelationShipWithNameIndex(key:K) { return function useRelationShip & { id: string },TE>( queryResult: UseTRPCQueryResult, dependsOnId:string|undefined, nameCallBack: (arg0:T[K]) => string ) { const [id, setRelationShipId] = useState() const [name, setRealtionShipName] = useState() const { data: data, isSuccess: success, error } = queryResult useRelationShipSuccess(data,key,setRelationShipId,setRealtionShipName,success,dependsOnId,nameCallBack) return {data,error,id,name,success} } } export function usePrevious(value:T,initialValue:T) { const ref = useRef(initialValue) useEffect(() => { ref.current = value }) return ref.current; }