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

Custom Action - Dashboard redirection #10780

Open
nicovon24 opened this issue May 14, 2024 · 6 comments
Open

Custom Action - Dashboard redirection #10780

nicovon24 opened this issue May 14, 2024 · 6 comments
Assignees
Labels
question generic question

Comments

@nicovon24
Copy link

nicovon24 commented May 14, 2024

Hello, I'm encountering an issue where I need to conditionally redirect to a new dashboard state, but I can't use the "navigate to another dashboard" action and must resort to using CUSTOM ACTIONS. I've tried using window.location.href, but it's significantly slower compared to the "Navigate to other dashboard" action and doesn't allow me to select the State. I'm unsure if this is the only solution available.

For instance, I typically have different dashboards for each device profile. In my current scenario, the hierarchy map comprises various devices from different device profiles. I aim to redirect each device to its respective dashboard, located on different dashboards within my platform, based on the predefined use case.

image

This is the current code I have which is not complett
image

In addition to that, as you can see I have for example used openState, updateState, openDashboardStateInSeparateDialog and openDashboardStateInPopover which are in the same dashboard.

But to do this in other dashboards, there is a similar function or method to do it?

Could you help me with this?

Thanks!

@nicovon24 nicovon24 added the question generic question label May 14, 2024
@nicovon24 nicovon24 reopened this May 14, 2024
@devaskim
Copy link
Contributor

In general, you need widgetContext.router.navigateByUrl() to redirect to another dashboard. I implemented my custom action in the following way (part of code was copied from TB UI source code)

let isPlant = additionalParams.nodeCtx.data.Type === 'Plant';

let state = {
    entityName: entityName,
    entityId: entityId,
    entityLabel: entityLabel
}
if (isPlant) {
    const params = {
        entityId: entityId,
        entityName: entityName,
        entityLabel: entityLabel
    };
    
    widgetContext.actionsApi.openDashboardStateInSeparateDialog('plant_information', params, null, true, 50, 50);
    //Unfortunatelly popover for some unknown reason auto closes
    // if (widgetContext.isMobile) {
    //     widgetContext.actionsApi.openDashboardStateInPopover($event, 'plant_information', params, true, 'top', false, '100vmin', '100vmin');
    // } else {
    //     widgetContext.actionsApi.openDashboardStateInPopover($event, 'plant_information', params, true, 'top', true, '60vmin', '40vmin');
    // }
} else {
    let dashboardState = {
        params: state,
        id: 'selected_silo'
    }
    
    let encodedState = objToBase64URI([dashboardState]);
    let siloDashboardId = 'd590e380-51e0-11ed-86fe-9dcd4c516023';
    let url = `/dashboard/${siloDashboardId}?state=${encodedState}`;
    widgetContext.router.navigateByUrl(url);
}

function objToBase64URI(data) {
  return encodeURIComponent(objToBase64(data));
}

function objToBase64(data) {
  const json = JSON.stringify(data);
  return btoa(encodeURIComponent(json).replace(/%([0-9A-F]{2})/g,
    function toSolidBytes(match, p1) {
      return String.fromCharCode(Number('0x' + p1));
    }));
}

@nicovon24
Copy link
Author

Hello @devaskim, thanks for your quick response! I will be using this code in the next days and test if it works, I will be writing here otherwise, see you!

@nicovon24
Copy link
Author

@devaskim it's working correctly and redirecting fastly to the new dashboard.

The question I have remained is, how can I update the state from this new dashboard state, I want to redirect to the details section and I need the currentDevice data, I want to do this

stateParams.currentEntity = { entityId: entityId, entityName: entityName, entityLabel: entityLabel };

@devaskim
Copy link
Contributor

devaskim commented May 24, 2024

You need to wrap entity id/name/label in currentEntity only if you use it as State entity parameter name in the target dashboard alias (see screenshot below). Otherwise set id/name/label directly to stateParams
Скриншот 24-05-2024 230221

@nicovon24
Copy link
Author

nicovon24 commented May 24, 2024

Good afternoon @devaskim,

I apologize for any confusion in my previous message. I need the solution using the code you provided, specifically for a Custom Action. I need the state update in the new dashboard opened.

Best regards,

In general, you need widgetContext.router.navigateByUrl() to redirect to another dashboard. I implemented my custom action in the following way (part of code was copied from TB UI source code)

let isPlant = additionalParams.nodeCtx.data.Type === 'Plant';

let state = {
    entityName: entityName,
    entityId: entityId,
    entityLabel: entityLabel
}
if (isPlant) {
    const params = {
        entityId: entityId,
        entityName: entityName,
        entityLabel: entityLabel
    };
    
    widgetContext.actionsApi.openDashboardStateInSeparateDialog('plant_information', params, null, true, 50, 50);
    //Unfortunatelly popover for some unknown reason auto closes
    // if (widgetContext.isMobile) {
    //     widgetContext.actionsApi.openDashboardStateInPopover($event, 'plant_information', params, true, 'top', false, '100vmin', '100vmin');
    // } else {
    //     widgetContext.actionsApi.openDashboardStateInPopover($event, 'plant_information', params, true, 'top', true, '60vmin', '40vmin');
    // }
} else {
    let dashboardState = {
        params: state,
        id: 'selected_silo'
    }
    
    let encodedState = objToBase64URI([dashboardState]);
    let siloDashboardId = 'd590e380-51e0-11ed-86fe-9dcd4c516023';
    let url = `/dashboard/${siloDashboardId}?state=${encodedState}`;
    widgetContext.router.navigateByUrl(url);
}

function objToBase64URI(data) {
  return encodeURIComponent(objToBase64(data));
}

function objToBase64(data) {
  const json = JSON.stringify(data);
  return btoa(encodeURIComponent(json).replace(/%([0-9A-F]{2})/g,
    function toSolidBytes(match, p1) {
      return String.fromCharCode(Number('0x' + p1));
    }));
}

@devaskim
Copy link
Contributor

Hi @nicovon24,
I have nothing to add. My code example includes example of state updates (look again at dashboardState )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question generic question
Projects
None yet
Development

No branches or pull requests

3 participants