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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import { AudioBook } from "@/schemas/AudioBook";
import {
CharacterVoice,
ElevenLabsTextToSpeechVoiceSettings,
ElevenLabsTextToSpeechVoiceSettingsDefaultValue,
} from "@/schemas/Character";
import { tRPC } from "@/utils/tRPC";
import { useEffect, useRef, useState } from "react";
import { produce } from "immer";
import debug from "debug";
import { useDebounce } from "./useDebounce";
const logger = debug("UseAudioBookForVoiceCastingPhase");
debug.enable("UseAudioBookForVoiceCastingPhase");
export type AudioBookPendingStateType = {
state: "pending";
};
export type AudioBookErrorStateType = { state: "error" };
export type AudioBookSuccessStateType = {
state: "success";
audioBook: AudioBook;
handleSelectVoice: (characterIndex: number, voice: CharacterVoice | null) => void;
handleVoiceSettingsChange: <
T extends keyof ElevenLabsTextToSpeechVoiceSettings,
V extends ElevenLabsTextToSpeechVoiceSettings[T],
>(
characterIndex: number,
key: T,
value: V,
) => void;
};
export type AudioBookStateType = AudioBookPendingStateType | AudioBookErrorStateType | AudioBookSuccessStateType;
export function useAudioBookForVoiceCastingPhase(projectID: string) {
const query = tRPC.project.get.useQuery(projectID);
const mutation = tRPC.project.update.useMutation();
const [audioBookState, setAudioBookState] = useState<AudioBookStateType>({ state: "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 !== "success") {
setAudioBookState({ state: "error" });
return;
}
const audioBook = query.data;
const handleSelectVoice = (characterIndex: number, voice: CharacterVoice | null) => {
const draftFn = (audioBook: AudioBook) => {
const character = audioBook.characters.at(characterIndex);
if (!character) {
return;
}
if (!voice) {
delete character.voice;
return;
}
character.voice = voice;
};
setAudioBookState((audioBookState) => {
if (audioBookState.state !== "success") {
return audioBookState;
}
return {
...audioBookState,
audioBook: produce(audioBookState.audioBook, draftFn),
};
});
setSyncStatus("IDLE");
};
const handleVoiceSettingsChange = <
T extends keyof ElevenLabsTextToSpeechVoiceSettings,
V extends ElevenLabsTextToSpeechVoiceSettings[T],
>(
characterIndex: number,
key: T,
value: V,
) => {
const draftFn = (audioBookPrevState: AudioBook) => {
const character = audioBookPrevState.characters.at(characterIndex);
if (!character) {
return;
}
if (!character.voice) {
return;
}
if (!character.voice.voiceSettings) {
character.voice.voiceSettings = {};
}
if (!character.voice.voiceSettings.textToSpeech) {
character.voice.voiceSettings.textToSpeech = structuredClone(
ElevenLabsTextToSpeechVoiceSettingsDefaultValue,
);
}
character.voice.voiceSettings.textToSpeech[key] = value;
};
setAudioBookState((audioBookPrevState) => {
if (audioBookPrevState.state !== "success") {
return audioBookPrevState;
}
return {
...audioBookPrevState,
audioBook: produce(audioBookPrevState.audioBook, draftFn),
};
});
setSyncStatus("IDLE");
};
logger("audioBook:", audioBook);
setAudioBookState({ state: "success", audioBook, handleSelectVoice, handleVoiceSettingsChange });
})();
}, [query.status, query.data, setAudioBookState]);
useEffect(() => {
if (!debouncedValue) {
return;
}
if (init.current) {
init.current = false;
return;
}
(async function () {
setSyncStatus("PENDING");
try {
await mutation.mutateAsync({ ...debouncedValue, id: projectID });
setSyncStatus("SUCCESS");
} catch {
setSyncStatus("ERROR");
}
})();
}, [debouncedValue, projectID, setSyncStatus]); // mutation.mutate generates a new mutation object
return { audioBookState, syncStatus };
}
|