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 Unstyled text flash on first app load #246

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions download_binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

ARCH=$1
BIN_DIR="electron/bins/"
mkdir -p $BIN_DIR

trader_version=$(poetry run python -c "import yaml; config = yaml.safe_load(open('templates/trader.yaml')); print(config['configuration']['trader_version'])")
curl -L -o "${BIN_DIR}aea_bin" "https://github.com/valory-xyz/trader/releases/download/${trader_version}/trader_bin_${ARCH}"
9 changes: 9 additions & 0 deletions frontend/components/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styled from 'styled-components';

const LoadingWrapper = styled.div`
background-color: #0000;
`;

const Loading = () => <LoadingWrapper />;

export default Loading;
27 changes: 26 additions & 1 deletion frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../styles/globals.scss';

import { ConfigProvider } from 'antd';
import type { AppProps } from 'next/app';
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useState } from 'react';

import { Layout } from '@/components/Layout';
import { BalanceProvider } from '@/context/BalanceProvider';
Expand All @@ -18,17 +18,42 @@ import { StakingContractInfoProvider } from '@/context/StakingContractInfoProvid
import { StoreProvider } from '@/context/StoreProvider';
import { WalletProvider } from '@/context/WalletProvider';
import { mainTheme } from '@/theme';
import Loading from '@/components/Loading';

export default function App({ Component, pageProps }: AppProps) {
const isMounted = useRef(false);
const [isLoaded, setIsLoaded] = useState(false);
const [loadingTimeReached, setLoadingTimeReached] = useState(false);

useEffect(() => {
isMounted.current = true;

const handleLoad = () => {
setIsLoaded(true);
};
const checkStylesLoaded = () => {
const styles = document.querySelectorAll('link[rel="stylesheet"]');
if (styles.length > 0) {
handleLoad();
}
};

const timer = setTimeout(() => {
setLoadingTimeReached(true);
}, 1000);

checkStylesLoaded();
window.addEventListener('load', checkStylesLoaded);
return () => {
isMounted.current = false;
clearTimeout(timer);
window.removeEventListener('load', checkStylesLoaded);
};
}, []);

if (!loadingTimeReached || !isLoaded) {
return <Loading />;
}
return (
<ElectronApiProvider>
<StoreProvider>
Expand Down
47 changes: 47 additions & 0 deletions frontend/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// _document.tsx
import Document, { Html, Head, Main, NextScript, DocumentContext, DocumentInitialProps } from 'next/document';
import { ServerStyleSheet } from 'styled-components';

class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
});

const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}

render() {
return (
<Html>
<Head>
<link rel="preload" href="/_next/static/css/antd.css" as="style" />
<link rel="stylesheet" href="/_next/static/css/antd.css" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}

export default MyDocument;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"productName": "Pearl",
"description": "An all-in-one application designed to streamline your entry into the world of autonomous agents and earning OLAS through staking.",
"scripts": {
"build": "rm -rf dist/ && node build.tester.js",
"build:frontend": "cd frontend && yarn build && rm -rf ../electron/.next && cp -r .next ../electron/.next && rm -rf ../electron/public && cp -r public ../electron/public",
"dev:backend": "poetry run python operate/cli.py",
"dev:frontend": "cd frontend && yarn dev",
Expand All @@ -52,9 +51,11 @@
"install:backend": "poetry install --no-root",
"install:frontend": "cd frontend && yarn",
"lint:frontend": "cd frontend && yarn lint",
"start": "electron .",
"start": "yarn electron .",
"dev": "dotenv -e .env -- yarn start",
"start:frontend": "cd frontend && yarn start",
"test:frontend": "cd frontend && yarn test"
"test:frontend": "cd frontend && yarn test",
"download-binaries": "sh download_binaries.sh x64"
},
"version": "0.1.0-rc71"
}
Loading