Skip to content

Commit

Permalink
refactor: Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Sep 12, 2023
1 parent 5987542 commit 49153ea
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions app/utils/l10n-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
import { LocalizationProvider } from "@mui/lab";
import DateAdapter from "@mui/lab/AdapterDateFns";
import React, { useEffect, useState } from "react";

const AsyncLocalizationProvider = ({
locale,
children,
}: {
locale: string;
children: React.ReactNode;
}) => {
const [dateFnsModule, setDateFnsModule] = useState<{ default: object }>();
useEffect(() => {
import React from "react";

import { Locale } from "@/locales/locales";

type AsyncLocalizationProviderProps = {
locale: Locale;
};

const AsyncLocalizationProvider = (
props: React.PropsWithChildren<AsyncLocalizationProviderProps>
) => {
const { locale, children } = props;
const [dateFnsLocale, setDateFnsLocale] = React.useState<object>();

React.useEffect(() => {
const run = async () => {
if (locale === "fr") {
setDateFnsModule(await import("date-fns/locale/fr"));
} else if (locale === "de") {
setDateFnsModule(await import("date-fns/locale/de"));
} else if (locale === "it") {
setDateFnsModule(await import("date-fns/locale/it"));
} else if (locale === "en") {
setDateFnsModule(await import("date-fns/locale/en-GB"));
switch (locale) {
case "en": {
const importedLocale = await import("date-fns/locale/en-GB");
setDateFnsLocale(importedLocale.default);
break;
}
case "de":
case "fr":
case "it": {
const importedLocale = await import(
/* webpackMode: "lazy", webpackChunkName: "date-fns-[index]", webpackExclude: /_lib/ */
`date-fns/locale/${locale}/index.js`
);
setDateFnsLocale(importedLocale.default);
break;
}
default:
const _exhaustiveCheck: never = locale;
return _exhaustiveCheck;
}
};

run();
}, [locale]);
if (!dateFnsModule) {

if (!dateFnsLocale) {
return null;
}

return (
<LocalizationProvider
dateAdapter={DateAdapter}
locale={dateFnsModule.default}
>
<LocalizationProvider dateAdapter={DateAdapter} locale={dateFnsLocale}>
{children}
</LocalizationProvider>
);
Expand Down

0 comments on commit 49153ea

Please sign in to comment.