Skip to content

Commit

Permalink
Fix usage of Lingui
Browse files Browse the repository at this point in the history
V4 required some migration steps that had been missed
(https://lingui.dev/releases/migration-4), namely:
- Use useLingui() hook when using `t` macro in React components
- When using `t` macros outside of React components, use the `msg`
  (`defineMessage`) macro instead

Also upgrade to latest Lingui version.
  • Loading branch information
shu8 committed Sep 3, 2023
1 parent b8c0045 commit 22ae2d5
Show file tree
Hide file tree
Showing 43 changed files with 604 additions and 488 deletions.
2 changes: 2 additions & 0 deletions components/ConnectToAlertingAuthorityForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Button, Form, InputPicker, Loader, Message } from "rsuite";
import { HandledError } from "../lib/helpers.client";
import { useToasterI18n } from "../lib/useToasterI18n";
import ErrorMessage from "./ErrorMessage";
import { useLingui } from "@lingui/react";

type RegisterData = {
alertingAuthorityId: string;
};

export default function ConnectToAlertingAuthorityForm() {
useLingui();
const toaster = useToasterI18n();
const [alertingAuthorities, setAlertingAuthorities] = useState([]);
const [finishedLoading, setFinishedLoading] = useState(false);
Expand Down
2 changes: 2 additions & 0 deletions components/KeyValueInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, Form, Stack, Uploader } from "rsuite";
import { useToasterI18n } from "../lib/useToasterI18n";
import ErrorMessage from "./ErrorMessage";
import { HandledError } from "../lib/helpers.client";
import { useLingui } from "@lingui/react";

