Refactors useLocale (#908)
* Removes unused component * Refactors useLocale We don't need to pass the locale prop everywhere * Fixes syntax error * Adds warning for missing localeProps * Simplify i18n utils * Update components/I18nLanguageHandler.tsx Co-authored-by: Mihai C <[email protected]> * Type fixes Co-authored-by: Mihai C <[email protected]>
This commit is contained in:
1 parent
7dd6fdde7a
commit
cfd70172f0
35 files changed
+366
-440
No files matched your search
+15
-12
@@ -13,7 +13,6 @@ import {
|
||||
localeOptions,
|
||||
OptionType,
|
||||
} from "@lib/core/i18n/i18n.utils";
|
||||
import { useLocale } from "@lib/hooks/useLocale";
|
||||
import { isBrandingHidden } from "@lib/isBrandingHidden";
|
||||
import prisma from "@lib/prisma";
|
||||
import { trpc } from "@lib/trpc";
|
||||
@@ -92,7 +91,6 @@ function HideBrandingInput(props: {
|
||||
}
|
||||
|
||||
export default function Settings(props: Props) {
|
||||
const { locale } = useLocale({ localeProp: props.localeProp });
|
||||
const mutation = trpc.useMutation("viewer.updateProfile");
|
||||
|
||||
const [successModalOpen, setSuccessModalOpen] = useState(false);
|
||||
@@ -101,14 +99,19 @@ export default function Settings(props: Props) {
|
||||
const descriptionRef = useRef<HTMLTextAreaElement>();
|
||||
const avatarRef = useRef<HTMLInputElement>(null);
|
||||
const hideBrandingRef = useRef<HTMLInputElement>(null);
|
||||
const [selectedTheme, setSelectedTheme] = useState({ value: props.user.theme });
|
||||
const [selectedTimeZone, setSelectedTimeZone] = useState({ value: props.user.timeZone });
|
||||
const [selectedWeekStartDay, setSelectedWeekStartDay] = useState({ value: props.user.weekStart });
|
||||
const [selectedLanguage, setSelectedLanguage] = useState<OptionType>({
|
||||
value: locale,
|
||||
label: props.localeLabels[locale],
|
||||
const [selectedTheme, setSelectedTheme] = useState<null | { value: string | null }>({
|
||||
value: props.user.theme,
|
||||
});
|
||||
const [imageSrc, setImageSrc] = useState<string>(props.user.avatar);
|
||||
const [selectedTimeZone, setSelectedTimeZone] = useState({ value: props.user.timeZone });
|
||||
const [selectedWeekStartDay, setSelectedWeekStartDay] = useState({
|
||||
value: props.user.weekStart,
|
||||
label: "",
|
||||
});
|
||||
const [selectedLanguage, setSelectedLanguage] = useState<OptionType>({
|
||||
value: props.localeProp,
|
||||
label: props.localeLabels[props.localeProp],
|
||||
});
|
||||
const [imageSrc, setImageSrc] = useState<string>(props.user.avatar || "");
|
||||
const [hasErrors, setHasErrors] = useState(false);
|
||||
const [errorMessage, setErrorMessage] = useState("");
|
||||
|
||||
@@ -117,7 +120,7 @@ export default function Settings(props: Props) {
|
||||
props.user.theme ? themeOptions.find((theme) => theme.value === props.user.theme) : null
|
||||
);
|
||||
setSelectedWeekStartDay({ value: props.user.weekStart, label: props.user.weekStart });
|
||||
setSelectedLanguage({ value: locale, label: props.localeLabels[locale] });
|
||||
setSelectedLanguage({ value: props.localeProp, label: props.localeLabels[props.localeProp] });
|
||||
}, []);
|
||||
|
||||
const closeSuccessModal = () => {
|
||||
@@ -276,7 +279,7 @@ export default function Settings(props: Props) {
|
||||
<div className="mt-1">
|
||||
<Select
|
||||
id="languageSelect"
|
||||
value={selectedLanguage || locale}
|
||||
value={selectedLanguage || props.localeProp}
|
||||
onChange={setSelectedLanguage}
|
||||
classNamePrefix="react-select"
|
||||
className="react-select-container border border-gray-300 rounded-sm shadow-sm focus:ring-neutral-500 focus:border-neutral-500 mt-1 block w-full sm:text-sm"
|
||||
@@ -327,7 +330,7 @@ export default function Settings(props: Props) {
|
||||
defaultValue={selectedTheme || themeOptions[0]}
|
||||
value={selectedTheme || themeOptions[0]}
|
||||
onChange={setSelectedTheme}
|
||||
className="shadow-sm focus:ring-neutral-500 focus:border-neutral-500 mt-1 block w-full sm:text-sm border-gray-300 rounded-sm"
|
||||
className="shadow-sm | { value: string } focus:ring-neutral-500 focus:border-neutral-500 mt-1 block w-full sm:text-sm border-gray-300 rounded-sm"
|
||||
options={themeOptions}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { GetServerSidePropsContext } from "next";
|
||||
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
||||
import React from "react";
|
||||
|
||||
@@ -5,26 +6,26 @@ import { getSession } from "@lib/auth";
|
||||
import { getOrSetUserLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
|
||||
import { useLocale } from "@lib/hooks/useLocale";
|
||||
import prisma from "@lib/prisma";
|
||||
import { inferSSRProps } from "@lib/types/inferSSRProps";
|
||||
|
||||
import SettingsShell from "@components/SettingsShell";
|
||||
import Shell from "@components/Shell";
|
||||
import ChangePasswordSection from "@components/security/ChangePasswordSection";
|
||||
import TwoFactorAuthSection from "@components/security/TwoFactorAuthSection";
|
||||
|
||||
export default function Security({ user, localeProp }) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { locale, t } = useLocale({ localeProp });
|
||||
export default function Security({ user }: inferSSRProps<typeof getServerSideProps>) {
|
||||
const { t } = useLocale();
|
||||
return (
|
||||
<Shell heading={t("security")} subtitle={t("manage_account_security")}>
|
||||
<SettingsShell>
|
||||
<ChangePasswordSection localeProp={locale} />
|
||||
<TwoFactorAuthSection localeProp={locale} twoFactorEnabled={user.twoFactorEnabled} />
|
||||
<ChangePasswordSection />
|
||||
<TwoFactorAuthSection twoFactorEnabled={user.twoFactorEnabled} />
|
||||
</SettingsShell>
|
||||
</Shell>
|
||||
);
|
||||
}
|
||||
|
||||
export async function getServerSideProps(context) {
|
||||
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
const session = await getSession(context);
|
||||
const locale = await getOrSetUserLocaleFromHeaders(context.req);
|
||||
|
||||
@@ -44,6 +45,10 @@ export async function getServerSideProps(context) {
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return { redirect: { permanent: false, destination: "/auth/login" } };
|
||||
}
|
||||
|
||||
return {
|
||||
props: {
|
||||
localeProp: locale,
|
||||
|
||||
@@ -8,7 +8,6 @@ import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import { getSession } from "@lib/auth";
|
||||
import { getOrSetUserLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
|
||||
import { useLocale } from "@lib/hooks/useLocale";
|
||||
import { Member } from "@lib/member";
|
||||
import { Team } from "@lib/team";
|
||||
|
||||
@@ -20,7 +19,7 @@ import TeamList from "@components/team/TeamList";
|
||||
import TeamListItem from "@components/team/TeamListItem";
|
||||
import Button from "@components/ui/Button";
|
||||
|
||||
export default function Teams(props: { localeProp: string }) {
|
||||
export default function Teams() {
|
||||
const noop = () => undefined;
|
||||
const [, loading] = useSession();
|
||||
const [teams, setTeams] = useState([]);
|
||||
@@ -29,7 +28,6 @@ export default function Teams(props: { localeProp: string }) {
|
||||
const [editTeamEnabled, setEditTeamEnabled] = useState(false);
|
||||
const [teamToEdit, setTeamToEdit] = useState<Team | null>();
|
||||
const nameRef = useRef<HTMLInputElement>() as React.MutableRefObject<HTMLInputElement>;
|
||||
const { locale } = useLocale({ localeProp: props.localeProp });
|
||||
|
||||
const handleErrors = async (resp: Response) => {
|
||||
if (!resp.ok) {
|
||||
@@ -114,11 +112,7 @@ export default function Teams(props: { localeProp: string }) {
|
||||
</div>
|
||||
<div>
|
||||
{!!teams.length && (
|
||||
<TeamList
|
||||
localeProp={locale}
|
||||
teams={teams}
|
||||
onChange={loadData}
|
||||
onEditTeam={editTeam}></TeamList>
|
||||
<TeamList teams={teams} onChange={loadData} onEditTeam={editTeam}></TeamList>
|
||||
)}
|
||||
|
||||
{!!invites.length && (
|
||||
@@ -127,7 +121,6 @@ export default function Teams(props: { localeProp: string }) {
|
||||
<ul className="px-4 mt-4 mb-2 bg-white border divide-y divide-gray-200 rounded">
|
||||
{invites.map((team: Team) => (
|
||||
<TeamListItem
|
||||
localeProp={locale}
|
||||
onChange={loadData}
|
||||
key={team.id}
|
||||
team={team}
|
||||
@@ -140,7 +133,7 @@ export default function Teams(props: { localeProp: string }) {
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{!!editTeamEnabled && <EditTeam localeProp={locale} team={teamToEdit} onCloseEdit={onCloseEdit} />}
|
||||
{!!editTeamEnabled && <EditTeam team={teamToEdit} onCloseEdit={onCloseEdit} />}
|
||||
{showCreateTeamModal && (
|
||||
<div
|
||||
className="fixed inset-0 z-50 overflow-y-auto"
|
||||
|
||||
Reference in new issue
Block a user