Skip to content

Commit

Permalink
Merge pull request #45 from Chia-Network/nwo-enhancements
Browse files Browse the repository at this point in the history
feat: implement localization to broadcast locale changes to child apps
  • Loading branch information
TheLastCicada authored Jul 26, 2024
2 parents 1ddbcab + f5f17d6 commit d24fdcf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 20 deletions.
60 changes: 43 additions & 17 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ExplorerLogo } from "./components/ExplorerLogo";
import { TokenizationLogo } from "./components/TokenizationLogo";
import { Flowbite, Button, Modal } from "flowbite-react";
import flowbiteThemeSettings from "./flowbite.theme";
import { LocaleSwitcher } from "./components/LocalSwitcher";

const appLinks = {
cadt: {
Expand Down Expand Up @@ -37,9 +38,36 @@ const App = () => {
}, [flowbiteThemeSettings]);

const ActiveLogo = activeApp.logo;
const TokenizationLogo = appLinks["climateTokenization"].logo;
const ExplorerLogo = appLinks["climateExplorer"].logo;
const CadtLogo = appLinks["cadt"].logo;

function getIframeOrigin(iframe) {
try {
const url = new URL(iframe.src);
return url.origin;
} catch (error) {
console.error("Invalid iframe URL", error);
return null;
}
}

function sendMessageToIframe(iframe, message) {
const targetOrigin = getIframeOrigin(iframe);
if (targetOrigin && iframe.contentWindow) {
iframe.contentWindow.postMessage(message, targetOrigin);
} else {
console.error(
"Failed to determine target origin or iframe is not available"
);
}
}

const handleLocaleChange = (event) => {
const message = { changeLocale: event };
[cadtRef, climateExplorerRef, climateTokenizationRef].forEach((ref) => {
if (ref.current) {
sendMessageToIframe(ref.current, message);
}
});
};

const handleSubmit = (e) => {
e.preventDefault();
Expand All @@ -48,12 +76,9 @@ const App = () => {
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());

// Validation function for host
const isValidHost = (host) => /^https?:\/\/.+(:\d{1,5})?$/.test(host);
// Validation function for API key (adjust as needed)
const isValidApiKey = (key) => key.trim() !== "";

// Array containing the field names, corresponding error element IDs, and validation functions
const fields = [
{
name: "cadtRegistryHost",
Expand Down Expand Up @@ -180,17 +205,18 @@ const App = () => {
) : (
<div></div>
)}

{validateLocalStorage() ? (
<Button color="gray" onClick={handleDisconnect}>
Disconnect
</Button>
) : (
<Button color="gray" onClick={() => setShowConnect(true)}>
Connect
</Button>
)}

<div className="flex gap-8 items-center">
<LocaleSwitcher handleLocaleChange={handleLocaleChange} />
{validateLocalStorage() ? (
<Button color="gray" onClick={handleDisconnect}>
Disconnect
</Button>
) : (
<Button color="gray" onClick={() => setShowConnect(true)}>
Connect
</Button>
)}
</div>
{showConnect && (
<Modal show={true} onClose={() => setShowConnect(false)}>
<Modal.Header>Connect to Core Registry</Modal.Header>
Expand Down
39 changes: 39 additions & 0 deletions src/components/LocalSwitcher.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useState } from "react";
import styled, { withTheme } from "styled-components";
import { Dropdown } from "flowbite-react";

const LANGUAGE_CODES = Object.freeze({
ENGLISH: "en-US",
SPANISH: "es-ES",
FRENCH: "fr-FR",
GERMAN: "de-DE",
CHINESE: "cn",
});

const LocaleSwitcher = ({ handleLocaleChange }) => {
const [currentLanguage, setCurrentLanguage] = useState(
LANGUAGE_CODES["ENGLISH"]
);

const handleChange = (value) => {
setCurrentLanguage(value);
handleLocaleChange(value);
};
return (
<Dropdown
label={currentLanguage}
>
{Object.keys(LANGUAGE_CODES).map((key) => (
<Dropdown.Item
key={LANGUAGE_CODES[key]}
onClick={() => handleChange(LANGUAGE_CODES[key])}
value={LANGUAGE_CODES[key]}
>
{key}
</Dropdown.Item>
))}
</Dropdown>
);
};

export { LocaleSwitcher };
6 changes: 3 additions & 3 deletions src/flowbite.theme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default () => ({
theme: {
dropdown: {
arrowIcon: "ml-2 h-11 w-4 items-center justify-center",
content: "py-1 focus:outline-none",
arrowIcon: "ml-2 w-4 self-center justify-center",
content: "items-center py-1 focus:outline-none",
floating: {
animation: "transition-opacity",
arrow: {
Expand All @@ -23,7 +23,7 @@ export default () => ({
hidden: "invisible opacity-0",
item: {
container: "focus:outline-none",
base: "focus:outline-none flex items-center justify-start py-2 px-4 text-sm text-gray-700 cursor-pointer w-full hover:bg-gray-900 focus:outline-none dark:text-gray-200 dark:hover:bg-gray-900 focus:outline-none dark:hover:text-white dark:focus:bg-gray-600 dark:focus:text-white",
base: "focus:outline-none flex items-center justify-start py-2 px-4 text-sm text-white cursor-pointer w-full hover:bg-gray-900 focus:outline-none dark:text-gray-200 dark:hover:bg-gray-900 focus:outline-none dark:hover:text-white dark:focus:bg-gray-600 dark:focus:text-white",
icon: "mr-2 h-4 w-4 focus:outline-none",
},
style: {
Expand Down

0 comments on commit d24fdcf

Please sign in to comment.