Files
reflector/www/app/lib/api-hooks.ts
Mathieu Virbel 68c161ee7e fix: resolve import errors and add missing api hooks
- Create constants.ts for RECORD_A_MEETING_URL
- Add api-types.ts for backward compatible type exports
- Update all imports from deleted api folder to new locations
- Add missing React Query hooks for rooms and zulip operations
- Create useApi compatibility layer for unmigrated components
2025-08-29 09:36:55 -06:00

196 lines
4.4 KiB
TypeScript

"use client";
import { $api } from "./apiClient";
import { useError } from "../(errors)/errorContext";
import { useQueryClient } from "@tanstack/react-query";
import type { paths } from "../reflector-api";
// Rooms hooks
export function useRoomsList(page: number = 1) {
const { setError } = useError();
return $api.useQuery(
"get",
"/v1/rooms",
{
params: {
query: { page },
},
},
{
onError: (error) => {
setError(error as Error, "There was an error fetching the rooms");
},
},
);
}
// Transcripts hooks
export function useTranscriptsSearch(
q: string = "",
options: {
limit?: number;
offset?: number;
room_id?: string;
source_kind?: string;
} = {},
) {
const { setError } = useError();
return $api.useQuery(
"get",
"/v1/transcripts/search",
{
params: {
query: {
q,
limit: options.limit,
offset: options.offset,
room_id: options.room_id,
source_kind: options.source_kind as any,
},
},
},
{
onError: (error) => {
setError(error as Error, "There was an error searching transcripts");
},
keepPreviousData: true, // For smooth pagination
},
);
}
export function useTranscriptDelete() {
const { setError } = useError();
const queryClient = useQueryClient();
return $api.useMutation("delete", "/v1/transcripts/{transcript_id}", {
onSuccess: () => {
// Invalidate transcripts queries to refetch
queryClient.invalidateQueries({
queryKey: $api.queryOptions("get", "/v1/transcripts/search").queryKey,
});
},
onError: (error) => {
setError(error as Error, "There was an error deleting the transcript");
},
});
}
export function useTranscriptProcess() {
const { setError } = useError();
return $api.useMutation("post", "/v1/transcripts/{transcript_id}/process", {
onError: (error) => {
setError(error as Error, "There was an error processing the transcript");
},
});
}
export function useTranscriptGet(transcriptId: string | null) {
const { setError } = useError();
return $api.useQuery(
"get",
"/v1/transcripts/{transcript_id}",
{
params: {
path: {
transcript_id: transcriptId || "",
},
},
},
{
enabled: !!transcriptId,
onError: (error) => {
setError(error as Error, "There was an error loading the transcript");
},
},
);
}
// Rooms mutations
export function useRoomCreate() {
const { setError } = useError();
const queryClient = useQueryClient();
return $api.useMutation("post", "/v1/rooms", {
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: $api.queryOptions("get", "/v1/rooms").queryKey,
});
},
onError: (error) => {
setError(error as Error, "There was an error creating the room");
},
});
}
export function useRoomUpdate() {
const { setError } = useError();
const queryClient = useQueryClient();
return $api.useMutation("patch", "/v1/rooms/{room_id}", {
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: $api.queryOptions("get", "/v1/rooms").queryKey,
});
},
onError: (error) => {
setError(error as Error, "There was an error updating the room");
},
});
}
export function useRoomDelete() {
const { setError } = useError();
const queryClient = useQueryClient();
return $api.useMutation("delete", "/v1/rooms/{room_id}", {
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: $api.queryOptions("get", "/v1/rooms").queryKey,
});
},
onError: (error) => {
setError(error as Error, "There was an error deleting the room");
},
});
}
// Zulip hooks
export function useZulipStreams() {
const { setError } = useError();
return $api.useQuery(
"get",
"/v1/zulip/get-streams",
{},
{
onError: (error) => {
setError(error as Error, "There was an error fetching Zulip streams");
},
},
);
}
export function useZulipTopics(streamId: number | null) {
const { setError } = useError();
return $api.useQuery(
"get",
"/v1/zulip/get-topics",
{
params: {
query: { stream_id: streamId || 0 },
},
},
{
enabled: !!streamId,
onError: (error) => {
setError(error as Error, "There was an error fetching Zulip topics");
},
},
);
}