1 import { Component, FormEventHandler, linkEvent } from "inferno";
2 import { LocalSiteRateLimit } from "lemmy-js-client";
3 import { i18n } from "../../i18next";
4 import Tabs from "../common/tabs";
6 const rateLimitTypes = [
15 interface RateLimitsProps {
16 handleRateLimit: FormEventHandler<HTMLInputElement>;
17 handleRateLimitPerSecond: FormEventHandler<HTMLInputElement>;
18 rateLimitLabel: string;
19 rateLimitValue?: number;
20 rateLimitPerSecondValue?: number;
23 interface RateLimitFormProps {
24 localSiteRateLimit: LocalSiteRateLimit;
27 interface RateLimitFormState {
29 message_per_second?: number;
31 post_per_second?: number;
33 comment_per_second?: number;
35 image_per_second?: number;
37 search_per_second?: number;
39 register_per_second?: number;
44 handleRateLimitPerSecond,
46 rateLimitPerSecondValue,
50 <div className="form-group row">
51 <label className="col-12 col-form-label" htmlFor="rate-limit">
57 className="form-control col-12"
59 value={rateLimitValue}
60 onInput={handleRateLimit}
62 <label className="col-12 col-form-label" htmlFor="rate-limit-per-second">
63 {i18n.t("per_second")}
67 id="rate-limit-per-second"
68 className="form-control col-12"
70 value={rateLimitPerSecondValue}
71 onInput={handleRateLimitPerSecond}
77 function handleRateLimitChange(
78 { rateLimitType, ctx }: { rateLimitType: string; ctx: RateLimitsForm },
82 [rateLimitType]: Number(event.target.value),
86 function handlePerSecondChange(
87 { rateLimitType, ctx }: { rateLimitType: string; ctx: RateLimitsForm },
91 [`${rateLimitType}_per_second`]: Number(event.target.value),
95 export default class RateLimitsForm extends Component<
99 state: RateLimitFormState = {};
100 constructor(props: RateLimitFormProps, context) {
101 super(props, context);
116 } = props.localSiteRateLimit;
137 tabs={rateLimitTypes.map(rateLimitType => ({
139 label: rateLimitType,
142 handleRateLimit={linkEvent(
143 { rateLimitType, ctx: this },
144 handleRateLimitChange
146 handleRateLimitPerSecond={linkEvent(
147 { rateLimitType, ctx: this },
148 handlePerSecondChange
150 rateLimitLabel={i18n.t(`rate_limit_${rateLimitType}`)}
151 rateLimitValue={this.state[rateLimitType]}
152 rateLimitPerSecondValue={
153 this.state[`${rateLimitType}_per_second`]