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

Feature: cra template overhaul #181

Merged
merged 10 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 0 additions & 8 deletions packages/cra-template-safe-app/src/App.test.tsx

This file was deleted.

43 changes: 0 additions & 43 deletions packages/cra-template-safe-app/src/App.tsx

This file was deleted.

34 changes: 0 additions & 34 deletions packages/cra-template-safe-app/src/GlobalStyle.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/cra-template-safe-app/src/fonts.d.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/cra-template-safe-app/src/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/cra-template-safe-app/src/react-app-env.d.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/cra-template-safe-app/src/setupProxy.js

This file was deleted.

30 changes: 0 additions & 30 deletions packages/cra-template-safe-app/src/setupTests.ts

This file was deleted.

14 changes: 7 additions & 7 deletions packages/cra-template-safe-app/template/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render, screen } from "@testing-library/react"
import App from "./App"
import { render, screen } from './tests/utils';
import App from './App';

test("renders learn loading screen", () => {
render(<App />)
const waitingHeading = screen.getByText(/Waiting for Safe/i)
expect(waitingHeading).toBeInTheDocument()
})
test('renders learn loading screen', () => {
render(<App />);
const waitingHeading = screen.getByText(/Waiting for Safe/i);
expect(waitingHeading).toBeInTheDocument();
});
34 changes: 17 additions & 17 deletions packages/cra-template-safe-app/template/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import React, { useCallback } from "react"
import styled from "styled-components"
import { Button, Title } from "@gnosis.pm/safe-react-components"
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk"
import React, { useCallback } from 'react';
import styled from 'styled-components';
import { Button, Title } from '@gnosis.pm/safe-react-components';
import { useSafeAppsSDK } from '@gnosis.pm/safe-apps-react-sdk';

const Container = styled.div`
padding: 1rem;
`
`;

const SafeApp = (): React.ReactElement => {
const { sdk, safe } = useSafeAppsSDK()
const { sdk, safe } = useSafeAppsSDK();

const submitTx = useCallback(async () => {
try {
const { safeTxHash } = await sdk.txs.send({
txs: [
{
to: safe.safeAddress,
value: "0",
data: "0x",
value: '0',
data: '0x',
},
],
})
console.log({ safeTxHash })
const safeTx = await sdk.txs.getBySafeTxHash(safeTxHash)
console.log({ safeTx })
});
console.log({ safeTxHash });
const safeTx = await sdk.txs.getBySafeTxHash(safeTxHash);
console.log({ safeTx });
} catch (e) {
console.error(e)
console.error(e);
}
}, [safe, sdk])
}, [safe, sdk]);

return (
<Container>
Expand All @@ -37,7 +37,7 @@ const SafeApp = (): React.ReactElement => {
Click to send a test transaction
</Button>
</Container>
)
}
);
};

export default SafeApp
export default SafeApp;
17 changes: 8 additions & 9 deletions packages/cra-template-safe-app/template/src/setupProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually

module.exports = function (app) {
app.use("/manifest.json", function (req, res, next) {
app.use('/manifest.json', function (req, res, next) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only have the cors proxy for the manifest can the icons be properly loaded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. No, they can't. I'll just apply it for every route

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, thinking about it a little bit more I'm not sure. The icons can be properly loaded via HTML img tag. They cannot be fetched from js code, though.

The interface doesn't display it because it tries to fetch it first: https://github.com/gnosis/safe-react/blob/development/src/routes/safe/components/Apps/utils.ts#L266 and if it doesn't work it doesn't try to use the URL anymore and uses placeholder image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 ... yeah not optimal not sure ...

Little bit different question shouldn't react app rewired also cover this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not well maintained and already has some caveats safe-global/safe-react-apps#88

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can stay with "react-scripts" it's better to stay with it, chances of getting updates are way higher

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense :)

res.set({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
})
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
});

next()
})
}
next();
});
};
25 changes: 25 additions & 0 deletions packages/cra-template-safe-app/template/src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
import crypto from 'crypto';

// This is required by safe-apps-sdk and it's not present in jsdom by default
function getRandomValues(buf: Uint8Array) {
if (!(buf instanceof Uint8Array)) {
throw new TypeError('expected Uint8Array');
}
if (buf.length > 65536) {
const e = new Error();
e.message =
"Failed to execute 'getRandomValues' on 'Crypto': The " +
"ArrayBufferView's byte length (" +
buf.length +
') exceeds the ' +
'number of bytes of entropy available via this API (65536).';
e.name = 'QuotaExceededError';
throw e;
}
const bytes = crypto.randomBytes(buf.length);
buf.set(bytes);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.crypto = { getRandomValues };