Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Locale overwriting #1175

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ You can also check the [release page](https://github.com/visualize-admin/visuali

- Fixes
- Table docs now work correctly again
- Changing the locale when previewing a larger cube no longer triggers multiple locale switches

# [3.22.5] - 2023-09-12

Expand Down
2 changes: 1 addition & 1 deletion app/browse/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const DataSetPreviewTable = ({
title: string;
dimensions: DimensionMetadataFragment[];
measures: DimensionMetadataFragment[];
observations: Observation[];
observations: Observation[] | undefined;
}) => {
const headers = useMemo(() => {
return getSortedColumns([...dimensions, ...measures]);
Expand Down
14 changes: 7 additions & 7 deletions app/browser/dataset-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ export const DataSetPreview = ({
window.scrollTo({ top: 0 });
}, []);

if (metadata?.dataCubeByIri) {
if (fetching) {
return (
<Flex className={classes.loadingWrapper}>
<Loading />
</Flex>
);
} else if (metadata?.dataCubeByIri) {
const { dataCubeByIri } = metadata;

return (
Expand Down Expand Up @@ -188,12 +194,6 @@ export const DataSetPreview = ({
</Paper>
</Flex>
);
} else if (fetching) {
return (
<Flex className={classes.loadingWrapper}>
<Loading />
</Flex>
);
} else {
return (
<Flex className={classes.loadingWrapper}>
Expand Down
4 changes: 2 additions & 2 deletions app/components/use-redirect-to-versioned-cube.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("use redirect to versioned cube", () => {
versionedCube: { iri: "https://versioned-cube" },
});
expect(router.replace).toHaveBeenCalledWith({
pathname: "/de/browse",
pathname: "/browse",
query: {
dataset: "https://versioned-cube",
},
Expand All @@ -100,7 +100,7 @@ describe("use redirect to versioned cube", () => {
versionedCube: { iri: "https://versioned-cube2" },
});
expect(router.replace).toHaveBeenCalledWith({
pathname: "/de/browse",
pathname: "/browse",
query: {
dataset: "https://versioned-cube2",
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/use-redirect-to-versioned-cube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const useRedirectToVersionedCube = ({

if (resp) {
router.replace({
pathname: `/${locale}/browse`,
pathname: "/browse",
query: {
...router.query,
...(router.query.iri ? { iri: resp.iri } : { dataset: resp.iri }),
Expand Down
3 changes: 1 addition & 2 deletions app/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ export default function App({
pageProps: { session, ...pageProps },
}: AppProps) {
const { events: routerEvents, asPath, locale: routerLocale } = useRouter();
const locale = parseLocaleString(routerLocale ?? "");

useNProgress();

const locale = parseLocaleString(routerLocale ?? "");

// Immediately activate locale to avoid re-render
if (i18n.locale !== locale) {
i18n.activate(locale);
Expand Down
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
Loading