Skip to content

Commit 198f0e0

Browse files
author
sw-yx
committed
use context for settings, move up getting the settings
1 parent b97accc commit 198f0e0

File tree

5 files changed

+45
-52
lines changed

5 files changed

+45
-52
lines changed

.size-snapshot.json

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
22
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.cjs.development.js": {
3-
"bundled": 11704,
4-
"minified": 7561,
5-
"gzipped": 1911
3+
"bundled": 12002,
4+
"minified": 7629,
5+
"gzipped": 1929
66
},
77
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.cjs.production.js": {
8-
"bundled": 11704,
9-
"minified": 7561,
10-
"gzipped": 1911
8+
"bundled": 12002,
9+
"minified": 7629,
10+
"gzipped": 1929
1111
},
1212
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.umd.development.js": {
13-
"bundled": 12627,
14-
"minified": 6621,
15-
"gzipped": 1878
13+
"bundled": 12935,
14+
"minified": 6636,
15+
"gzipped": 1884
1616
},
1717
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.umd.production.js": {
18-
"bundled": 12627,
19-
"minified": 6621,
20-
"gzipped": 1878
18+
"bundled": 12935,
19+
"minified": 6636,
20+
"gzipped": 1884
2121
},
2222
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.es.production.js": {
23-
"bundled": 11469,
24-
"minified": 7372,
25-
"gzipped": 1860,
23+
"bundled": 11767,
24+
"minified": 7397,
25+
"gzipped": 1869,
2626
"treeshaked": {
2727
"rollup": {
28-
"code": 524,
28+
"code": 548,
2929
"import_statements": 125
3030
},
3131
"webpack": {
32-
"code": 1683
32+
"code": 1709
3333
}
3434
}
3535
}

src/app.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ import { Logout } from "./components/logout"
44
import { Signup } from "./components/signup"
55
import { useIdentityContext } from "./context"
66
import { Tabs, TabList, Tab, TabPanels, TabPanel } from "@reach/tabs"
7-
import { Settings } from "react-netlify-identity"
7+
88
import { Providers } from "./components/providers"
99
function LoggedOutScreen() {
10-
const { settings } = useIdentityContext()
11-
const [setting, setSetting] = React.useState<Settings | null>(null)
12-
React.useEffect(() => {
13-
settings().then(x => setSetting(x))
14-
}, [settings])
1510
return (
1611
<div>
1712
<Tabs defaultIndex={0}>
@@ -29,7 +24,7 @@ function LoggedOutScreen() {
2924
</TabPanel>
3025
</TabPanels>
3126
</Tabs>
32-
<Providers setting={setting} />
27+
<Providers />
3328
</div>
3429
)
3530
}
@@ -40,7 +35,6 @@ function LoggedInScreen() {
4035

4136
function Gate({ }: { onCloseDialog: Function }) {
4237
const identity = useIdentityContext()
43-
console.log({ identity })
4438
const isLoggedIn = Boolean(identity && identity.user)
4539
return isLoggedIn ? <LoggedInScreen /> : <LoggedOutScreen />
4640
}

src/components/providers.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from "react"
2-
import { useIdentityContext } from "../context"
2+
import { useIdentityContext, SettingContext } from "../context"
33
import { Settings } from "react-netlify-identity"
4-
export function Providers({ setting }: { setting: Settings | null }) {
4+
export function Providers() {
5+
const setting = React.useContext(SettingContext)
56
const hasProviders =
67
setting &&
78
setting.external &&
89
Object.entries(setting.external).some(([k, v]) => ["github", "gitlab", "bitbucket", "google"].includes(k) && v)
910
if (!hasProviders) return null
10-
console.log({ setting })
1111
return (
1212
<div className="providersGroup">
1313
<hr className="hr" />

src/context.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from "react"
22
import { useNetlifyIdentity } from "react-netlify-identity"
3-
3+
import { Settings } from "react-netlify-identity"
44
export const [useIdentityContext, IdentityContextProvider, IdentityContext] = createUsableCtx<
55
ReturnType<typeof useNetlifyIdentity>
66
>()
77

8+
export const SettingContext = React.createContext<Settings | null>(null)
9+
810
export const [FormStateContext, FormStateContextProvider] = createMutableCtx<"login" | "signup">("login")
911

1012
// utils

src/index.tsx

+20-23
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
} from "@reach/dialog"
66
import VisuallyHidden from "@reach/visually-hidden"
77
import { Widget } from "./app"
8-
import { FormStateContextProvider } from "./context"
8+
import { FormStateContextProvider, SettingContext } from "./context"
99

10-
import { useNetlifyIdentity as _useNetlifyIdentity } from "react-netlify-identity"
10+
import { Settings, useNetlifyIdentity as _useNetlifyIdentity } from "react-netlify-identity"
1111

1212
/** URL of your Netlify Instance with Identity enabled e.g. https://netlify-gotrue-in-react.netlify.com */
1313

@@ -29,33 +29,30 @@ export const useNetlifyIdentity = _useNetlifyIdentity
2929
export const IdentityContext = _IdentityContext
3030
export const useIdentityContext = _useIdentityContext
3131
export function IdentityModal({ showDialog, onCloseDialog }: ModalProps) {
32+
const { settings } = useIdentityContext()
33+
const [setting, setSetting] = React.useState<Settings | null>(null)
34+
React.useEffect(() => {
35+
settings().then(x => setSetting(x))
36+
}, [settings])
3237
return (
33-
// <DialogOverlay isOpen={showDialog}>
34-
<Dialog
35-
isOpen={showDialog}
36-
onDismiss={() => void console.log("hi") || onCloseDialog()}
37-
style={{
38-
border: "solid 5px hsla(0, 0%, 0%, 0.5)",
39-
borderRadius: "10px",
40-
position: "relative",
41-
maxWidth: 400
42-
}}
43-
>
44-
{/* <DialogContent
38+
<SettingContext.Provider value={setting}>
39+
<Dialog
40+
isOpen={showDialog}
41+
onDismiss={() => void console.log("hi") || onCloseDialog()}
4542
style={{
4643
border: "solid 5px hsla(0, 0%, 0%, 0.5)",
4744
borderRadius: "10px",
4845
position: "relative",
4946
maxWidth: 400
5047
}}
51-
> */}
52-
<button className="btn btnClose" onClick={onCloseDialog}>
53-
<VisuallyHidden>Close</VisuallyHidden>
54-
</button>
55-
<FormStateContextProvider>
56-
<Widget onCloseDialog={onCloseDialog} />
57-
</FormStateContextProvider>
58-
{/* </DialogContent> */}
59-
</Dialog>
48+
>
49+
<button className="btn btnClose" onClick={onCloseDialog}>
50+
<VisuallyHidden>Close</VisuallyHidden>
51+
</button>
52+
<FormStateContextProvider>
53+
<Widget onCloseDialog={onCloseDialog} />
54+
</FormStateContextProvider>
55+
</Dialog>
56+
</SettingContext.Provider>
6057
)
6158
}

0 commit comments

Comments
 (0)