Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 27, 2024
1 parent 5313c4f commit a6889ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion web/src/context/installerL10n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useInstallerClientStatus } from "./installer";
import agama from "~/agama";
import supportedLanguages from "~/languages.json";
import { fetchConfig, updateConfig } from "~/api/l10n";
import { useL10n, useL10nUIChanges } from "~/queries/l10n";

const L10nContext = React.createContext(null);

Expand Down Expand Up @@ -221,6 +222,8 @@ function InstallerL10nProvider({ children }) {
const [keymap, setKeymap] = useState(undefined);
const [backendPending, setBackendPending] = useState(false);
const { cancellablePromise } = useCancellablePromise();
const l10n = useL10n();
useL10nUIChanges();

const storeInstallerLanguage = useCallback(
async (newLanguage) => {
Expand Down Expand Up @@ -297,7 +300,14 @@ function InstallerL10nProvider({ children }) {

const value = { language, changeLanguage, keymap, changeKeymap };

return <L10nContext.Provider value={value}>{children}</L10nContext.Provider>;
return (
<L10nContext.Provider value={value}>
<div>
<p>{l10n.uiLocale?.id}</p>
{children}
</div>
</L10nContext.Provider>
);
}

export { InstallerL10nProvider, useInstallerL10n };
22 changes: 22 additions & 0 deletions web/src/queries/l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ const useL10nConfigChanges = () => {
}, [client, queryClient]);
};

/**
* Hook that returns a useEffect to listen for L10nConfigChanged events
*
* When the configuration changes, it invalidates the config query and forces the router to
* revalidate its data (executing the loaders again).
*/
const useL10nUIChanges = () => {
const queryClient = useQueryClient();
const client = useInstallerClient();

React.useEffect(() => {
if (!client) return;

return client.onEvent((event) => {
if (event.type === "LocaleChanged") {
queryClient.invalidateQueries({ queryKey: ["l10n/config"] });
}
});
}, [client, queryClient]);
};

/// Returns the l10n data.
const useL10n = () => {
const [{ data: config }, { data: locales }, { data: keymaps }, { data: timezones }] =
Expand Down Expand Up @@ -127,4 +148,5 @@ export {
useConfigMutation,
useL10n,
useL10nConfigChanges,
useL10nUIChanges,
};

0 comments on commit a6889ff

Please sign in to comment.