microsoft/azure-devops-extension-sample

Getting project name on constructor

brunoffalves5 opened this issue · 3 comments

Hello,

I creating a very simple extension which creates a button to open a link.
The link, however, is dependent on the Project Name from which it was called.

I have this code on the React Component:


import "./OpenPowerApp.scss";

import * as React from "react";
import { showRootComponent } from "../../Common";
import * as SDK from "azure-devops-extension-sdk"; // Import Azure DevOps SDK
import { CommonServiceIds, IProjectPageService,IHostNavigationService, INavigationElement, IPageRoute  } from "azure-devops-extension-api";

interface IHubContentState {
    projectName?: string;
    fullScreenMode?: boolean
    headerDescription?: string;
    useLargeTitle?: boolean;
    useCompactPivots?: boolean;
    iframeUrl?: string;
}
class HubContent extends React.Component<{}, IHubContentState> {
  constructor(props: {}) {
        super(props);
    }
    public componentDidMount() {
        this.initializeState();
    }

    public async initializeState():  Promise<void>{
        await SDK.ready();

        window.open("http://www.google.com", "_blank");
        const projectService = await SDK.getService<IProjectPageService>(CommonServiceIds.ProjectPageService);
        const project = await projectService.getProject();
        if (project) {
            this.setState({ projectName: project.name });
        } 
    }
}
const Hub_Content = new HubContent({});
Hub_Content.componentDidMount();
const projectName = Hub_Content.state;    
const projectUrl = "https://myurl?ProjectName=" + projectName;
window.open(projectUrl, "_blank");
showRootComponent(<HubContent />);

This code does open the link, but the projectName comes as undefined, and through the tests I've done, by placing the URL call inside the async function, it seems that any code inside the async function is not being run.

Do you have any suggestions or alternatives to get the project name and open the URL?

In this example I gave, the google link is not opened, but if I place it inside ComponentDidMount function, which is synchronous, it does open.

Managed to get it working, apparently I was missing a SDK.init(); on componentDidMount()

So close the issue, please.