export async function handleOpenPluginDeeplink()

in desktop/flipper-ui-core/src/dispatcher/handleOpenPluginDeeplink.tsx [61:198]


export async function handleOpenPluginDeeplink(
  store: Store,
  query: string,
  trackInteraction: (interaction: DeeplinkInteraction) => void,
) {
  const params = parseOpenPluginParams(query);
  const title = `Opening plugin ${params.pluginId}…`;
  console.debug(`[deeplink] ${title} for with params`, params);

  if (!(await verifyLighthouseAndUserLoggedIn(store, title))) {
    trackInteraction({
      state: 'PLUGIN_LIGHTHOUSE_BAIL',
      plugin: params,
    });
    return;
  }
  console.debug('[deeplink] Cleared Lighthouse and log-in check.');
  await verifyFlipperIsUpToDate(title);
  console.debug('[deeplink] Cleared up-to-date check.');
  const [pluginStatusResult, pluginStatus] = await verifyPluginStatus(
    store,
    params.pluginId,
    title,
  );
  if (!pluginStatusResult) {
    trackInteraction({
      state: 'PLUGIN_STATUS_BAIL',
      plugin: params,
      extra: {pluginStatus},
    });
    return;
  }
  console.debug('[deeplink] Cleared plugin status check:', pluginStatusResult);

  const isDevicePlugin = store
    .getState()
    .plugins.devicePlugins.has(params.pluginId);
  const pluginDefinition = isDevicePlugin
    ? store.getState().plugins.devicePlugins.get(params.pluginId)!
    : store.getState().plugins.clientPlugins.get(params.pluginId)!;
  const deviceOrClient = await selectDevicesAndClient(
    store,
    params,
    title,
    isDevicePlugin,
  );
  console.debug(
    '[deeplink] Selected device and client:',
    deviceOrClient instanceof BaseDevice
      ? deviceOrClient.description
      : deviceOrClient instanceof Client
      ? deviceOrClient.query
      : deviceOrClient,
  );
  if ('errorState' in deviceOrClient) {
    trackInteraction({
      state: deviceOrClient.errorState,
      plugin: params,
    });
    return;
  }
  const client: Client | undefined = isDevicePlugin
    ? undefined
    : (deviceOrClient as Client);
  const device: BaseDevice = isDevicePlugin
    ? (deviceOrClient as BaseDevice)
    : (deviceOrClient as Client).device;
  console.debug('[deeplink] Client: ', client?.query);
  console.debug('[deeplink] Device: ', device?.description);

  // verify plugin supported by selected device / client
  if (isDevicePlugin && !device.supportsPlugin(pluginDefinition)) {
    await Dialog.alert({
      title,
      type: 'error',
      message: `This plugin is not supported by device ${device.displayTitle()}`,
    });
    trackInteraction({
      state: 'PLUGIN_DEVICE_UNSUPPORTED',
      plugin: params,
      extra: {device: device.displayTitle()},
    });
    return;
  }
  console.debug('[deeplink] Cleared device plugin support check.');
  if (!isDevicePlugin && !client!.plugins.has(params.pluginId)) {
    await Dialog.alert({
      title,
      type: 'error',
      message: `This plugin is not supported by client ${client!.query.app}`,
    });
    trackInteraction({
      state: 'PLUGIN_CLIENT_UNSUPPORTED',
      plugin: params,
      extra: {client: client!.query.app},
    });
    return;
  }
  console.debug('[deeplink] Cleared client plugin support check.');

  // verify plugin enabled
  if (isDevicePlugin) {
    // for the device plugins enabling is a bit more complication and should go through the pluginManager
    if (
      !store.getState().connections.enabledDevicePlugins.has(params.pluginId)
    ) {
      store.dispatch(switchPlugin({plugin: pluginDefinition}));
    }
  } else {
    store.dispatch(setPluginEnabled(params.pluginId, client!.query.app));
  }
  console.debug('[deeplink] Cleared plugin enabling.');

  // open the plugin
  if (isDevicePlugin) {
    store.dispatch(
      selectPlugin({
        selectedPlugin: params.pluginId,
        selectedAppId: null,
        selectedDevice: device,
        deepLinkPayload: params.payload,
      }),
    );
  } else {
    store.dispatch(
      selectPlugin({
        selectedPlugin: params.pluginId,
        selectedAppId: client!.id,
        selectedDevice: device,
        deepLinkPayload: params.payload,
      }),
    );
  }
  trackInteraction({
    state: 'PLUGIN_OPEN_SUCCESS',
    plugin: params,
  });
}