mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
Remove domain segment
This commit is contained in:
74
www/app/(app)/transcripts/useParticipants.ts
Normal file
74
www/app/(app)/transcripts/useParticipants.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Participant } from "../../api";
|
||||
import { useError } from "../../(errors)/errorContext";
|
||||
import useApi from "../../lib/useApi";
|
||||
import { shouldShowError } from "../../lib/errorUtils";
|
||||
|
||||
type ErrorParticipants = {
|
||||
error: Error;
|
||||
loading: false;
|
||||
response: null;
|
||||
};
|
||||
|
||||
type LoadingParticipants = {
|
||||
response: Participant[] | null;
|
||||
loading: true;
|
||||
error: null;
|
||||
};
|
||||
|
||||
type SuccessParticipants = {
|
||||
response: Participant[];
|
||||
loading: boolean;
|
||||
error: null;
|
||||
};
|
||||
|
||||
export type UseParticipants = (
|
||||
| ErrorParticipants
|
||||
| LoadingParticipants
|
||||
| SuccessParticipants
|
||||
) & { refetch: () => void };
|
||||
|
||||
const useParticipants = (transcriptId: string): UseParticipants => {
|
||||
const [response, setResponse] = useState<Participant[] | null>(null);
|
||||
const [loading, setLoading] = useState<boolean>(true);
|
||||
const [error, setErrorState] = useState<Error | null>(null);
|
||||
const { setError } = useError();
|
||||
const api = useApi();
|
||||
const [count, setCount] = useState(0);
|
||||
|
||||
const refetch = () => {
|
||||
if (!loading) {
|
||||
setCount(count + 1);
|
||||
setLoading(true);
|
||||
setErrorState(null);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!transcriptId || !api) return;
|
||||
|
||||
setLoading(true);
|
||||
api
|
||||
.v1TranscriptGetParticipants({ transcriptId })
|
||||
.then((result) => {
|
||||
setResponse(result);
|
||||
setLoading(false);
|
||||
console.debug("Participants Loaded:", result);
|
||||
})
|
||||
.catch((error) => {
|
||||
const shouldShowHuman = shouldShowError(error);
|
||||
if (shouldShowHuman) {
|
||||
setError(error, "There was an error loading the participants");
|
||||
} else {
|
||||
setError(error);
|
||||
}
|
||||
setErrorState(error);
|
||||
setResponse(null);
|
||||
setLoading(false);
|
||||
});
|
||||
}, [transcriptId, !api, count]);
|
||||
|
||||
return { response, loading, error, refetch } as UseParticipants;
|
||||
};
|
||||
|
||||
export default useParticipants;
|
||||
Reference in New Issue
Block a user