import { Component, linkEvent } from "inferno"; import { CommunityResponse, FollowCommunity, GetSiteResponse, ListCommunities, ListCommunitiesResponse, ListingType, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { i18n } from "../../i18next"; import { InitialFetchRequest } from "../../interfaces"; import { WebSocketService } from "../../services"; import { QueryParams, getPageFromString, getQueryParams, getQueryString, isBrowser, myAuth, numToSI, setIsoData, showLocal, toast, wsClient, wsSubscribe, } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; import { ListingTypeSelect } from "../common/listing-type-select"; import { Paginator } from "../common/paginator"; import { CommunityLink } from "./community-link"; const communityLimit = 50; interface CommunitiesState { listCommunitiesResponse?: ListCommunitiesResponse; loading: boolean; siteRes: GetSiteResponse; searchText: string; } interface CommunitiesProps { listingType: ListingType; page: number; } function getCommunitiesQueryParams() { return getQueryParams({ listingType: getListingTypeFromQuery, page: getPageFromString, }); } function getListingTypeFromQuery(listingType?: string): ListingType { return listingType ? (listingType as ListingType) : "Local"; } function toggleSubscribe(community_id: number, follow: boolean) { const auth = myAuth(); if (auth) { const form: FollowCommunity = { community_id, follow, auth, }; WebSocketService.Instance.send(wsClient.followCommunity(form)); } } function refetch() { const { listingType, page } = getCommunitiesQueryParams(); const listCommunitiesForm: ListCommunities = { type_: listingType, sort: "TopMonth", limit: communityLimit, page, auth: myAuth(false), }; WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm)); } export class Communities extends Component { private subscription?: Subscription; private isoData = setIsoData(this.context); state: CommunitiesState = { loading: true, siteRes: this.isoData.site_res, searchText: "", }; constructor(props: any, context: any) { super(props, context); this.handlePageChange = this.handlePageChange.bind(this); this.handleListingTypeChange = this.handleListingTypeChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); // Only fetch the data if coming from another route if (this.isoData.path === this.context.router.route.match.url) { const listRes = this.isoData.routeData[0] as ListCommunitiesResponse; this.state = { ...this.state, listCommunitiesResponse: listRes, loading: false, }; } else { refetch(); } } componentWillUnmount() { if (isBrowser()) { this.subscription?.unsubscribe(); } } get documentTitle(): string { return `${i18n.t("communities")} - ${ this.state.siteRes.site_view.site.name }`; } render() { const { listingType, page } = getCommunitiesQueryParams(); return (
{this.state.loading ? (
) : (

{i18n.t("list_of_communities")}

{this.searchForm()}
{this.state.listCommunitiesResponse?.communities.map(cv => ( ))}
{i18n.t("name")} {i18n.t("subscribers")} {i18n.t("users")} / {i18n.t("month")} {i18n.t("posts")} {i18n.t("comments")}
{numToSI(cv.counts.subscribers)} {numToSI(cv.counts.users_active_month)} {numToSI(cv.counts.posts)} {numToSI(cv.counts.comments)} {cv.subscribed == "Subscribed" && ( )} {cv.subscribed === "NotSubscribed" && ( )} {cv.subscribed === "Pending" && (
{i18n.t("subscribe_pending")}
)}
)}
); } searchForm() { return (
); } updateUrl({ listingType, page }: Partial) { const { listingType: urlListingType, page: urlPage } = getCommunitiesQueryParams(); const queryParams: QueryParams = { listingType: listingType ?? urlListingType, page: (page ?? urlPage)?.toString(), }; this.props.history.push(`/communities${getQueryString(queryParams)}`); refetch(); } handlePageChange(page: number) { this.updateUrl({ page }); } handleListingTypeChange(val: ListingType) { this.updateUrl({ listingType: val, page: 1, }); } handleUnsubscribe(communityId: number) { toggleSubscribe(communityId, false); } handleSubscribe(communityId: number) { toggleSubscribe(communityId, true); } handleSearchChange(i: Communities, event: any) { i.setState({ searchText: event.target.value }); } handleSearchSubmit(i: Communities) { const searchParamEncoded = encodeURIComponent(i.state.searchText); i.context.router.history.push(`/search?q=${searchParamEncoded}`); } static fetchInitialData({ query: { listingType, page }, client, auth, }: InitialFetchRequest>): Promise[] { const listCommunitiesForm: ListCommunities = { type_: getListingTypeFromQuery(listingType), sort: "TopMonth", limit: communityLimit, page: getPageFromString(page), auth: auth, }; return [client.listCommunities(listCommunitiesForm)]; } parseMessage(msg: any) { const op = wsUserOp(msg); console.log(msg); if (msg.error) { toast(i18n.t(msg.error), "danger"); } else if (op === UserOperation.ListCommunities) { const data = wsJsonToRes(msg); this.setState({ listCommunitiesResponse: data, loading: false }); window.scrollTo(0, 0); } else if (op === UserOperation.FollowCommunity) { const { community_view: { community, subscribed, counts: { subscribers }, }, } = wsJsonToRes(msg); const res = this.state.listCommunitiesResponse; const found = res?.communities.find( ({ community: { id } }) => id == community.id ); if (found) { found.subscribed = subscribed; found.counts.subscribers = subscribers; this.setState(this.state); } } } }