File size: 5,657 Bytes
cc276cc | 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 |
"use client";
import React, { createContext, useContext, useState, useCallback } from 'react';
import { ref, update, remove, set } from "firebase/database";
import { storageService } from '@/lib/storage-service';
import type { User, ReplyTo, PrivacySettings, DataMode, ChatRecipient } from '@/lib/types';
import { useAuth } from '@/contexts/auth-context';
import { useFirebase } from '@/contexts/firebase-context';
import { useSettings } from '@/contexts/settings-context';
import { Capacitor } from '@capacitor/core';
export const useChatUtilsCore = () => {
const { currentUser } = useAuth();
const { rtdb } = useFirebase();
const { addToast, t, dataMode, privacySettings } = useSettings();
const clearChatHistory = useCallback(async (chatId: string) => {
try {
await storageService.clearMessages(chatId);
addToast(t('historyCleared'));
window.dispatchEvent(new CustomEvent('localDataChange', { detail: { chatId } }));
} catch (error) {
console.error("Error clearing local chat history:", error);
addToast(t('historyClearError'), { variant: "destructive" });
}
}, [addToast, t]);
const deleteMessage = useCallback(async (chatId: string, messageId: string) => {
if (!currentUser) return;
try {
const messageRef = ref(rtdb, `chats/${chatId}/messages/${messageId}`);
const updatePayload = {
text: null,
encryptedText: null,
isDeleted: true,
audioKey: null,
imageKey: null,
videoKey: null,
replyTo: null,
transcription: null,
urlPreviewData: null,
compressedText: null,
reactions: null,
};
await update(messageRef, updatePayload);
await storageService.updateMessage(chatId, messageId, { ...updatePayload, isDeleted: true });
window.dispatchEvent(new CustomEvent('localDataChange', { detail: { chatId } }));
} catch (error) {
console.error("Error deleting message:", error);
addToast(t('deleteMessageError'), { variant: "destructive" });
}
}, [currentUser, addToast, rtdb, t]);
const setUserTyping = useCallback((chatId: string, isTyping: boolean) => {
if (!currentUser || dataMode === 'ultra' || !privacySettings.showTyping) return;
const typingRef = ref(rtdb, `typing/${chatId}/${currentUser.uid}`);
if (isTyping) {
set(typingRef, currentUser.displayName);
} else {
remove(typingRef);
}
}, [currentUser, dataMode, rtdb, privacySettings.showTyping]);
const uploadFile = useCallback(async (file: File, onProgress: (progress: number) => void) => {
const formData = new FormData();
formData.append('file', file);
return new Promise<{ fileKey: string }>((resolve, reject) => {
const xhr = new XMLHttpRequest();
const isNative = Capacitor.isNativePlatform();
const baseUrl = isNative ? process.env.NEXT_PUBLIC_API_BASE_URL : '';
const uploadUrl = `${baseUrl}/api/media`;
xhr.open('POST', uploadUrl, true);
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) {
const progress = (event.loaded / event.total) * 100;
onProgress(progress);
}
};
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const response = JSON.parse(xhr.responseText);
resolve({ fileKey: response.fileKey });
} catch (e) {
reject(new Error('Invalid JSON response from server.'));
}
} else {
reject(new Error(`Upload failed: ${xhr.statusText}`));
}
};
xhr.onerror = () => {
reject(new Error('Upload failed due to a network error.'));
};
xhr.send(formData);
});
}, []);
const transcribeAndSet = useCallback(async (messageId: string, audioFile: File, recipient: ChatRecipient) => {
if (!currentUser) return;
const chatId = recipient.isGroup
? recipient.uid
: (currentUser.uid < recipient.uid
? `private_'''${currentUser.uid}'''_'''${recipient.uid}'''`
: `private_'''${recipient.uid}'''_'''${currentUser.uid}'''`);
if (!chatId) return;
try {
// Disabled
} catch (error) {
console.warn("Could not transcribe audio:", error);
}
}, [currentUser, rtdb]);
return {
clearChatHistory,
deleteMessage,
setUserTyping,
uploadFile,
transcribeAndSet,
};
};
type ChatUtilsContextType = ReturnType<typeof useChatUtilsCore>;
const ChatUtilsContext = createContext<ChatUtilsContextType | null>(null);
export const useChatUtils = () => {
const context = useContext(ChatUtilsContext);
if (!context) {
throw new Error('useChatUtils must be used within a ChatUtilsProvider');
}
return context;
};
export const ChatUtilsProvider = ({ children }: { children: React.ReactNode }) => {
const chatUtilsData = useChatUtilsCore();
return (
<ChatUtilsContext.Provider value={chatUtilsData}>
{children}
</ChatUtilsContext.Provider>
);
};
|