// pages/index.tsx import Head from 'next/head'; import { useEffect, useRef, useState } from 'react'; import Box from '../../components/Box'; import TextArea from '../../components/TextArea'; import Input from '../../components/Input'; import Button from '../../components/Button'; import Markdown from '../../components/Markdown'; import Typography from '../../components/Typography'; import Script from 'next/script'; // @ts-ignore import desc from './desc.md'; /* * HOOK THE MODULE FROM THE DOM */ interface Wg2ndModule { wg2nd_cmdseq: (intfName : string, intfconfig : string) => string; } declare global { interface Window { Wg2ndModule: any; wg2ndOnLoad: (module : Wg2ndModule) => void } } const useModule = (onInitCallback? : (module: T) => void): T | null => { const [module, setModule] = useState(null); function setModuleWithCallback(module : T) { console.log('setting module'); if(onInitCallback !== undefined) { onInitCallback(module); } setModule(module); } useEffect(() => { window.wg2ndOnLoad = function() { setModuleWithCallback(window.Wg2ndModule); } if(module === null && typeof window.Wg2ndModule === "object") { setModuleWithCallback(window.Wg2ndModule); } }); return module; }; const DEMO_CONFIG = `[Interface] PrivateKey = 0OCS+dV5wsDje6qUAEDQzPmTNWOLE9HE8kfGU1wJUE0= Address = 10.55.127.342/32, ab00:aaaa:aaa:aa02::5:abcd/128 DNS = 10.64.0.1 [Peer] PublicKey = WBSnuq6Vswxz5G5zz9pUt60ZSA+JfZ1iTXdg0RJGjks= AllowedIPs = 0.0.0.0/0,::0/0 Endpoint = 128.45.210.64:51821 `; let rows = 0; for(const char of DEMO_CONFIG) if(char == '\n') rows++; interface PanelProps { children?: React.ReactNode; customStyle?: React.CSSProperties; }; const Panel: React.FC = ({ children, customStyle }) => (
{children}
); import { Breadcrumbs, LinkCrumb } from "../../components/Breadcrumbs"; const Wg2nd = () => { const intfNameRef = useRef(null); const intfConfigRef = useRef(null); const ndConfigRef = useRef(null); const parentDebounceDiv = useRef(null); const convertConfig = (module : Wg2ndModule) => { if(intfNameRef.current === null || intfConfigRef.current === null || ndConfigRef.current === null || parentDebounceDiv.current === null) { return; } const intfName = intfNameRef.current!.value; const intfConfig = intfConfigRef.current!.value; const result = module.wg2nd_cmdseq(intfName, intfConfig); // This is a hack: adjust a wrapper around the output text area // Thus, we can detect the scrollHeight of the TextArea without // the screen moving. parentDebounceDiv.current!.style.height = ndConfigRef.current!.style.height; ndConfigRef.current!.style.height = `0px`; ndConfigRef.current!.value = result; const scrollHeight = ndConfigRef.current!.scrollHeight; ndConfigRef.current!.style.height = `${scrollHeight}px`; parentDebounceDiv.current!.style.height = 'auto'; }; const module = useModule(convertConfig); return ( <>