From b8d6653fba234ec7f174f41826a8b5826ce2d69d Mon Sep 17 00:00:00 2001 From: Sartxi Date: Tue, 30 Apr 2024 17:14:05 -0600 Subject: [PATCH 1/4] require sidekick signin to start fetching project info --- libs/blocks/locui/loc/index.js | 25 +++++++++++++++++++++++++ libs/blocks/locui/loc/view.js | 17 ++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/libs/blocks/locui/loc/index.js b/libs/blocks/locui/loc/index.js index 9cc540a4be..bed29bfd59 100644 --- a/libs/blocks/locui/loc/index.js +++ b/libs/blocks/locui/loc/index.js @@ -9,6 +9,7 @@ import { telemetry, allowSyncToLangstore, canRefresh, + user, } from '../utils/state.js'; import { setStatus } from '../utils/status.js'; import { getStatus, preview } from '../utils/franklin.js'; @@ -111,7 +112,31 @@ async function loginToSharePoint() { await login({ scopes, telemetry }); } +async function connectSidekick() { + return new Promise((resolve) => { + const statusFetched = ({ detail }) => { + if (detail?.data?.profile) { + user.value = detail.data.profile; + resolve(); + } + setStatus('details', 'info', 'Please sign-in to AEM sidekick.'); + }; + const openSideKick = document.querySelector('helix-sidekick'); + if (openSideKick) { + openSideKick.addEventListener('statusfetched', statusFetched); + /* c8 ignore next 6 */ + } else { + setStatus('details', 'info', 'Please open AEM sidekick to continue.'); + document.addEventListener('sidekick-ready', () => { + const sidekick = document.querySelector('helix-sidekick'); + sidekick.addEventListener('statusfetched', statusFetched); + }, { once: true }); + } + }); +} + export async function setup() { + await connectSidekick(); await loginToSharePoint(); await loadHeading(); await loadDetails(); diff --git a/libs/blocks/locui/loc/view.js b/libs/blocks/locui/loc/view.js index 4366c5e514..ab6905919b 100644 --- a/libs/blocks/locui/loc/view.js +++ b/libs/blocks/locui/loc/view.js @@ -1,6 +1,13 @@ import { html, useEffect } from '../../../deps/htm-preact.js'; import { autoSetup, setup } from './index.js'; -import { showLogin, heading, languages, urls, serviceStatus } from '../utils/state.js'; +import { + showLogin, + heading, + languages, + urls, + serviceStatus, + user, +} from '../utils/state.js'; import { account } from '../../../tools/sharepoint/state.js'; import Heading from '../heading/view.js'; @@ -12,6 +19,14 @@ import Sync from '../sync/view.js'; export default function Localization() { useEffect(() => { autoSetup(); }, []); + + if (!user.value) { + return html` +

Milo Localization

+
<${Status} />
+ `; + } + if (!account.value.username) { return html`

Milo Localization

From 9d43e8ea3c441185e34794970520b53ee566bfa5 Mon Sep 17 00:00:00 2001 From: Sartxi Date: Tue, 21 May 2024 16:43:53 -0600 Subject: [PATCH 2/4] connect sidekick nextGen --- libs/blocks/locui/loc/index.js | 15 ++++----------- libs/utils/sidekick.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/libs/blocks/locui/loc/index.js b/libs/blocks/locui/loc/index.js index bed29bfd59..3c34afae1d 100644 --- a/libs/blocks/locui/loc/index.js +++ b/libs/blocks/locui/loc/index.js @@ -15,6 +15,7 @@ import { setStatus } from '../utils/status.js'; import { getStatus, preview } from '../utils/franklin.js'; import login from '../../../tools/sharepoint/login.js'; import { getServiceUpdates } from '../utils/miloc.js'; +import { connectAemSK } from '../../../utils/sidekick.js'; const LANG_ACTIONS = ['Translate', 'English Copy', 'Rollout']; const MOCK_REFERRER = 'https%3A%2F%2Fadobe.sharepoint.com%2F%3Ax%3A%2Fr%2Fsites%2Fadobecom%2F_layouts%2F15%2FDoc.aspx%3Fsourcedoc%3D%257B94460FAC-CDEE-4B31-B8E0-AA5E3F45DCC5%257D%26file%3Dwesco-demo.xlsx'; @@ -114,24 +115,16 @@ async function loginToSharePoint() { async function connectSidekick() { return new Promise((resolve) => { - const statusFetched = ({ detail }) => { + const onConnected = ({ detail }) => { if (detail?.data?.profile) { user.value = detail.data.profile; resolve(); } setStatus('details', 'info', 'Please sign-in to AEM sidekick.'); }; - const openSideKick = document.querySelector('helix-sidekick'); - if (openSideKick) { - openSideKick.addEventListener('statusfetched', statusFetched); - /* c8 ignore next 6 */ - } else { + connectAemSK(onConnected, () => { setStatus('details', 'info', 'Please open AEM sidekick to continue.'); - document.addEventListener('sidekick-ready', () => { - const sidekick = document.querySelector('helix-sidekick'); - sidekick.addEventListener('statusfetched', statusFetched); - }, { once: true }); - } + }); }); } diff --git a/libs/utils/sidekick.js b/libs/utils/sidekick.js index 80a92c65b4..b4d2b5d8c5 100644 --- a/libs/utils/sidekick.js +++ b/libs/utils/sidekick.js @@ -95,3 +95,33 @@ export default function init({ createTag, loadBlock, loadScript, loadStyle }) { // Color code publish button stylePublish(sk); } + +function onSidekickAvailable(callback) { + // sidekick nextGen + const observer = new MutationObserver(() => { + const sidekick = document.querySelector('aem-sidekick'); + if (sidekick) { + observer.disconnect(); + callback(sidekick); + } + }); + observer.observe(document.body, { childList: true, subtree: true }); + + // sidekick v1 ready event + document.addEventListener('sidekick-ready', () => { + callback(document.querySelector('helix-sidekick')); + }, { once: true }); +} + +export function connectAemSK(setStatus, notLoaded = null) { + const isOpen = document.querySelector('helix-sidekick') + || document.querySelector('aem-sidekick'); + if (isOpen) { + isOpen.addEventListener('statusfetched', setStatus); + } else { + notLoaded?.(); + onSidekickAvailable((sidekick) => { + sidekick?.addEventListener('statusfetched', setStatus); + }); + } +} From e8da2f096f393ed09d393487e846eac3391fcd99 Mon Sep 17 00:00:00 2001 From: Sartxi Date: Tue, 4 Jun 2024 11:41:58 -0600 Subject: [PATCH 3/4] dialing --- libs/blocks/locui/loc/index.js | 6 +++--- libs/utils/sidekick.js | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libs/blocks/locui/loc/index.js b/libs/blocks/locui/loc/index.js index 360eac80da..1c9d306786 100644 --- a/libs/blocks/locui/loc/index.js +++ b/libs/blocks/locui/loc/index.js @@ -15,7 +15,7 @@ import { setStatus } from '../utils/status.js'; import { getStatus, preview } from '../utils/franklin.js'; import login from '../../../tools/sharepoint/login.js'; import { getServiceUpdates } from '../utils/miloc.js'; -import { connectAemSK } from '../../../utils/sidekick.js'; +import { connectSK } from '../../../utils/sidekick.js'; const LANG_ACTIONS = ['Translate', 'English Copy', 'Rollout']; const MOCK_REFERRER = 'https%3A%2F%2Fadobe.sharepoint.com%2F%3Ax%3A%2Fr%2Fsites%2Fadobecom%2F_layouts%2F15%2FDoc.aspx%3Fsourcedoc%3D%257B94460FAC-CDEE-4B31-B8E0-AA5E3F45DCC5%257D%26file%3Dwesco-demo.xlsx'; @@ -137,14 +137,14 @@ async function loginToSharePoint() { async function connectSidekick() { return new Promise((resolve) => { - const onConnected = ({ detail }) => { + const onConnect = ({ detail }) => { if (detail?.data?.profile) { user.value = detail.data.profile; resolve(); } setStatus('details', 'info', 'Please sign-in to AEM sidekick.'); }; - connectAemSK(onConnected, () => { + connectSK(onConnect, () => { setStatus('details', 'info', 'Please open AEM sidekick to continue.'); }); }); diff --git a/libs/utils/sidekick.js b/libs/utils/sidekick.js index b4d2b5d8c5..ce6c1439f9 100644 --- a/libs/utils/sidekick.js +++ b/libs/utils/sidekick.js @@ -96,7 +96,7 @@ export default function init({ createTag, loadBlock, loadScript, loadStyle }) { stylePublish(sk); } -function onSidekickAvailable(callback) { +function onSkLoaded(callback) { // sidekick nextGen const observer = new MutationObserver(() => { const sidekick = document.querySelector('aem-sidekick'); @@ -113,15 +113,15 @@ function onSidekickAvailable(callback) { }, { once: true }); } -export function connectAemSK(setStatus, notLoaded = null) { - const isOpen = document.querySelector('helix-sidekick') +export function connectSK(status, standby = null) { + const sidekick = document.querySelector('helix-sidekick') || document.querySelector('aem-sidekick'); - if (isOpen) { - isOpen.addEventListener('statusfetched', setStatus); + if (sidekick) { + sidekick.addEventListener('statusfetched', status); } else { - notLoaded?.(); - onSidekickAvailable((sidekick) => { - sidekick?.addEventListener('statusfetched', setStatus); + standby?.(); + onSkLoaded((sk) => { + sk?.addEventListener('statusfetched', status); }); } } From 20a51ac33f77b088aa5bcedd3cf5be3576fe7c40 Mon Sep 17 00:00:00 2001 From: Sartxi Date: Tue, 11 Jun 2024 13:48:22 -0600 Subject: [PATCH 4/4] handle signouts --- libs/blocks/locui/loc/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/blocks/locui/loc/index.js b/libs/blocks/locui/loc/index.js index 1c9d306786..2a5b2e2984 100644 --- a/libs/blocks/locui/loc/index.js +++ b/libs/blocks/locui/loc/index.js @@ -137,14 +137,17 @@ async function loginToSharePoint() { async function connectSidekick() { return new Promise((resolve) => { - const onConnect = ({ detail }) => { - if (detail?.data?.profile) { - user.value = detail.data.profile; + const onStatus = ({ detail }) => { + const userInfo = detail?.data?.profile ?? null; + user.value = userInfo; + if (user.value) { + setStatus('details'); resolve(); + } else { + setStatus('details', 'info', 'Please sign-in to AEM sidekick.'); } - setStatus('details', 'info', 'Please sign-in to AEM sidekick.'); }; - connectSK(onConnect, () => { + connectSK(onStatus, () => { setStatus('details', 'info', 'Please open AEM sidekick to continue.'); }); });