import { h, Component } from '/js/web_modules/preact.js'; import htm from '/js/web_modules/htm.js'; import { URL_FOLLOWERS } from '/js/utils/constants.js'; const html = htm.bind(h); import { paginateArray } from '../../utils/helpers.js'; export default class FollowerList extends Component { constructor(props) { super(props); this.state = { followers: [], followersPage: 0, }; } componentDidMount() { try { this.getFollowers(); } catch (e) { console.error('followers error: ', e); } } async getFollowers() { const response = await fetch(URL_FOLLOWERS); const followers = await response.json(); this.setState({ followers: followers, }); } changeFollowersPage(page) { this.setState({ followersPage: page }); } render() { const FOLLOWER_PAGE_SIZE = 15; const { followersPage } = this.state; const { followers } = this.state; if (!followers) { return null; } const noFollowersInfo = html`

Be the first to follow this live stream.

By following this stream you'll get updates when it goes live, receive posts from the streamer, and be featured here as a follower.

Learn more about ${' '} The Fediverse, where you can follow this server as well as so much more.

`; const paginatedFollowers = paginateArray( followers, followersPage + 1, FOLLOWER_PAGE_SIZE ); const paginationControls = paginatedFollowers.totalPages > 1 && Array(paginatedFollowers.totalPages) .fill() .map((x, n) => { const activePageClass = n === followersPage && 'bg-indigo-600 rounded-full shadow-md focus:shadow-md text-white'; return html`
  • this.changeFollowersPage(n)} > ${n + 1}
  • `; }); return html`
    ${followers.length === 0 && noFollowersInfo} ${paginatedFollowers.items.map((follower) => { return html` <${SingleFollower} user=${follower} /> `; })}
    `; } } function SingleFollower(props) { const { user } = props; const { name, username, link, image } = user; var displayName = name; var displayUsername = username; if (!displayName) { displayName = displayUsername.split('@', 1)[0]; } return html` { currentTarget.onerror = null; currentTarget.src = '/img/logo.svg'; }} />

    ${displayName}

    ${displayUsername}

    `; }