@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
51 lines • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const hooks_1 = require("../../../store/hooks");
const chatSlice_1 = require("../../../store/slices/chatSlice");
const useProject_1 = __importDefault(require("../../projects/useProject"));
const handleError_1 = require("../../../utils/handleError");
const useFetchConversation_1 = __importDefault(require("./useFetchConversation"));
const useUpdateConversation_1 = __importDefault(require("./useUpdateConversation"));
const useDeleteConversation_1 = __importDefault(require("./useDeleteConversation"));
function useConversation({ conversationId, }) {
const dispatch = (0, hooks_1.useReplykeDispatch)();
const { projectId } = (0, useProject_1.default)();
const conversation = (0, hooks_1.useReplykeSelector)((0, chatSlice_1.selectConversation)(conversationId));
const loading = (0, hooks_1.useReplykeSelector)((0, chatSlice_1.selectConversationLoading)(conversationId));
const fetchConversation = (0, useFetchConversation_1.default)();
const _update = (0, useUpdateConversation_1.default)();
const _delete = (0, useDeleteConversation_1.default)();
// Fetch conversation on mount if not already loaded
(0, react_1.useEffect)(() => {
if (!projectId || !conversationId || conversation)
return;
const load = async () => {
dispatch((0, chatSlice_1.setConversationLoading)({ conversationId, loading: true }));
try {
const fetched = await fetchConversation({ conversationId });
dispatch((0, chatSlice_1.setConversation)(fetched));
}
catch (err) {
(0, handleError_1.handleError)(err, "Failed to fetch conversation");
dispatch((0, chatSlice_1.setConversationLoading)({ conversationId, loading: false }));
}
};
load();
}, [projectId, conversationId]); // eslint-disable-line react-hooks/exhaustive-deps
const update = (0, react_1.useCallback)(async (params) => {
const updated = await _update({ conversationId, ...params });
if (updated)
dispatch((0, chatSlice_1.setConversation)(updated));
return updated;
}, [_update, conversationId, dispatch]);
const deleteConversation = (0, react_1.useCallback)(async () => {
await _delete({ conversationId });
}, [_delete, conversationId]);
return { conversation, loading, update, deleteConversation };
}
exports.default = useConversation;
//# sourceMappingURL=useConversation.js.map