-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[openMFP] Move luigiContext and isInIframe to own composables (#2223)
* move luigiContext and isInIframe to own composables * rename to useOpenMFP * PR feedback * PR feedback II
- Loading branch information
1 parent
a55826d
commit 8754a10
Showing
11 changed files
with
120 additions
and
98 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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import { computed } from 'vue' | ||
import { createGlobalState } from '@vueuse/core' | ||
|
||
export const useIsInIframe = createGlobalState(() => { | ||
return computed(() => window.self !== window.top) | ||
}) |
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,77 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import { useRoute } from 'vue-router' | ||
import { | ||
until, | ||
createGlobalState, | ||
} from '@vueuse/core' | ||
import LuigiClient from '@luigi-project/client' | ||
import { | ||
computed, | ||
ref, | ||
toRef, | ||
watch, | ||
} from 'vue' | ||
|
||
import { useLogger } from '@/composables/useLogger' | ||
import { useIsInIframe } from '@/composables/useIsInIframe' | ||
|
||
export const useOpenMFP = createGlobalState((options = {}) => { | ||
const { | ||
logger = useLogger(), | ||
isInIframe = useIsInIframe(), | ||
route = useRoute(), | ||
} = options | ||
|
||
const luigiContext = ref(null) | ||
|
||
if (isInIframe.value) { | ||
logger.debug('Registering listener for Luigi context initialization and context updates') | ||
LuigiClient.addInitListener(context => setLuigiContext(context)) | ||
LuigiClient.addContextUpdateListener(context => setLuigiContext(context)) | ||
const pathname = toRef(route, 'path') | ||
watch(pathname, value => { | ||
if (value) { | ||
LuigiClient.linkManager().fromVirtualTreeRoot().withoutSync().navigate(value) | ||
} | ||
}, { | ||
immediate: true, | ||
}) | ||
} | ||
|
||
function setLuigiContext (value) { | ||
luigiContext.value = value | ||
} | ||
|
||
const accountId = computed(() => luigiContext.value?.accountId) | ||
|
||
async function getLuigiContext () { | ||
if (!isInIframe.value) { | ||
return null | ||
} | ||
if (luigiContext.value !== null) { | ||
return luigiContext.value | ||
} | ||
const timeout = 3000 | ||
try { | ||
await until(luigiContext).toBeTruthy({ | ||
timeout, | ||
throwOnTimeout: true, | ||
}) | ||
return luigiContext.value | ||
} catch (err) { | ||
logger.error('The initialization of the Luigi Client has timed out after %d milliseconds', timeout) | ||
return null | ||
} | ||
} | ||
|
||
return { | ||
accountId, | ||
luigiContext, | ||
getLuigiContext, | ||
} | ||
}) |
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
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
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
Oops, something went wrong.