2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2023-05-11 18:32:32 +00:00
|
|
|
import {
|
|
|
|
GetFederatedInstancesResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
Instance,
|
|
|
|
UserOperation,
|
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { Subscription } from "rxjs";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
2023-05-11 18:32:32 +00:00
|
|
|
import { InitialFetchRequest } from "../../interfaces";
|
|
|
|
import { WebSocketService } from "../../services";
|
|
|
|
import {
|
|
|
|
isBrowser,
|
|
|
|
relTags,
|
|
|
|
setIsoData,
|
|
|
|
toast,
|
|
|
|
wsClient,
|
|
|
|
wsSubscribe,
|
|
|
|
} from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { HtmlTags } from "../common/html-tags";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface InstancesState {
|
|
|
|
siteRes: GetSiteResponse;
|
2023-05-11 18:32:32 +00:00
|
|
|
instancesRes?: GetFederatedInstancesResponse;
|
|
|
|
loading: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Instances extends Component<any, InstancesState> {
|
2020-09-07 22:24:48 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2023-01-04 16:56:24 +00:00
|
|
|
state: InstancesState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
siteRes: this.isoData.site_res,
|
2023-05-11 18:32:32 +00:00
|
|
|
loading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2023-05-11 18:32:32 +00:00
|
|
|
private subscription?: Subscription;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
2023-05-11 18:32:32 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
instancesRes: this.isoData
|
|
|
|
.routeData[0] as GetFederatedInstancesResponse,
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
WebSocketService.Instance.send(wsClient.getFederatedInstances({}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2023-06-05 21:31:12 +00:00
|
|
|
const promises: Promise<any>[] = [];
|
2023-05-11 18:32:32 +00:00
|
|
|
|
|
|
|
promises.push(req.client.getFederatedInstances({}));
|
|
|
|
|
|
|
|
return promises;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2022-11-09 19:53:07 +00:00
|
|
|
return `${i18n.t("instances")} - ${this.state.siteRes.site_view.site.name}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription?.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
render() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const federated_instances = this.state.instancesRes?.federated_instances;
|
2023-01-04 16:56:24 +00:00
|
|
|
return federated_instances ? (
|
|
|
|
<div className="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)}
|
2021-02-01 18:08:45 +00:00
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
{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>
|
|
|
|
)}
|
2020-09-07 22:24:48 +00:00
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
);
|
2021-02-01 18:08:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
itemList(items: Instance[]) {
|
2021-02-01 18:08:45 +00:00
|
|
|
return items.length > 0 ? (
|
2023-05-11 18:32:32 +00:00
|
|
|
<div className="table-responsive">
|
|
|
|
<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>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{items.map(i => (
|
|
|
|
<tr key={i.domain}>
|
|
|
|
<td>
|
|
|
|
<a href={`https://${i.domain}`} rel={relTags}>
|
|
|
|
{i.domain}
|
|
|
|
</a>
|
|
|
|
</td>
|
|
|
|
<td>{i.software}</td>
|
|
|
|
<td>{i.version}</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-02-01 18:08:45 +00:00
|
|
|
) : (
|
2023-05-11 18:32:32 +00:00
|
|
|
<div>{i18n.t("none_found")}</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
2023-05-11 18:32:32 +00:00
|
|
|
parseMessage(msg: any) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const op = wsUserOp(msg);
|
2023-05-11 18:32:32 +00:00
|
|
|
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) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const data = wsJsonToRes<GetFederatedInstancesResponse>(msg);
|
2023-05-11 18:32:32 +00:00
|
|
|
this.setState({ loading: false, instancesRes: data });
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|