UNPKG

@tycoonsystems/tycoon-modules

Version:
1 lines 4.18 kB
function _extends(){return(_extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var i,s=arguments[t];for(i in s)Object.prototype.hasOwnProperty.call(s,i)&&(e[i]=s[i])}return e}).apply(this,arguments)}import React from"react";import{v4 as uuidv4}from"uuid";import apiReq from"@tycoonsystems/tycoon-modules/utility/api/apiReq";import WatchPageStyles from"@tycoonsystems/tycoon-modules/streaming/watch/WatchPage.module.scss";import{ReadComment}from".";import{submitPost}from"@tycoonsystems/tycoon-modules/utility/utility/comment";const Module=c=>{const[e,i]=React.useState(!1),[,t]=React.useState(!1),[s,o]=React.useState(null),[l,r]=React.useState(!1),[,m]=React.useState(-1),[a,n]=React.useState(null),[d,p]=React.useState([]),[u,f]=React.useState(0),[h,g]=React.useState(null),[w,y]=React.useState(null),[R,v]=React.useState(!1),_=c?.useId??c?.commentUseParent,E=(c?.pipe&&(c._LocalEventEmitter.unsubscribe(c.pipe),c._LocalEventEmitter.subscribe(c.pipe,i=>{if(i)if("comment_published"===i.dispatch){if(i?.comment){var e,t,s,a=[...d];const o=i?.comment?.meta?.opId;o?-1<(e=d.findIndex(e=>e.id===o))&&(a[e].replies||(a[e].replies=[]),(t=[...a[e].replies]).push(i.comment),(s={...a[e]}).replies=t,a[e]=s):a.unshift(i.comment),i.comment?.meta?.opId&&c._LocalEventEmitter.dispatch(i.comment.meta.opId,{dispatch:"comment_published",comment:i.comment}),p(a)}}else if("refetch"===i.dispatch&&i?.index){var n=[...d];for(let t=0;t<n.length;t++){if(n?.[t]?.id===i.index){delete n[t].liked,delete n[t].disliked,i?.status&&(n[t][i.status+"d"]=!0);break}if(n[t]?.replies)for(let e=0;e<n[t].replies.length;e++)if(n[t].replies?.[e]?.reply_id===i.index){delete n[t].replies[e].liked,delete n[t].replies[e].disliked,i?.status&&(n[t].replies[e][i.status+"d"]=!0),c._LocalEventEmitter.dispatch(i.index,{dispatch:"comment_update",reply_id:i.index,replies:n[t].replies});break}}p(n)}})),c._LocalEventEmitter.unsubscribe(a),c._LocalEventEmitter.subscribe(a,e=>{if(e&&"scroll_window"===e.dispatch){var t,e=document.body,i=document.documentElement,s=e?.scrollHeight??0,e=e?.offsetHeight??0,a=i?.clientHeight??0,n=i?.scrollHeight??0,i=i?.offsetHeight??0,o=window?.scrollY+window?.innerHeight,s=Math.max(s,e,a,n,i);if(!l)try{s-(window?.innerHeight<450?window.innerHeight/2:600)<o&&(null===w||w?.scrollY>window?.scrollY+50||w?.scrollY<window?.scrollY-50)&&(y({scrollX:window?.scrollX,scrollY:window?.scrollY}),t=u+25,f(t),r(!0),E(_,t))}catch(e){}}}),React.useEffect(()=>{if(!e){const t=uuidv4();try{window.addEventListener("scroll_window",function(e){c._LocalEventEmitter.dispatch(t,{dispatch:"scroll_window"})})}catch(e){}y({scrollX:window?.scrollX,scrollY:window?.scrollY}),n(t),i(!0)}},[e]),React.useEffect(()=>{l||(_&&_!==s||!R&&c?._loggedIn?.identifier)&&(c?._loggedIn?.identifier&&!R&&v(!0),r(!0),t(!0),E(_,u))},[_,l,s,c?._loggedIn?.identifier,R]),async(e,t,i,s)=>{try{o(e);var a,n=await apiReq("/p/getposts",{apiUrl:c?.apiUrl,id:e,offset:t,par:i,parentOffset:s,orderDir:c?.orderDir??null,identifier:c?._loggedIn?.identifier,hash:c?._loggedIn?.hash});n?.data?(m((new Date).getTime()),r(!1),0<t&&!i&&!s?(a=[...d].concat(n.data),p(a)):p(n.data)):(r(!1),g({dispatch:"error",message:n?.message??"Failed to get posts"}))}catch(e){r(!1)}});var b=React.useCallback(e=>{g(null)});const C=React.useCallback((e,t,i,s,a,n)=>{console.log("Handle Post",e,t);var o=c?.commentUseParent??"",l=c?.commentUseParentType??"";submitPost(e,c,t,i,o,l,s,a,n)});var S=React.useMemo(()=>d.map((e,t)=>React.createElement("div",{key:t},React.createElement(ReadComment,_extends({},c,{commentData:e,i:t,handlePost:C,sub:e})))),[d]);return React.createElement("div",{className:c.className+" AddComment_Container"},d?.map?React.createElement("div",{className:`${WatchPageStyles.readCommentsListContainer} ${WatchPageStyles.readCommentsListContainerOp} Post_ReadCommentsListContainer`},S):null,React.createElement("div",{className:`spinner spinnerBig ${l?"opacity1":"opacity0 spinnerHide"} spinnerRelative`,style:{marginTop:l?"1rem":0}}),h?React.createElement("div",{className:"error CommentForceNoBlur",style:{margin:".25rem",marginBottom:"0"},onClick:b},h.message):null)};export default Module;