const toBeFollowed = props.accountId ?? "sking.near"; if (!toBeFollowed || !context.accountId || context.accountId === toBeFollowed) { return ""; } State.init({ loading: false, }); const followEdge = Social.keys( `${context.accountId}/graph/follow/${toBeFollowed}`, undefined, { values_only: true, }, ); const inverseEdge = Social.keys( `${toBeFollowed}/graph/follow/${context.accountId}`, undefined, { values_only: true, }, ); const loading = followEdge === null || inverseEdge === null; const isFollowing = Object.keys(followEdge || {}).length > 0; const isInverse = Object.keys(inverseEdge || {}).length > 0; const type = follow ? "unfollow" : "follow"; const data = { graph: { follow: { [toBeFollowed]: isFollowing ? null : "" } }, index: { graph: JSON.stringify({ key: "follow", value: { type, accountId: toBeFollowed, }, }), notify: JSON.stringify({ key: toBeFollowed, value: { type, }, }), }, }; return ( <Widget src="nearui.near/widget/Input.Button" props={{ children: ( <> {isFollowing && <i className="bi bi-check-lg"></i>} {isFollowing ? "Following" : isInverse ? "Follow Back" : "Follow"} </> ), variant: props.variant ?? (isFollowing ? [] : ["secondary"]), disabled: props.disabled ?? loading, onClick: () => { State.update({ loading: true }); Social.set(data, { force: true, onCommit: () => { State.update({ loading: false }); }, onCancel: () => { State.update({ loading: false }); }, }); }, ...props, }} /> );