feat: favorite message
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useSelector } from "react-redux";
|
||||
import {
|
||||
useLazyRemoveFavoriteQuery,
|
||||
useFavoriteMessageMutation,
|
||||
} from "../../app/services/message";
|
||||
export default function useFavMessage(cid = null) {
|
||||
const [removeFav] = useLazyRemoveFavoriteQuery();
|
||||
const [addFav] = useFavoriteMessageMutation();
|
||||
const [favorites, setFavorites] = useState([]);
|
||||
const { favs = [] } = useSelector((store) => {
|
||||
return {
|
||||
// mids: store.channelMessage.byId[cid],
|
||||
favs: store.favorites,
|
||||
// loginUser: store.contacts.byId[store.authData.uid],
|
||||
};
|
||||
});
|
||||
const addFavorite = async (mid = []) => {
|
||||
const mids = Array.isArray(mid) ? mid.map((i) => +i) : [+mid];
|
||||
if (mids.length == 0) return;
|
||||
await addFav(mids);
|
||||
};
|
||||
const removeFavorite = (id) => {
|
||||
if (!id) return;
|
||||
removeFav(id);
|
||||
};
|
||||
const isFavorited = (mid = null) => {
|
||||
if (!mid) return false;
|
||||
let mids = [];
|
||||
favorites.forEach((f) => {
|
||||
if (f.messages.length == 1) {
|
||||
const ids = f.messages.map((m) => m.from_mid);
|
||||
mids = [...mids, ...ids];
|
||||
}
|
||||
});
|
||||
return mids.findIndex((i) => i == mid) > -1;
|
||||
};
|
||||
useEffect(() => {
|
||||
const filtereds = cid
|
||||
? favs.filter((f) => {
|
||||
if (!f.messages) return false;
|
||||
return f.messages.every((m) => m.source.gid == cid);
|
||||
})
|
||||
: favs;
|
||||
console.log("filtered", filtereds);
|
||||
setFavorites(filtereds);
|
||||
}, [cid, favs]);
|
||||
|
||||
return {
|
||||
isFavorited,
|
||||
addFavorite,
|
||||
removeFavorite,
|
||||
favorites,
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import BASE_URL, { ContentTypes } from "../../app/config";
|
||||
import { normalizeArchiveData } from "../../common/utils";
|
||||
import { useLazyGetArchiveMessageQuery } from "../../app/services/message";
|
||||
export default function useNormalizeMessage() {
|
||||
const [filePath, setFilePath] = useState(null);
|
||||
@@ -10,44 +10,7 @@ export default function useNormalizeMessage() {
|
||||
] = useLazyGetArchiveMessageQuery();
|
||||
useEffect(() => {
|
||||
if (data && isSuccess) {
|
||||
const msgs = data.messages.map(
|
||||
({
|
||||
content,
|
||||
file_id,
|
||||
thumbnail_id,
|
||||
content_type,
|
||||
properties,
|
||||
from_user,
|
||||
}) => {
|
||||
const transformedContent =
|
||||
content_type == ContentTypes.file
|
||||
? `${BASE_URL}/resource/archive/attachment?file_path=${filePath}&attachment_id=${file_id}`
|
||||
: content;
|
||||
const thumbnail =
|
||||
content_type == ContentTypes.file
|
||||
? `${BASE_URL}/resource/archive/attachment?file_path=${filePath}&attachment_id=${thumbnail_id}`
|
||||
: "";
|
||||
const download =
|
||||
content_type == ContentTypes.file
|
||||
? `${BASE_URL}/resource/archive/attachment?file_path=${filePath}&attachment_id=${file_id}&download=true`
|
||||
: "";
|
||||
let user = { ...(data.users[from_user] || {}) };
|
||||
user.avatar =
|
||||
user.avatar !== null
|
||||
? `${BASE_URL}/resource/archive/attachment?file_path=${filePath}&attachment_id=${user.avatar}`
|
||||
: "";
|
||||
|
||||
console.log("user data", transformedContent, user);
|
||||
return {
|
||||
user,
|
||||
content: transformedContent,
|
||||
content_type,
|
||||
properties,
|
||||
download,
|
||||
thumbnail,
|
||||
};
|
||||
}
|
||||
);
|
||||
const msgs = normalizeArchiveData(data, filePath);
|
||||
setNormalizedMessages(msgs);
|
||||
}
|
||||
}, [data, isSuccess, filePath]);
|
||||
|
||||
Reference in New Issue
Block a user