const item = props.item; if (!item) { return ""; } const likes = Social.index("like", item); const dataLoading = likes === null; const likesByUsers = {}; (likes || []).forEach((like) => { if (like.value.type === "like") { likesByUsers[like.accountId] = like; } else if (like.value.type === "unlike") { delete likesByUsers[like.accountId]; } }); if (state.hasLike === true) { likesByUsers[context.accountId] = { accountId: context.accountId, }; } else if (state.hasLike === false) { delete likesByUsers[context.accountId]; } const accountsWithLikes = Object.keys(likesByUsers); const totalLikes = accountsWithLikes.length; const hasLike = context.accountId && !!likesByUsers[context.accountId]; const LikeButton = styled.button` border: 0; display: inline-flex; align-items: center; gap: 6px; color: #687076; font-weight: 400; font-size: 14px; line-height: 17px; cursor: pointer; background: none; i { font-size: 16px; transition: background-color 200ms; &.bi-heart-fill { color: #E5484D !important; } } &:hover, &:focus { outline: none; color: #11181C; } `; const likeClick = () => { if (state.loading) { return; } State.update({ loading: true, }); const data = { index: { like: JSON.stringify({ key: item, value: { type: hasLike ? "unlike" : "like", }, }), }, }; if (!hasLike && props.notifyAccountId) { data.index.notify = JSON.stringify({ key: props.notifyAccountId, value: { type: "like", item, }, }); } Social.set(data, { onCommit: () => State.update({ loading: false, hasLike: !hasLike }), onCancel: () => State.update({ loading: false }), }); }; const title = hasLike ? "Unlike" : "Like"; return ( <LikeButton disabled={state.loading || dataLoading || !context.accountId} title={title} onClick={likeClick} > <i className={`${hasLike ? "bi-heart-fill" : "bi-heart"}`} /> {totalLikes} </LikeButton> );