From 42642184397c35aa944e9bd6dc82da9eeac7e956 Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Thu, 6 Apr 2023 12:13:47 -0400 Subject: [PATCH] Disable "Login with Netlify Identity" button until Netlify identity is initialized --- .../dev-test/backends/git-gateway/index.html | 1 + packages/core/dev-test/index.js | 1 - .../git-gateway/AuthenticationPage.tsx | 37 +++++++++++++++++++ .../backends/git-gateway/implementation.tsx | 22 ----------- .../entry-editor/EditorInterface.tsx | 5 --- .../components/entry-editor/EditorToolbar.tsx | 7 ---- packages/core/src/components/login/Login.tsx | 4 +- 7 files changed, 41 insertions(+), 36 deletions(-) diff --git a/packages/core/dev-test/backends/git-gateway/index.html b/packages/core/dev-test/backends/git-gateway/index.html index 03a2b384..41e379de 100644 --- a/packages/core/dev-test/backends/git-gateway/index.html +++ b/packages/core/dev-test/backends/git-gateway/index.html @@ -2,6 +2,7 @@ + Static CMS - Git Gateway Development Test diff --git a/packages/core/dev-test/index.js b/packages/core/dev-test/index.js index 13e0084d..e77b98f3 100644 --- a/packages/core/dev-test/index.js +++ b/packages/core/dev-test/index.js @@ -232,7 +232,6 @@ CMS.registerShortcode('youtube', { return [src]; }, control: ({ src, onChange, theme }) => { - console.log('[SHORTCUT] shortcut theme', theme); return h('span', {}, [ h('input', { key: 'control-input', diff --git a/packages/core/src/backends/git-gateway/AuthenticationPage.tsx b/packages/core/src/backends/git-gateway/AuthenticationPage.tsx index f9c3188f..19655140 100644 --- a/packages/core/src/backends/git-gateway/AuthenticationPage.tsx +++ b/packages/core/src/backends/git-gateway/AuthenticationPage.tsx @@ -32,6 +32,42 @@ const GitGatewayAuthenticationPage = ({ onLogin, t }: GitGatewayAuthenticationPa password?: string; }>({}); + const [initialized, setInitialized] = useState(false); + + useEffect(() => { + if (window.netlifyIdentity) { + let initialized = false; + Promise.race([ + new Promise(resolve => { + window.netlifyIdentity?.on('init', () => { + if (!initialized) { + initialized = true; + resolve(); + } + }); + }), + new Promise(resolve => { + const interval = setInterval(() => { + if (initialized) { + clearInterval(interval); + return; + } + + if (window.netlifyIdentity) { + console.info('Manually initializing identity widget'); + initialized = true; + window.netlifyIdentity.init(); + clearInterval(interval); + resolve(); + } + }, 250); + }), + ]).then(() => { + setInitialized(true); + }); + } + }, []); + useEffect(() => { if (!loggedIn && window.netlifyIdentity && window.netlifyIdentity.currentUser()) { setLoggingIn(true); @@ -119,6 +155,7 @@ const GitGatewayAuthenticationPage = ({ onLogin, t }: GitGatewayAuthenticationPa label={t('auth.loginWithNetlifyIdentity')} inProgress={loggingIn} error={errorContent} + disabled={!initialized} /> ); }; diff --git a/packages/core/src/backends/git-gateway/implementation.tsx b/packages/core/src/backends/git-gateway/implementation.tsx index 823c3f05..de5e6ba8 100644 --- a/packages/core/src/backends/git-gateway/implementation.tsx +++ b/packages/core/src/backends/git-gateway/implementation.tsx @@ -106,27 +106,6 @@ function getEndpoint(endpoint: string, netlifySiteURL: string | null) { return endpoint; } -// wait for identity widget to initialize -// force init on timeout -let initPromise = Promise.resolve() as Promise; -if (window.netlifyIdentity) { - let initialized = false; - initPromise = Promise.race([ - new Promise(resolve => { - window.netlifyIdentity?.on('init', () => { - initialized = true; - resolve(); - }); - }), - new Promise(resolve => setTimeout(resolve, 2500)).then(() => { - if (!initialized) { - console.info('Manually initializing identity widget'); - window.netlifyIdentity?.init(); - } - }), - ]); -} - interface NetlifyUser extends Credentials { jwt: () => Promise; email: string; @@ -222,7 +201,6 @@ export default class GitGateway implements BackendClass { if (this.authClient) { return this.authClient; } - await initPromise; this.authClient = { logout: () => window.netlifyIdentity?.logout(), currentUser: () => window.netlifyIdentity?.currentUser(), diff --git a/packages/core/src/components/entry-editor/EditorInterface.tsx b/packages/core/src/components/entry-editor/EditorInterface.tsx index 1ee36f88..42c68fa8 100644 --- a/packages/core/src/components/entry-editor/EditorInterface.tsx +++ b/packages/core/src/components/entry-editor/EditorInterface.tsx @@ -136,16 +136,13 @@ const EditorInterface = ({ }, []); const [showPreviewToggle, previewInFrame] = useMemo(() => { - console.log('[PREVIEW] collection', collection); let preview = collection.editor?.preview ?? true; - console.log('[PREVIEW] preview', preview); let frame = collection.editor?.frame ?? true; if ('files' in collection) { const file = getFileFromSlug(collection, entry.slug); if (file?.editor?.preview !== undefined) { preview = file.editor.preview; - console.log('[PREVIEW] preview file', preview); } if (file?.editor?.frame !== undefined) { @@ -247,8 +244,6 @@ const EditorInterface = ({ ); const breadcrumbs = useBreadcrumbs(collection, nestedFieldPath, { isNewEntry, summary, t }); - console.log('[PREVIEW] showPreviewToggle', showPreviewToggle); - return ( (
diff --git a/packages/core/src/components/login/Login.tsx b/packages/core/src/components/login/Login.tsx index 20c12fb1..af1425a2 100644 --- a/packages/core/src/components/login/Login.tsx +++ b/packages/core/src/components/login/Login.tsx @@ -16,6 +16,7 @@ export interface LoginProps { icon?: FC<{ className?: string | undefined }>; label?: string; error?: ReactNode; + disabled?: boolean; } const Login = ({ @@ -24,6 +25,7 @@ const Login = ({ icon, label, error, + disabled = false, t, }: TranslatedProps) => { const config = useAppSelector(selectConfig); @@ -53,7 +55,7 @@ const Login = ({
{error}
) : null} - {config?.site_url && }