microsoft / azure-devops-extension-sample

Sample web extension for Azure DevOps
MIT License
235 stars 154 forks source link

Getting project name on constructor #148

Closed brunoffalves5 closed 10 months ago

brunoffalves5 commented 10 months ago

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?

brunoffalves5 commented 10 months ago

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.

brunoffalves5 commented 10 months ago

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

karelkral commented 10 months ago

So close the issue, please.