Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openMFP] Move luigiContext and isInIframe to own composables #2223

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import { useProjectStore } from '@/store/project'

import { useCustomColors } from '@/composables/useCustomColors'

import { useOpenMFP } from './composables/useOpenMFP'

import get from 'lodash/get'

const theme = useTheme()
Expand All @@ -47,9 +45,6 @@ const loginStore = useLoginStore()
const shootStore = useShootStore()
const projectStore = useProjectStore()
const logger = inject('logger')
const openMFP = useOpenMFP()

openMFP.setRoute(route)

async function setCustomColors () {
try {
Expand Down
69 changes: 32 additions & 37 deletions frontend/src/composables/useOpenMFP.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,43 @@
// 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 { createGlobalState } from '@vueuse/core'
import LuigiClient from '@luigi-project/client'

import { useLogger } from '@/composables/useLogger'
import { useIsInIframe } from '@/composables/useIsInIframe'

export const useOpenMFP = createGlobalState(() => {
const logger = useLogger()
const isInIframe = 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) {
Expand All @@ -34,49 +49,29 @@ export const useOpenMFP = createGlobalState(() => {

const accountId = computed(() => luigiContext.value?.accountId)

function getLuigiContext () {
async function getLuigiContext () {
if (!isInIframe.value) {
return Promise.resolve(null)
return null
}
if (luigiContext.value !== null) {
return Promise.resolve(luigiContext.value)
return luigiContext.value
}
return new Promise(resolve => {
const timeout = 3000
const timeoutId = setTimeout(() => {
unwatch()
logger.error('The initialization of the Luigi Client has timed out after %d milliseconds', timeout)
resolve(null)
}, timeout)
const unwatch = watch(luigiContext, context => {
if (context !== null) {
clearTimeout(timeoutId)
unwatch()
resolve(context)
}
}, {
immediate: true,
})
})
}

function setRoute (route) {
if (isInIframe.value) {
const pathname = toRef(route, 'path')
watch(pathname, value => {
if (value) {
LuigiClient.linkManager().fromVirtualTreeRoot().withoutSync().navigate(value)
}
}, {
immediate: true,
const timeout = 3000
try {
await until(luigiContext).toBeTruthy({
timeout: 1000,
petersutter marked this conversation as resolved.
Show resolved Hide resolved
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,
setRoute,
getLuigiContext,
}
})
2 changes: 1 addition & 1 deletion frontend/src/router/guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { useApi } from '@/composables/useApi'
export function createGlobalBeforeGuards () {
const logger = useLogger()
const api = useApi()
const openMFP = useOpenMFP()
const appStore = useAppStore()
const authnStore = useAuthnStore()
const configStore = useConfigStore()
Expand All @@ -52,6 +51,7 @@ export function createGlobalBeforeGuards () {
return true
}

const openMFP = useOpenMFP()
const context = await openMFP.getLuigiContext()
petersutter marked this conversation as resolved.
Show resolved Hide resolved
if (context) {
logger.debug('Luigi context:', context)
Expand Down
Loading