All checks were successful
CI / test (push) Successful in 27s
- add media preview and upload confirmation for image/video - add upload progress tracking for presigned uploads - keep voice recording/upload flow with better UI states - include related realtime/chat updates currently in working tree
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import { http } from "./http";
|
|
import type { Chat, ChatType, Message, MessageType } from "../chat/types";
|
|
import axios from "axios";
|
|
|
|
export async function getChats(): Promise<Chat[]> {
|
|
const { data } = await http.get<Chat[]>("/chats");
|
|
return data;
|
|
}
|
|
|
|
export async function createPrivateChat(memberId: number): Promise<Chat> {
|
|
return createChat("private", null, [memberId]);
|
|
}
|
|
|
|
export async function createChat(type: ChatType, title: string | null, memberIds: number[] = []): Promise<Chat> {
|
|
const { data } = await http.post<Chat>("/chats", {
|
|
type,
|
|
title,
|
|
member_ids: memberIds
|
|
});
|
|
return data;
|
|
}
|
|
|
|
export async function getMessages(chatId: number, beforeId?: number): Promise<Message[]> {
|
|
const { data } = await http.get<Message[]>(`/messages/${chatId}`, {
|
|
params: {
|
|
limit: 50,
|
|
before_id: beforeId
|
|
}
|
|
});
|
|
return data;
|
|
}
|
|
|
|
export async function sendMessage(chatId: number, text: string, type: MessageType = "text"): Promise<Message> {
|
|
const { data } = await http.post<Message>("/messages", { chat_id: chatId, text, type });
|
|
return data;
|
|
}
|
|
|
|
export interface UploadUrlResponse {
|
|
upload_url: string;
|
|
file_url: string;
|
|
object_key: string;
|
|
expires_in: number;
|
|
required_headers: Record<string, string>;
|
|
}
|
|
|
|
export async function requestUploadUrl(file: File): Promise<UploadUrlResponse> {
|
|
const { data } = await http.post<UploadUrlResponse>("/media/upload-url", {
|
|
file_name: file.name,
|
|
file_type: file.type || "application/octet-stream",
|
|
file_size: file.size
|
|
});
|
|
return data;
|
|
}
|
|
|
|
export async function uploadToPresignedUrl(
|
|
uploadUrl: string,
|
|
requiredHeaders: Record<string, string>,
|
|
file: File,
|
|
onProgress?: (percent: number) => void
|
|
): Promise<void> {
|
|
await axios.put(uploadUrl, file, {
|
|
headers: requiredHeaders,
|
|
onUploadProgress: (progressEvent) => {
|
|
if (!onProgress || !progressEvent.total) {
|
|
return;
|
|
}
|
|
onProgress(Math.round((progressEvent.loaded * 100) / progressEvent.total));
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function attachFile(messageId: number, fileUrl: string, fileType: string, fileSize: number): Promise<void> {
|
|
await http.post("/media/attachments", {
|
|
message_id: messageId,
|
|
file_url: fileUrl,
|
|
file_type: fileType,
|
|
file_size: fileSize
|
|
});
|
|
}
|