2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
CommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
FollowCommunity,
|
2022-06-21 21:42:29 +00:00
|
|
|
GetSiteResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
ListCommunities,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListCommunitiesResponse,
|
2021-01-26 15:59:38 +00:00
|
|
|
ListingType,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { InitialFetchRequest } from "shared/interfaces";
|
|
|
|
import { i18n } from "../../i18next";
|
2021-07-19 13:04:06 +00:00
|
|
|
import { WebSocketService } from "../../services";
|
2020-09-07 03:41:46 +00:00
|
|
|
import {
|
2023-05-11 17:06:32 +00:00
|
|
|
QueryParams,
|
2023-04-15 14:47:10 +00:00
|
|
|
getPageFromString,
|
|
|
|
getQueryParams,
|
|
|
|
getQueryString,
|
2020-09-07 03:41:46 +00:00
|
|
|
isBrowser,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2021-09-18 16:35:49 +00:00
|
|
|
numToSI,
|
2020-09-07 15:32:07 +00:00
|
|
|
setIsoData,
|
2021-07-18 16:17:50 +00:00
|
|
|
showLocal,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
|
|
|
wsClient,
|
2020-09-07 15:32:07 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2021-07-18 16:17:50 +00:00
|
|
|
import { ListingTypeSelect } from "../common/listing-type-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Paginator } from "../common/paginator";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { CommunityLink } from "./community-link";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-05-15 19:53:29 +00:00
|
|
|
const communityLimit = 50;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CommunitiesState {
|
2023-01-04 16:56:24 +00:00
|
|
|
listCommunitiesResponse?: ListCommunitiesResponse;
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: GetSiteResponse;
|
2021-01-23 23:07:02 +00:00
|
|
|
searchText: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunitiesProps {
|
2023-04-15 14:47:10 +00:00
|
|
|
listingType: ListingType;
|
2023-05-15 19:53:29 +00:00
|
|
|
page: number;
|
2023-04-15 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCommunitiesQueryParams() {
|
|
|
|
return getQueryParams<CommunitiesProps>({
|
|
|
|
listingType: getListingTypeFromQuery,
|
|
|
|
page: getPageFromString,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getListingTypeFromQuery(listingType?: string): ListingType {
|
2023-05-11 18:32:32 +00:00
|
|
|
return listingType ? (listingType as ListingType) : "Local";
|
2023-04-15 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-05-11 18:32:32 +00:00
|
|
|
sort: "TopMonth",
|
2023-04-15 14:47:10 +00:00
|
|
|
limit: communityLimit,
|
|
|
|
page,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Communities extends Component<any, CommunitiesState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
private subscription?: Subscription;
|
|
|
|
private isoData = setIsoData(this.context);
|
|
|
|
state: CommunitiesState = {
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: true,
|
2022-06-21 21:42:29 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2021-02-22 02:39:04 +00:00
|
|
|
searchText: "",
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
2021-07-16 19:40:56 +00:00
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
2021-07-18 16:17:50 +00:00
|
|
|
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
2020-09-07 15:32:07 +00:00
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2023-04-15 14:47:10 +00:00
|
|
|
if (this.isoData.path === this.context.router.route.match.url) {
|
|
|
|
const listRes = this.isoData.routeData[0] as ListCommunitiesResponse;
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
listCommunitiesResponse: listRes,
|
|
|
|
loading: false,
|
|
|
|
};
|
2020-09-07 03:41:46 +00:00
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
refetch();
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-07 03:41:46 +00:00
|
|
|
if (isBrowser()) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("communities")} - ${
|
|
|
|
this.state.siteRes.site_view.site.name
|
|
|
|
}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { listingType, page } = getCommunitiesQueryParams();
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="row">
|
|
|
|
<div className="col-md-6">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h4>{i18n.t("list_of_communities")}</h4>
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="mb-2">
|
2021-07-18 16:17:50 +00:00
|
|
|
<ListingTypeSelect
|
2023-04-15 14:47:10 +00:00
|
|
|
type_={listingType}
|
2021-07-18 16:17:50 +00:00
|
|
|
showLocal={showLocal(this.isoData)}
|
2022-05-23 19:19:14 +00:00
|
|
|
showSubscribed
|
2021-07-18 16:17:50 +00:00
|
|
|
onChange={this.handleListingTypeChange}
|
|
|
|
/>
|
|
|
|
</span>
|
2021-01-23 23:07:02 +00:00
|
|
|
</div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-md-6">
|
|
|
|
<div className="float-md-right">{this.searchForm()}</div>
|
2021-01-23 23:07:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="table-responsive">
|
|
|
|
<table
|
|
|
|
id="community_table"
|
|
|
|
className="table table-sm table-hover"
|
|
|
|
>
|
|
|
|
<thead className="pointer">
|
2020-09-06 16:15:25 +00:00
|
|
|
<tr>
|
2021-02-22 02:39:04 +00:00
|
|
|
<th>{i18n.t("name")}</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right">{i18n.t("subscribers")}</th>
|
|
|
|
<th className="text-right">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("users")} / {i18n.t("month")}
|
2021-01-29 15:10:37 +00:00
|
|
|
</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("posts")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
2022-09-22 15:03:35 +00:00
|
|
|
<th className="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("comments")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2023-01-04 16:56:24 +00:00
|
|
|
{this.state.listCommunitiesResponse?.communities.map(cv => (
|
|
|
|
<tr key={cv.community.id}>
|
|
|
|
<td>
|
|
|
|
<CommunityLink community={cv.community} />
|
|
|
|
</td>
|
|
|
|
<td className="text-right">
|
|
|
|
{numToSI(cv.counts.subscribers)}
|
|
|
|
</td>
|
|
|
|
<td className="text-right">
|
|
|
|
{numToSI(cv.counts.users_active_month)}
|
|
|
|
</td>
|
|
|
|
<td className="text-right d-none d-lg-table-cell">
|
|
|
|
{numToSI(cv.counts.posts)}
|
|
|
|
</td>
|
|
|
|
<td className="text-right d-none d-lg-table-cell">
|
|
|
|
{numToSI(cv.counts.comments)}
|
|
|
|
</td>
|
|
|
|
<td className="text-right">
|
2023-05-11 18:32:32 +00:00
|
|
|
{cv.subscribed == "Subscribed" && (
|
2023-01-04 16:56:24 +00:00
|
|
|
<button
|
|
|
|
className="btn btn-link d-inline-block"
|
|
|
|
onClick={linkEvent(
|
|
|
|
cv.community.id,
|
|
|
|
this.handleUnsubscribe
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("unsubscribe")}
|
|
|
|
</button>
|
|
|
|
)}
|
2023-05-11 18:32:32 +00:00
|
|
|
{cv.subscribed === "NotSubscribed" && (
|
2023-01-04 16:56:24 +00:00
|
|
|
<button
|
|
|
|
className="btn btn-link d-inline-block"
|
|
|
|
onClick={linkEvent(
|
|
|
|
cv.community.id,
|
|
|
|
this.handleSubscribe
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("subscribe")}
|
|
|
|
</button>
|
|
|
|
)}
|
2023-05-11 18:32:32 +00:00
|
|
|
{cv.subscribed === "Pending" && (
|
2023-01-04 16:56:24 +00:00
|
|
|
<div className="text-warning d-inline-block">
|
|
|
|
{i18n.t("subscribe_pending")}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
2020-09-06 16:15:25 +00:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2023-04-15 14:47:10 +00:00
|
|
|
<Paginator page={page} onChange={this.handlePageChange} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
searchForm() {
|
|
|
|
return (
|
|
|
|
<form
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-inline"
|
2021-01-23 23:07:02 +00:00
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
2021-02-09 16:21:24 +00:00
|
|
|
id="communities-search"
|
2022-09-22 15:03:35 +00:00
|
|
|
className="form-control mr-2 mb-2"
|
2021-01-23 23:07:02 +00:00
|
|
|
value={this.state.searchText}
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={`${i18n.t("search")}...`}
|
2021-01-23 23:07:02 +00:00
|
|
|
onInput={linkEvent(this, this.handleSearchChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2022-09-22 15:03:35 +00:00
|
|
|
<label className="sr-only" htmlFor="communities-search">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("search")}
|
2021-02-09 16:21:24 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<button type="submit" className="btn btn-secondary mr-2 mb-2">
|
2021-02-22 02:39:04 +00:00
|
|
|
<span>{i18n.t("search")}</span>
|
2021-01-23 23:07:02 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
|
|
|
|
const { listingType: urlListingType, page: urlPage } =
|
|
|
|
getCommunitiesQueryParams();
|
|
|
|
|
|
|
|
const queryParams: QueryParams<CommunitiesProps> = {
|
|
|
|
listingType: listingType ?? urlListingType,
|
|
|
|
page: (page ?? urlPage)?.toString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.props.history.push(`/communities${getQueryString(queryParams)}`);
|
|
|
|
|
|
|
|
refetch();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 19:53:29 +00:00
|
|
|
handlePageChange(page: number) {
|
2021-07-16 19:40:56 +00:00
|
|
|
this.updateUrl({ page });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:17:50 +00:00
|
|
|
handleListingTypeChange(val: ListingType) {
|
|
|
|
this.updateUrl({
|
|
|
|
listingType: val,
|
2023-05-15 19:53:29 +00:00
|
|
|
page: 1,
|
2021-07-18 16:17:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleUnsubscribe(communityId: number) {
|
2023-04-15 14:47:10 +00:00
|
|
|
toggleSubscribe(communityId, false);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubscribe(communityId: number) {
|
2023-04-15 14:47:10 +00:00
|
|
|
toggleSubscribe(communityId, true);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
handleSearchChange(i: Communities, event: any) {
|
|
|
|
i.setState({ searchText: event.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchSubmit(i: Communities) {
|
|
|
|
const searchParamEncoded = encodeURIComponent(i.state.searchText);
|
2023-04-15 14:47:10 +00:00
|
|
|
i.context.router.history.push(`/search?q=${searchParamEncoded}`);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
static fetchInitialData({
|
|
|
|
query: { listingType, page },
|
|
|
|
client,
|
|
|
|
auth,
|
|
|
|
}: InitialFetchRequest<QueryParams<CommunitiesProps>>): Promise<any>[] {
|
|
|
|
const listCommunitiesForm: ListCommunities = {
|
|
|
|
type_: getListingTypeFromQuery(listingType),
|
2023-05-11 18:32:32 +00:00
|
|
|
sort: "TopMonth",
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: communityLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
page: getPageFromString(page),
|
|
|
|
auth: auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
return [client.listCommunities(listCommunitiesForm)];
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2023-04-15 14:47:10 +00:00
|
|
|
} else if (op === UserOperation.ListCommunities) {
|
|
|
|
const data = wsJsonToRes<ListCommunitiesResponse>(msg);
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState({ listCommunitiesResponse: data, loading: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
window.scrollTo(0, 0);
|
2023-04-15 14:47:10 +00:00
|
|
|
} else if (op === UserOperation.FollowCommunity) {
|
|
|
|
const {
|
|
|
|
community_view: {
|
|
|
|
community,
|
|
|
|
subscribed,
|
|
|
|
counts: { subscribers },
|
|
|
|
},
|
|
|
|
} = wsJsonToRes<CommunityResponse>(msg);
|
|
|
|
const res = this.state.listCommunitiesResponse;
|
|
|
|
const found = res?.communities.find(
|
|
|
|
({ community: { id } }) => id == community.id
|
2023-01-04 16:56:24 +00:00
|
|
|
);
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (found) {
|
2023-04-15 14:47:10 +00:00
|
|
|
found.subscribed = subscribed;
|
|
|
|
found.counts.subscribers = subscribers;
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|