This commit is contained in:
Nouman Tahir 2023-01-17 18:01:12 +05:00
parent a60bbe3e23
commit 302cf2f430

View File

@ -61,83 +61,87 @@ export const usePostsCachePrimer = () => {
};
};
/** */
/**
* fetches and sectioned discussion to be used readily with sectioned flat list
* also injects local cache to data if any
* @param _author
* @param _permlink
* @returns raw query with sectionedData as extra parameter
*/
export const useDiscussionQuery = (_author?: string, _permlink?: string) => {
const currentAccount = useAppSelector((state) => state.account.currentAccount);
const cachedComments = useAppSelector((state)=> state.cache.comments);
const currentAccount = useAppSelector((state) => state.account.currentAccount);
const cachedComments = useAppSelector((state) => state.cache.comments);
const [author, setAuthor] = useState(_author);
const [permlink, setPermlink] = useState(_permlink);
const [author, setAuthor] = useState(_author);
const [permlink, setPermlink] = useState(_permlink);
const [sectionedData, setSectionedData] = useState([]);
const [sectionedData, setSectionedData] = useState([]);
const _injectCachedComments = (_comments) => {
//TODO: inject cached comments here
return _comments;
const _injectCachedComments = (_comments) => {
// TODO: inject cached comments here
return _comments;
};
// traverse discussion collection to curate sections
const parseSectionedData = async (commentsMap: any, author: string, permlink: string) => {
const MAX_THREAD_LEVEL = 3;
const comments: any = [];
if (!commentsMap) {
setSectionedData([]);
return;
}
// traverse discussion collection to curate sections
const parseSectionedData = async (commentsMap: any, author: string, permlink: string) => {
const MAX_THREAD_LEVEL = 3;
const comments:any = [];
if (!commentsMap) {
return null;
}
//set replies as data for a section, a single array with level indicating reply placement
const parseReplies = (commentsMap: any, replyKeys: any[], level: number, replies:any[] = []) => {
if (replyKeys && replyKeys.length > 0 && MAX_THREAD_LEVEL > level) {
replyKeys.forEach((pathKey) => {
const comment = commentsMap[pathKey];
if (comment) {
comment.level = level;
replies.push(comment);
replies = parseReplies(commentsMap, comment.replies, level + 1, replies);
return comment;
}
});
// set replies as data for a section, a single array with level indicating reply placement
const parseReplies = (
commentsMap: any,
replyKeys: any[],
level: number,
replies: any[] = [],
) => {
if (replyKeys && replyKeys.length > 0 && MAX_THREAD_LEVEL > level) {
replyKeys.forEach((pathKey) => {
const comment = commentsMap[pathKey];
if (comment) {
comment.level = level;
replies.push(comment);
replies = parseReplies(commentsMap, comment.replies, level + 1, replies);
return comment;
}
return replies;
};
for (const key in commentsMap) {
if (commentsMap.hasOwnProperty(key)) {
const comment = commentsMap[key];
// prcoess first level comment
if (comment && comment.parent_author === author && comment.parent_permlink === permlink) {
comment.data = parseReplies(commentsMap, comment.replies, 1);
comments.push(comment);
}
}
}
setSectionedData(comments);
};
});
}
return replies;
};
const _fetchComments = async () => {
console.log('fetching discussions')
const response = await getDiscussionCollection(author, permlink);
console.log("discussion response", response)
return response
for (const key in commentsMap) {
if (commentsMap.hasOwnProperty(key)) {
const comment = commentsMap[key];
// prcoess first level comment
if (comment && comment.parent_author === author && comment.parent_permlink === permlink) {
comment.data = parseReplies(commentsMap, comment.replies, 1);
comments.push(comment);
}
}
}
const query = useQuery(
[QUERIES.POST.GET_DISCUSSION, author, permlink],
_fetchComments
)
setSectionedData(comments);
};
const data = useMemo(() => _injectCachedComments(query.data), [query.data, cachedComments])
const _fetchComments = async () => {
console.log('fetching discussions');
const response = await getDiscussionCollection(author, permlink);
console.log('discussion response', response);
return response;
};
useEffect(()=>{
if(!data){
setSectionedData([]);
return;
}
const query = useQuery([QUERIES.POST.GET_DISCUSSION, author, permlink], _fetchComments);
parseSectionedData(data, author, permlink)
}, [data])
const data = useMemo(() => _injectCachedComments(query.data), [query.data, cachedComments]);
useEffect(() => {
parseSectionedData(data, author, permlink);
}, [data]);
return {
...query,
@ -146,9 +150,4 @@ export const useDiscussionQuery = (_author?: string, _permlink?: string) => {
setAuthor,
setPermlink,
};
}
};