export default function KeyValueInput({
keyLabel,
Expand All @@ -24,6 +25,7 @@ export default function KeyValueInput({
disabled?: boolean;
allowImageUploadValue?: boolean;
}) {
useLingui();
const toaster = useToasterI18n();
const [showForm, setShowForm] = useState(false);
const [newKey, setNewKey] = useState("");
Expand Down
2 changes: 2 additions & 0 deletions components/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { HandledError, updateState } from "../lib/helpers.client";
import { useToasterI18n } from "../lib/useToasterI18n";
import styles from "../styles/components/AuthenticateForm.module.css";
import ErrorMessage from "./ErrorMessage";
import { useLingui } from "@lingui/react";

type RegisterData = {
name: string;
email: string;
};

export default function RegisterForm({ email = "" }) {
useLingui();
const toaster = useToasterI18n();
const [formData, setFormData] = useState<RegisterData>({ name: "", email });

Expand Down
2 changes: 2 additions & 0 deletions components/UpdateAlertingAuthorityDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fetcher, HandledError } from "../lib/helpers.client";
import { useToasterI18n } from "../lib/useToasterI18n";
import ErrorMessage from "./ErrorMessage";
import useSWR from "swr";
import { useLingui } from "@lingui/react";

type Data = {
defaultTimezone: string;
Expand All @@ -18,6 +19,7 @@ export default function UpdateAlertingAuthorityDetailsForm({
}: {
alertingAuthorityId: string;
}) {
useLingui();
const toaster = useToasterI18n();
const router = useRouter();
const {
Expand Down
4 changes: 3 additions & 1 deletion components/UpdatePersonalDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { HandledError } from "../lib/helpers.client";
import { useToasterI18n } from "../lib/useToasterI18n";
import ErrorMessage from "./ErrorMessage";
import { useSession } from "next-auth/react";
import { useLingui } from "@lingui/react";

type Data = { name: string };
export default function UpdatePersonalDetailsForm() {
useLingui();
const { data: session } = useSession();
const toaster = useToasterI18n();
const router = useRouter();
Expand Down Expand Up @@ -45,7 +47,7 @@ export default function UpdatePersonalDetailsForm() {
toaster.push(
<ErrorMessage
error={err}
action="updating your personal details"
action={t`updating your personal details`}
/>
)
);
Expand Down
6 changes: 4 additions & 2 deletions components/editor/EditorSinglePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import {
} from "./fields";
import SeverityCertaintyMatrix from "./SeverityCertaintyMatrix";
import XMLPreview from "./XMLPreview";
import { useLingui } from "@lingui/react";

const STEPS = ["metadata", "category", "map", "data", "text", "summary"];
export type Step = typeof STEPS[number];
export type Step = (typeof STEPS)[number];

export type FormAlertData = {
// Only present if an Alert is being edited (instead of created)
Expand Down Expand Up @@ -86,6 +87,7 @@ type Props = {
};

export default function EditorSinglePage(props: Props) {
useLingui();
const [alertData, setAlertData] = useState(props.defaultAlertData);
const isNewLanguageDraft = !!props.multiLanguageGroupId;
const onUpdate = (data: Partial<FormAlertData>) =>
Expand Down Expand Up @@ -151,7 +153,7 @@ export default function EditorSinglePage(props: Props) {
className={styles.shareIcon}
onClick={() => {
const email = window.prompt(
"Please enter the email address of the user you wish to invite to collaborate"
t`Please enter the email address of the user you wish to invite to collaborate`
);
if (!email) return;

Expand Down
3 changes: 3 additions & 0 deletions components/editor/XMLPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useDebounce } from "usehooks-ts";
import { UserAlertingAuthority } from "../../lib/types/types";
import { FormAlertData } from "./EditorSinglePage";
import { formatDate } from "../../lib/helpers.client";
import { useLingui } from "@lingui/react";

export default function XMLPreview({
alertingAuthority,
Expand All @@ -16,6 +17,8 @@ export default function XMLPreview({
alertData: FormAlertData;
multiLanguageGroupId?: string;
}) {
useLingui();

// Debounce alertData, so we only send preview API request every second
const debouncedAlertData = useDebounce(alertData, 1000);
const [xmlPreview, setXmlPreview] = useState("");
Expand Down
51 changes: 27 additions & 24 deletions components/editor/fields/Category.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import { t } from "@lingui/macro";
import { useLingui } from "@lingui/react";
import { DropdownField, FieldProps } from "./common";

const CATEGORIES = [
{ label: t`Geophysical (e.g., landslide)`, value: "Geo" },
{ label: t`Meteorological (inc. flood)`, value: "Met" },
{ label: t`General emergency & public safety`, value: "Safety" },
{
label: t`Law enforcement, military, homeland & local/private security`,
value: "Security",
},
{ label: t`Rescue & recovery`, value: "Rescue" },
{ label: t`Fire supression & rescue`, value: "Fire" },
{ label: t`Medical & public health`, value: "Health" },
{ label: t`Pollution & other environmental`, value: "Env" },
{ label: t`Public & private transportation`, value: "Transport" },
{
label: t`Utility, telecommunication & other non-transport infrastructure`,
value: "Infra",
},
{
label: t`Chemical, Biological, Radiological, Nuclear or High-Yield Explosive threat or attack`,
value: "CBRNE",
},
{ label: t`Other`, value: "Other" },
];

export default function Category(props: FieldProps) {
useLingui();

const CATEGORIES = [
{ label: t`Geophysical (e.g., landslide)`, value: "Geo" },
{ label: t`Meteorological (inc. flood)`, value: "Met" },
{ label: t`General emergency & public safety`, value: "Safety" },
{
label: t`Law enforcement, military, homeland & local/private security`,
value: "Security",
},
{ label: t`Rescue & recovery`, value: "Rescue" },
{ label: t`Fire supression & rescue`, value: "Fire" },
{ label: t`Medical & public health`, value: "Health" },
{ label: t`Pollution & other environmental`, value: "Env" },
{ label: t`Public & private transportation`, value: "Transport" },
{
label: t`Utility, telecommunication & other non-transport infrastructure`,
value: "Infra",
},
{
label: t`Chemical, Biological, Radiological, Nuclear or High-Yield Explosive threat or attack`,
value: "CBRNE",
},
{ label: t`Other`, value: "Other" },
];

return (
<DropdownField
{...props}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Certainty.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { t } from "@lingui/macro";
import { DropdownField, FieldProps } from "./common";
import { iso6393 } from "iso-639-3";
import { useLingui } from "@lingui/react";

export default function Certainty(props: FieldProps) {
useLingui();
return (
<DropdownField
{...props}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Contact.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { t } from "@lingui/macro";
import { FieldProps, TextField } from "./common";
import { useLingui } from "@lingui/react";

export default function Contact({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<TextField
onUpdate={onUpdate}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Description.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { t } from "@lingui/macro";
import { FieldProps, TextField } from "./common";
import { useLingui } from "@lingui/react";

export default function Description({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<TextField
onUpdate={onUpdate}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Event.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { t } from "@lingui/macro";
import { FieldProps, TextField } from "./common";
import { useLingui } from "@lingui/react";

export default function Event({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<TextField
onUpdate={onUpdate}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Expires.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { t } from "@lingui/macro";
import { DateTimeField, FieldProps } from "./common";
import { useLingui } from "@lingui/react";

export default function Expires(props: FieldProps) {
useLingui();
return <DateTimeField {...props} label={t`Expires`} fieldName="expires" />;
}
2 changes: 2 additions & 0 deletions components/editor/fields/Headline.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { t } from "@lingui/macro";
import { FieldProps, TextField } from "./common";
import { useLingui } from "@lingui/react";

export default function Headline({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<TextField
onUpdate={onUpdate}
Expand Down
3 changes: 3 additions & 0 deletions components/editor/fields/Instruction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useState } from "react";
import { HandledError, camelise } from "../../../lib/helpers.client";
import ErrorMessage from "../../ErrorMessage";
import { Button, Modal, Nav, Panel, PanelGroup } from "rsuite";
import { useLingui } from "@lingui/react";

const WhatNowMessage = ({
message,
Expand Down Expand Up @@ -78,6 +79,8 @@ export default function Instruction({
alertData,
alertingAuthority,
}: FieldProps & { alertingAuthority: UserAlertingAuthority }) {
useLingui();

const toaster = useToasterI18n();
const [whatNowMessages, setWhatNowMessages] = useState<WhatNowResponse[]>([]);
const [showWhatNowModal, setShowWhatNowModal] = useState(false);
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Language.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { t } from "@lingui/macro";
import { DropdownField, FieldProps } from "./common";
import { iso6393 } from "iso-639-3";
import { useLingui } from "@lingui/react";

export default function Language({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<DropdownField
onUpdate={onUpdate}
Expand Down
3 changes: 3 additions & 0 deletions components/editor/fields/MapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import KeyValueInput from "../../KeyValueInput";
import Map from "../map/Map";
import { FieldProps, Textarea } from "./common";
import styles from "../../../styles/components/editor/EditorSinglePage.module.css";
import { useLingui } from "@lingui/react";

const POSITIVE_COORDINATE_REGEX = /\d+(\.\d{1,3})?/;
const COORDINATE_REGEX = /-?\d+(\.\d{1,3})?/;
Expand All @@ -24,6 +25,8 @@ export default function MapForm({
disabled,
alertingAuthority,
}: FieldProps & { alertingAuthority: UserAlertingAuthority }) {
useLingui();

const toaster = useToasterI18n();
const [countries, setCountries] = useState([]);
const [selectedRegion, setSelectedRegion] = useState("");
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/MessageType.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { t } from "@lingui/macro";
import { DropdownField, FieldProps } from "./common";
import { iso6393 } from "iso-639-3";
import { useLingui } from "@lingui/react";

export default function MessageType(props: FieldProps) {
useLingui();
return (
<DropdownField
{...props}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Onset.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { t } from "@lingui/macro";
import { DateTimeField, FieldProps } from "./common";
import { useLingui } from "@lingui/react";

export default function Onset(props: FieldProps) {
useLingui();
return <DateTimeField {...props} label={t`Onset`} fieldName="onset" />;
}
3 changes: 3 additions & 0 deletions components/editor/fields/References.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { useToasterI18n } from "../../../lib/useToasterI18n";
import { useState } from "react";
import { HandledError } from "../../../lib/helpers.client";
import ErrorMessage from "../../ErrorMessage";
import { useLingui } from "@lingui/react";

export default function References({
onUpdate,
alertData,
alertingAuthorityId,
}: FieldProps & { alertingAuthorityId: string }) {
useLingui();

const toaster = useToasterI18n();
const [referenceOptions, setReferenceOptions] = useState<Alert[]>([]);
const fetchReferenceOptions = () => {
Expand Down
6 changes: 4 additions & 2 deletions components/editor/fields/Resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { HandledError } from "../../../lib/helpers.client";
import { useToasterI18n } from "../../../lib/useToasterI18n";
import KeyValueInput from "../../KeyValueInput";
import { FieldProps } from "./common";
import { useLingui } from "@lingui/react";

export default function Resources({ onUpdate, alertData }: FieldProps) {
useLingui();
const toaster = useToasterI18n();

const getMimeType = async (url: string): Promise<string> => {
Expand All @@ -21,8 +23,8 @@ export default function Resources({ onUpdate, alertData }: FieldProps) {
<Form.ControlLabel>
<Trans>Resources</Trans>{" "}
<KeyValueInput
keyLabel="Description"
valueLabel="URL"
keyLabel={t`Description`}
valueLabel={t`URL`}
addLabel={t`Add URL?`}
allowImageUploadValue
emptyLabel={
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/ResponseType.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { t } from "@lingui/macro";
import { DropdownField, FieldProps } from "./common";
import { useLingui } from "@lingui/react";

export default function ResponseType(props: FieldProps) {
useLingui();
return (
<DropdownField
{...props}
Expand Down
3 changes: 2 additions & 1 deletion components/editor/fields/Severity.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { t } from "@lingui/macro";
import { useLingui } from "@lingui/react";
import { DropdownField, FieldProps } from "./common";
import { iso6393 } from "iso-639-3";

export default function Severity(props: FieldProps) {
useLingui();
return (
<DropdownField
{...props}
Expand Down
3 changes: 2 additions & 1 deletion components/editor/fields/Status.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { t } from "@lingui/macro";
import { DropdownField, FieldProps } from "./common";
import { iso6393 } from "iso-639-3";
import { useLingui } from "@lingui/react";

export default function Status(props: FieldProps) {
useLingui();
return (
<DropdownField
{...props}
Expand Down
2 changes: 2 additions & 0 deletions components/editor/fields/Timezone.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { t } from "@lingui/macro";
import timezones from "timezones.json";
import { DropdownField, FieldProps } from "./common";
import { useLingui } from "@lingui/react";

export default function Timezone({ onUpdate, alertData }: FieldProps) {
useLingui();
return (
<DropdownField
onUpdate={onUpdate}
Expand Down
Loading

0 comments on commit 22ae2d5

Please sign in to comment.