+import { setIsoData } from "@utils/app";
+import { RouteDataResponse } from "@utils/types";
import { Component } from "inferno";
import {
GetFederatedInstancesResponse,
GetSiteResponse,
Instance,
- UserOperation,
- wsJsonToRes,
- wsUserOp,
} from "lemmy-js-client";
-import { Subscription } from "rxjs";
-import { i18n } from "../../i18next";
+import { relTags } from "../../config";
import { InitialFetchRequest } from "../../interfaces";
-import { WebSocketService } from "../../services";
-import {
- isBrowser,
- relTags,
- setIsoData,
- toast,
- wsClient,
- wsSubscribe,
-} from "../../utils";
+import { FirstLoadService, I18NextService } from "../../services";
+import { HttpService, RequestState } from "../../services/HttpService";
import { HtmlTags } from "../common/html-tags";
+import { Spinner } from "../common/icon";
+
+type InstancesData = RouteDataResponse<{
+ federatedInstancesResponse: GetFederatedInstancesResponse;
+}>;
interface InstancesState {
+ instancesRes: RequestState<GetFederatedInstancesResponse>;
siteRes: GetSiteResponse;
- instancesRes?: GetFederatedInstancesResponse;
- loading: boolean;
+ isIsomorphic: boolean;
}
export class Instances extends Component<any, InstancesState> {
- private isoData = setIsoData(this.context);
+ private isoData = setIsoData<InstancesData>(this.context);
state: InstancesState = {
+ instancesRes: { state: "empty" },
siteRes: this.isoData.site_res,
- loading: true,
+ isIsomorphic: false,
};
- private subscription?: Subscription;
constructor(props: any, context: any) {
super(props, context);
- 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) {
+ if (FirstLoadService.isFirstLoad) {
this.state = {
...this.state,
- instancesRes: this.isoData
- .routeData[0] as GetFederatedInstancesResponse,
- loading: false,
+ instancesRes: this.isoData.routeData.federatedInstancesResponse,
+ isIsomorphic: true,
};
- } else {
- WebSocketService.Instance.send(wsClient.getFederatedInstances({}));
}
}
- static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
- const promises: Promise<any>[] = [];
+ async componentDidMount() {
+ if (!this.state.isIsomorphic) {
+ await this.fetchInstances();
+ }
+ }
+
+ async fetchInstances() {
+ this.setState({
+ instancesRes: { state: "loading" },
+ });
- promises.push(req.client.getFederatedInstances({}));
+ this.setState({
+ instancesRes: await HttpService.client.getFederatedInstances({}),
+ });
+ }
- return promises;
+ static async fetchInitialData({
+ client,
+ }: InitialFetchRequest): Promise<InstancesData> {
+ return {
+ federatedInstancesResponse: await client.getFederatedInstances({}),
+ };
}
get documentTitle(): string {
- return `${i18n.t("instances")} - ${this.state.siteRes.site_view.site.name}`;
+ return `${I18NextService.i18n.t("instances")} - ${
+ this.state.siteRes.site_view.site.name
+ }`;
}
- componentWillUnmount() {
- if (isBrowser()) {
- this.subscription?.unsubscribe();
+ renderInstances() {
+ switch (this.state.instancesRes.state) {
+ case "loading":
+ return (
+ <h5>
+ <Spinner large />
+ </h5>
+ );
+ case "success": {
+ const instances = this.state.instancesRes.data.federated_instances;
+ return instances ? (
+ <>
+ <h1 className="h4 mb-4">{I18NextService.i18n.t("instances")}</h1>
+ <div className="row">
+ <div className="col-md-6">
+ <h2 className="h5 mb-3">
+ {I18NextService.i18n.t("linked_instances")}
+ </h2>
+ {this.itemList(instances.linked)}
+ </div>
+ </div>
+ <div className="row">
+ {instances.allowed && instances.allowed.length > 0 && (
+ <div className="col-md-6">
+ <h2 className="h5 mb-3">
+ {I18NextService.i18n.t("allowed_instances")}
+ </h2>
+ {this.itemList(instances.allowed)}
+ </div>
+ )}
+ {instances.blocked && instances.blocked.length > 0 && (
+ <div className="col-md-6">
+ <h2 className="h5 mb-3">
+ {I18NextService.i18n.t("blocked_instances")}
+ </h2>
+ {this.itemList(instances.blocked)}
+ </div>
+ )}
+ </div>
+ </>
+ ) : (
+ <></>
+ );
+ }
}
}
render() {
- const federated_instances = this.state.instancesRes?.federated_instances;
- return federated_instances ? (
- <div className="container-lg">
+ return (
+ <div className="home-instances container-lg">
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
/>
- <div className="row">
- <div className="col-md-6">
- <h5>{i18n.t("linked_instances")}</h5>
- {this.itemList(federated_instances.linked)}
- </div>
- {federated_instances.allowed &&
- federated_instances.allowed.length > 0 && (
- <div className="col-md-6">
- <h5>{i18n.t("allowed_instances")}</h5>
- {this.itemList(federated_instances.allowed)}
- </div>
- )}
- {federated_instances.blocked &&
- federated_instances.blocked.length > 0 && (
- <div className="col-md-6">
- <h5>{i18n.t("blocked_instances")}</h5>
- {this.itemList(federated_instances.blocked)}
- </div>
- )}
- </div>
+ {this.renderInstances()}
</div>
- ) : (
- <></>
);
}
<table id="instances_table" className="table table-sm table-hover">
<thead className="pointer">
<tr>
- <th>{i18n.t("name")}</th>
- <th>{i18n.t("software")}</th>
- <th>{i18n.t("version")}</th>
+ <th>{I18NextService.i18n.t("name")}</th>
+ <th>{I18NextService.i18n.t("software")}</th>
+ <th>{I18NextService.i18n.t("version")}</th>
</tr>
</thead>
<tbody>
</table>
</div>
) : (
- <div>{i18n.t("none_found")}</div>
+ <div>{I18NextService.i18n.t("none_found")}</div>
);
}
- parseMessage(msg: any) {
- const op = wsUserOp(msg);
- console.log(msg);
- if (msg.error) {
- toast(i18n.t(msg.error), "danger");
- this.context.router.history.push("/");
- this.setState({ loading: false });
- return;
- } else if (op == UserOperation.GetFederatedInstances) {
- const data = wsJsonToRes<GetFederatedInstancesResponse>(msg);
- this.setState({ loading: false, instancesRes: data });
- }
- }
}