-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
10,256 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: SSR Check | ||
|
||
on: [push] | ||
|
||
jobs: | ||
check: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
node-version: [16.5] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Get yarn cache directory path | ||
id: yarn-cache-dir-path | ||
run: echo "::set-output name=dir::$(yarn config get cacheFolder)" | ||
- uses: actions/cache@v2 | ||
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-and-maven-cache.outputs.cache-hit != 'true'`) | ||
with: | ||
path: | | ||
${{ steps.yarn-cache-dir-path.outputs.dir }} | ||
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-yarn- | ||
- run: yarn install --immutable | ||
- run: yarn --cwd ./test/gatsby/ install | ||
- run: yarn --cwd ./test/gatsby/ build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/node_modules/ | ||
/storybook-static/ | ||
/lib/ | ||
/test/gatsby/node_modules | ||
/test/gatsby/public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function isSSR() { | ||
return typeof globalThis.window?.document?.createElement === "undefined" | ||
} | ||
|
||
export function isBrowser() { | ||
return !isSSR() | ||
} | ||
|
||
export function onSSR(callback, otherwise) { | ||
return isSSR() ? callback() : otherwise?.() | ||
} | ||
|
||
export function onBrowser(callback, otherwise) { | ||
return isBrowser() ? callback() : otherwise?.() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import { useConfluxJSDefined } from "./"; | ||
import { useEffectOnce } from "react-use"; | ||
import { useState } from "react"; | ||
import { useConfluxJSDefined } from "./" | ||
import { useEffectOnce } from "react-use" | ||
import { useState } from "react" | ||
import { onBrowser } from "./ssr-helper.js" | ||
|
||
export const UPDATE_CHAINID_SWR_ID = "UPDATE_CHAINID_SWR_ID"; | ||
export const UPDATE_NETWORKID_SWR_ID = "UPDATE_NETWORKID_SWR_ID"; | ||
export const UPDATE_CHAINID_SWR_ID = "UPDATE_CHAINID_SWR_ID" | ||
export const UPDATE_NETWORKID_SWR_ID = "UPDATE_NETWORKID_SWR_ID" | ||
|
||
export default function useChainNetId() { | ||
useConfluxJSDefined(); | ||
export default () => | ||
onBrowser( | ||
() => { | ||
useConfluxJSDefined() | ||
|
||
const [chainId, setChainId] = useState(window?.conflux?.chainId); | ||
const [networkId, setNetworkId] = useState(parseInt(window?.conflux?.networkVersion ,10) || null); | ||
const [chainId, setChainId] = useState( | ||
globalThis.window?.conflux?.chainId | ||
) | ||
const [networkId, setNetworkId] = useState( | ||
parseInt(window?.conflux?.networkVersion, 10) || null | ||
) | ||
|
||
useEffectOnce(() => { | ||
const chainListener = (chainId) => { | ||
setChainId(chainId); | ||
}; | ||
const networkListener = (networkId) => { | ||
setNetworkId(parseInt(networkId, 10) || null); | ||
}; | ||
window?.conflux?.on("chainIdChanged", chainListener); | ||
window?.conflux?.on("networkChanged", networkListener); | ||
return () => { | ||
window?.conflux?.off("chainIdChanged", chainListener); | ||
window?.conflux?.off("networkChanged", networkListener); | ||
}; | ||
}) | ||
useEffectOnce(() => { | ||
const chainListener = (chainId) => { | ||
setChainId(chainId) | ||
} | ||
const networkListener = (networkId) => { | ||
setNetworkId(parseInt(networkId, 10) || null) | ||
} | ||
window?.conflux?.on("chainIdChanged", chainListener) | ||
window?.conflux?.on("networkChanged", networkListener) | ||
return () => { | ||
window?.conflux?.off("chainIdChanged", chainListener) | ||
window?.conflux?.off("networkChanged", networkListener) | ||
} | ||
}) | ||
|
||
return {chainId, networkId} | ||
} | ||
return { chainId, networkId } | ||
}, | ||
() => ({}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
import { useState, useRef } from "react"; | ||
import { useEffectOnce } from "react-use"; | ||
import { useState, useRef } from "react" | ||
import { useEffectOnce } from "react-use" | ||
import { onBrowser } from "./ssr-helper.js" | ||
|
||
let CONFLUXJS_INTERVAL_DEFINED = false; | ||
let CONFLUXJS_INTERVAL_DEFINED = false | ||
|
||
export default function useConfluxJSDefined(customInterval = 100) { | ||
const [defined, setDefined] = useState( | ||
CONFLUXJS_INTERVAL_DEFINED || Boolean(window?.confluxJS) | ||
); | ||
const intervalRef = useRef(null); | ||
useEffectOnce(() => { | ||
if (!defined) { | ||
intervalRef.current = setInterval(() => { | ||
if (window?.confluxJS) { | ||
setDefined(true); | ||
clearInterval(intervalRef.current); | ||
export default (customInterval = 100) => | ||
onBrowser( | ||
() => { | ||
const [defined, setDefined] = useState( | ||
CONFLUXJS_INTERVAL_DEFINED || Boolean(window?.confluxJS) | ||
) | ||
const intervalRef = useRef(null) | ||
useEffectOnce(() => { | ||
if (!defined) { | ||
intervalRef.current = setInterval(() => { | ||
if (window?.confluxJS) { | ||
setDefined(true) | ||
clearInterval(intervalRef.current) | ||
} | ||
}, customInterval) | ||
} | ||
}, customInterval); | ||
} | ||
|
||
return () => { | ||
if (intervalRef.current) clearInterval(intervalRef.current); | ||
}; | ||
}); | ||
return () => { | ||
if (intervalRef.current) clearInterval(intervalRef.current) | ||
} | ||
}) | ||
|
||
return defined; | ||
} | ||
return defined | ||
}, | ||
() => false | ||
) |
Oops, something went wrong.