I need something like "useProductContext" in my CUSTOM UI application

Hey everyone.

I just wonder, is there a way that I could have access to information that useProductContext is providing in my CUSTOM UI ?
I cannot use UI KIT hooks directly into my CUSTOM UI app, but I need so have some context information about my app.

is there a conveniente way to provide such a information in a CUSTOM UI app?

Thanks a lot!

Hey :wave:
In Custom UI we have different approach for context data.
You have access to those in resolvers.

f.e.

resolver.define('getData', async (request) => {
  // `context` here is equivalent of `useProductContext` output
  const { payload, context } = request;
  //...
});

More information about how to get context in both Forge UI Kit and Custom UI is here :point_down:

https://developer.atlassian.com/platform/forge/app-context-security/

3 Likes

Worth noting that Forge also just gained a dedicated function as of today’s release 2021-05-04:

Custom UI now supports context on the frontend through the new getContext function that’s present on the existing view object. See the view documentation for more details.

See getContext() for details:

The getContext method enables you to retrieve contextual information for your custom UI app. The data available depends on the module in which your app is used. […]

3 Likes

Indeed! Thx @sopel for mentioning that, indeed it was released today :smiley:

import { view } from '@forge/bridge';

const context = await view.getContext();

https://developer.atlassian.com/platform/forge/runtime-reference/custom-ui-bridge/?_ga=2.178743780.1342737107.1620072960-18596019.1617977522#getcontext

3 Likes

Thank you @MichalMichalczuk and @sopel for your help!
not directly related to this topic but, is there a way to have access on context or I don’t know, pass any value to my manifest.yaml file?

I want to dynamically change my app title based on the project that it’s on ( project name )

AFAIK this is not possible.
In what extension you’d like to do this, and what case do you have?

You can render your own page header in jira:adminPage and jira:projectPage when you set layout: basic in module config (I am not sure about Confluence extension points).

1 Like

Hi guys,
I’m trying to implement the new function, but I get an empty object.
I tried both backend and frontend, same results. I just need to get the page Id the macro is in.
Any suggestion?

Matteo

Hey @MatteoGubelliniSoftC :wave:

Can you reveal what you have so far?
And what version of forge libraries you’re using?

Hi @MichalMichalczuk ,
I tried the new function view.getContext(), but if implemented as above I get a an error related to the use of “await” outside an async function. I’m new to Forge and coding in general as you can see.
I have bridge 1.4.0, cli 1.3.2.
Thanks

@MatteoGubelliniSoftC , You can’t call await outside a async function that’s duo to javascript on browser, top-level await call is not supported by default.

You can try it like this:

async function getContext() {

const context = await view.getContext()

// set context to state or do other logics here
}

useEffect(() => {

getContext()
},[ ])
2 Likes