export async function addTenantToShareURL()

in public/services/shared-link.ts [20:70]


export async function addTenantToShareURL(core: CoreStart) {
  let tenant = '';
  try {
    tenant = await core.http.get(API_ENDPOINT_MULTITENANCY);
    if (!tenant) {
      tenant = 'global';
    } else if (tenant === '__user__') {
      tenant = 'private';
    }
  } catch (error) {
    console.log(`failed to get user tenant: ${error}`);
    return;
  }
  // Add the tenant to URLs copied from the share panel
  document.addEventListener('copy', (event) => {
    const shareButton = document.querySelector('[data-share-url]');
    const target = document.querySelector('body > span');
    // The copy event listens to Cmd + C too, so we need to make sure
    // that we're actually copied something via the share panel
    if (
      shareButton &&
      target &&
      shareButton.getAttribute('data-share-url') === target.textContent
    ) {
      const originalValue = target.textContent;
      let urlPart = originalValue;

      // We need to figure out where in the value to add the tenant.
      // Since OpenSearchDashboards sometimes adds values that aren't in the current location/url,
      // we need to use the actual input values to do a sanity check.
      try {
        // For the iFrame urls we need to parse out the src
        if (originalValue && originalValue.toLowerCase().indexOf('<iframe') === 0) {
          const regex = /<iframe[^>]*src="([^"]*)"/i;
          const match = regex.exec(originalValue);
          if (match) {
            urlPart = match[1]; // Contains the matched src, [0] contains the string where the match was found
          }
        }

        const newValue = addTenantToURL(urlPart!, originalValue!, tenant);

        if (newValue !== originalValue) {
          target.textContent = newValue;
        }
      } catch (error) {
        // Probably wasn't an url, so we just ignore this
      }
    }
  });
}