All files / src/hooks useAudioBookForDialogueSynthesisPhase.ts

0% Statements 0/15
0% Branches 0/4
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                                                     
import { AudioBook, AudioBookWithCharacterVoices, AudioBookWithCharacterVoicesSchema } from "@/schemas/AudioBook";
import z from "zod";
import { tRPC } from "@/utils/tRPC";
import { useEffect, useRef, useState } from "react";
import { produce } from "immer";
import debug from "debug";
import { useDebounce } from "./useDebounce";
import { AsyncStateType, SuccessAsyncStateType } from "@/app/types/AsyncState";
import { TRPCError } from "@trpc/server";
 
const logger = debug("UseAudioBookForDialogueSynthesisPhase");
debug.enable("UseAudioBookForDialogueSynthesisPhase");
 
export type AudioBookWithCharacterVoicesSuccessAsyncStateType = SuccessAsyncStateType<AudioBookWithCharacterVoices>;
export type AudioBookWithCharacterVoicesAsyncStateType = AsyncStateType<AudioBookWithCharacterVoices>;
 
export function useAudioBookForDialogueSynthesisPhase(projectID: string) {
    const query = tRPC.project.get.useQuery(projectID);
 
    const [audioBookWithCharacterVoicesState, setAudioBookWithCharacterVoicesState] =
        useState<AudioBookWithCharacterVoicesAsyncStateType>({ status: "PENDING" });
 
    // const init = useRef(true);
    // const [syncStatus, setSyncStatus] = useState<"IDLE" | "PENDING" | "SUCCESS" | "ERROR">("SUCCESS");
 
    // const debouncedValue = useDebounce(audioBookState.state === "success" ? audioBookState.audioBook : null, 5000);
 
    useEffect(() => {
        if (query.status === "pending") {
            return;
        }
 
        (async function () {
            if (query.status === "error") {
                setAudioBookWithCharacterVoicesState({
                    status: "ERROR",
                    error: new Error(query.error.message, { cause: query.error }),
                });
                return;
            }
 
            const audioBook = query.data;
            const audioBookWithCharacterVoices = await AudioBookWithCharacterVoicesSchema.parseAsync(audioBook);
 
            setAudioBookWithCharacterVoicesState({ status: "SUCCESS", data: audioBookWithCharacterVoices });
        })();
    }, [query.status, query.data, query.error, setAudioBookWithCharacterVoicesState]);
 
    return { audioBookWithCharacterVoicesState };
}