Skip to content

Commit 8507a96

Browse files
Merge feature branch to main (#1044)
* Configure FCL (#1026) * Add FCL * Configure for docusaurus * Add different AN --------- Co-authored-by: Chase Fleming <[email protected]> * Add FCL auth in nav (#1028) * Add connect button to nav * Setup connect button with fcl auth --------- Co-authored-by: Chase Fleming <[email protected]> * Improve design system buttons (#1029) * Improve design system buttons * Remove * Refactor to objects * Refactor to one button * Fix indentation * Fix import * Change * Change node version * Revert "Change node version" This reverts commit e523ec4. --------- Co-authored-by: Chase Fleming <[email protected]> * Add event for auth * Hash addr for ga (#1040) Co-authored-by: Chase Fleming <[email protected]> * Add flow.json (#1039) * Add flow.json * Commit emulator key --------- Co-authored-by: Chase Fleming <[email protected]> * Add wc to auth (#1042) * Add fcl wc * Add env example * Remove * Add WC project id * Add app detail --------- Co-authored-by: Chase Fleming <[email protected]> --------- Co-authored-by: Chase Fleming <[email protected]> Co-authored-by: Alex <[email protected]>
1 parent 99e02b2 commit 8507a96

File tree

23 files changed

+2352
-161
lines changed

23 files changed

+2352
-161
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ TYPESENSE_SEARCH_ONLY_API_KEY="<TYPESENSE_SEARCH_ONLY_API_KEY>"
44
TYPESENSE_ADMIN_API_KEY="<TYPESENSE_ADMIN_API_KEY>"
55

66
MIXPANEL_PROJECT_TOKEN="<MIXPANEL_PROJECT_TOKEN>"
7+
8+
FLOW_NETWORK=testnet
9+
WALLET_CONNECT_PROJECT_ID="<WALLET_CONNECT_PROJECT_ID>"

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ docs/build/core-contracts/flow-nft/*
3636
docs/ecosystem/overview/*
3737

3838
docs/.obsidian
39+
40+
# flow
41+
imports

docusaurus.config.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { DocusaurusConfig } from '@docusaurus/types';
2+
3+
declare module '@generated/docusaurus.config' {
4+
interface CustomFields {
5+
flowNetwork: string;
6+
}
7+
8+
interface CustomDocusaurusConfig extends DocusaurusConfig {
9+
customFields: CustomFields;
10+
}
11+
12+
const config: CustomDocusaurusConfig;
13+
export default config;
14+
}

docusaurus.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const { remarkCodeHike } = require('@code-hike/mdx');
99
const path = require('path');
1010
const fs = require('fs');
1111

12+
const flowNetwork = process.env.FLOW_NETWORK || 'testnet';
13+
1214
const externalDataSourceLocation = './src/data/data-sources.json';
1315
let cachedRepositories;
1416

@@ -282,6 +284,10 @@ const config = {
282284
label: 'Ecosystem',
283285
activeBasePath: '/ecosystem',
284286
},
287+
{
288+
type: 'custom-connectButton',
289+
position: 'right',
290+
},
285291
{
286292
href: 'https://github.com/onflow',
287293
html: '<img src="" alt="GitHub" id="navbar-github" class="box-content h-32 w-32"/><span class="p-2 desktop:hidden">Github</span>',
@@ -637,6 +643,10 @@ const config = {
637643
},
638644
],
639645
clientModules: [require.resolve('./src/modules/toolscards.ts')],
646+
647+
customFields: {
648+
flowNetwork,
649+
},
640650
};
641651

642652
module.exports = config;

emulator-account.pkey

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x87c62162e859f621e11e74742aa66e7d3ad135d9dad476b753c50990a872f635

flow.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"networks": {
3+
"emulator": "127.0.0.1:3569",
4+
"mainnet": "access.mainnet.nodes.onflow.org:9000",
5+
"testing": "127.0.0.1:3569",
6+
"testnet": "access.devnet.nodes.onflow.org:9000"
7+
},
8+
"accounts": {
9+
"emulator-account": {
10+
"address": "f8d6e0586b0a20c7",
11+
"key": {
12+
"type": "file",
13+
"location": "emulator-account.pkey"
14+
}
15+
}
16+
}
17+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@fortawesome/react-fontawesome": "^0.2.0",
3232
"@headlessui/react": "1.7.13",
3333
"@mdx-js/react": "^3.1.0",
34+
"@onflow/fcl": "^1.13.1",
3435
"@tailwindcss/aspect-ratio": "0.4.2",
3536
"@tailwindcss/forms": "0.5.6",
3637
"@tailwindcss/typography": "0.5.10",

src/components/addNetworkButton.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2-
import {
3-
Button,
4-
ButtonLink,
5-
} from '@site/src/ui/design-system/src/lib/Components/Button/index';
2+
import { Button } from '@site/src/ui/design-system/src/lib/Components/Button/index';
63

74
const targetChains = [
85
{
@@ -89,7 +86,6 @@ export const AddNetworkButton = (): JSX.Element => {
8986
<Button
9087
key={chain.id}
9188
disabled={isNetworkAdded}
92-
variant="secondary"
9389
onClick={() => {
9490
addFlowNetwork(chain).catch((e) => {
9591
console.error(e);
@@ -103,8 +99,8 @@ export const AddNetworkButton = (): JSX.Element => {
10399
))}
104100
</div>
105101
) : (
106-
<ButtonLink variant="primary" href="https://metamask.io/download/">
102+
<Button variant="primary" href="https://metamask.io/download/">
107103
Install MetaMask
108-
</ButtonLink>
104+
</Button>
109105
);
110106
};

src/components/connect-button.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { useCurrentUser } from '@site/src/hooks/use-current-user';
3+
import { Button } from '@site/src/ui/design-system/src/lib/Components/Button';
4+
5+
const ConnectButton: React.FC = () => {
6+
const { user, logIn, logOut } = useCurrentUser();
7+
8+
return (
9+
<Button size="sm" className="mr-2" onClick={user.loggedIn ? logOut : logIn}>
10+
{user.loggedIn ? 'Disconnect' : 'Connect'}
11+
</Button>
12+
);
13+
};
14+
15+
export default ConnectButton;

src/config/fcl.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as fcl from '@onflow/fcl';
2+
import config from '@generated/docusaurus.config';
3+
4+
const flowNetwork = config.customFields.flowNetwork;
5+
6+
console.log('Dapp running on network:', flowNetwork);
7+
8+
export function configureFCL(): void {
9+
fcl.config({
10+
'flow.network': flowNetwork,
11+
'accessNode.api': flowNetwork ?
12+
'https://rest-testnet.onflow.org' :
13+
'https://rest-mainnet.onflow.org',
14+
'discovery.wallet': `https://fcl-discovery.onflow.org/${flowNetwork}/authn`,
15+
'app.detail.icon': 'https://avatars.githubusercontent.com/u/62387156?v=4',
16+
'app.detail.title': 'Flow Dev Portal',
17+
'app.detail.description': 'The developer portal for Flow Blockchain',
18+
'walletconnect.projectId': process.env.WALLETCONNECT_PROJECT_ID,
19+
});
20+
}
21+
22+
configureFCL();

src/hooks/use-current-user.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use client';
2+
3+
import { useEffect, useState } from 'react';
4+
import * as fcl from '@onflow/fcl';
5+
import { event } from '@site/src/utils/gtags.client';
6+
7+
interface FlowUser {
8+
addr: string | null;
9+
loggedIn: boolean;
10+
}
11+
12+
interface UseCurrentUserReturn {
13+
user: FlowUser;
14+
logIn: () => Promise<void>;
15+
logOut: () => void;
16+
}
17+
18+
async function hashAddress(address: string): Promise<string> {
19+
const encoder = new TextEncoder();
20+
const data = encoder.encode(address);
21+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
22+
const hashArray = Array.from(new Uint8Array(hashBuffer));
23+
const hashHex = hashArray
24+
.map((byte) => byte.toString(16).padStart(2, '0'))
25+
.join('');
26+
return hashHex;
27+
}
28+
29+
export function useCurrentUser(): UseCurrentUserReturn {
30+
const [user, setUser] = useState<FlowUser>({ addr: null, loggedIn: false });
31+
32+
useEffect(() => {
33+
const unsubscribe = fcl.currentUser.subscribe(setUser);
34+
return () => unsubscribe();
35+
}, []);
36+
37+
const logIn = async (): Promise<void> => {
38+
try {
39+
await fcl.authenticate();
40+
41+
const snapshot = await fcl.currentUser.snapshot();
42+
const userAddrSnapshot = snapshot?.addr;
43+
44+
if (userAddrSnapshot) {
45+
const hashedAddr = await hashAddress(userAddrSnapshot);
46+
47+
event({
48+
category: 'auth',
49+
action: 'login',
50+
label: `user_${hashedAddr}`,
51+
value: 1,
52+
});
53+
} else {
54+
event({
55+
category: 'auth',
56+
action: 'login',
57+
label: 'unknown_user',
58+
value: 1,
59+
});
60+
}
61+
} catch (error) {
62+
console.error('Error during login:', error);
63+
}
64+
};
65+
66+
const logOut = (): void => {
67+
fcl.unauthenticate();
68+
};
69+
70+
return {
71+
user,
72+
logIn,
73+
logOut,
74+
};
75+
}

src/pages/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import HomePage from '../ui/design-system/src/lib/Pages/HomePage';
77
// import { refreshTools } from '../cms/tools.server'
88
import { externalLinks } from '../data/external-links';
99
import { contentNavigationListItems } from '../data/pages/home';
10+
import '../config/fcl';
1011

1112
const data = {
1213
discordUrl: externalLinks.discord,
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ComponentTypes from '@theme-original/NavbarItem/ComponentTypes';
2+
import ConnectButton from '@site/src/components/connect-button';
3+
4+
export default {
5+
...ComponentTypes,
6+
'custom-connectButton': ConnectButton,
7+
};

0 commit comments

Comments
 (0)