Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.9 KB
Raw Blame
import { hideImportPromotion } from '@app/features/connector-import/primitives/promotion';
import {
  IntegrationRow,
  SettingsSurface,
} from '@app/features/settings/primitives';
import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { enableSlackArchiveImport } from '@core/constant/featureFlags';
import { useSettingsState } from '@core/constant/SettingsState';
import { useUserId } from '@core/context/user';
import { usePipedreamMcpFlag } from '@core/pipedream/flag';
import { requestConnectApp } from '@core/pipedream/pendingConnect';
import { SLACK_CONNECT_SLUG } from '@core/pipedream/slugs';
import { holdAutomaticReload } from '@core/util/reloadForNewerBuild';
import SlackIcon from '@icon/slack-color.svg';
import {
  invalidateListChannels,
  useListChannelsQuery,
} from '@queries/channel/channels';
import { queryClient } from '@queries/client';
import { revalidateChannelLists } from '@queries/soup/graphql/channel-list-revalidation';
import { invalidateAllSoup } from '@queries/soup/normalized-cache';
import { createConnectionWebsocketEffect } from '@service-connection/websocket';
// ast-grep-ignore: tsx-no-service-client-outside-queries -- Composition only; endpoint calls remain in the query adapter.
import { storageServiceClient } from '@service-storage/client';
import { getGraphqlSoupClient } from '@service-storage/graphql-soup';
import { uploadSlackImport } from '@service-storage/slack-import-upload';
import { type JSX, Show, Suspense } from 'solid-js';
import type { ImportJob } from './context/contracts';
import { ImportProvider } from './context/import-context';
import {
  createArchiveSource,
  protectImportFile,
} from './queries/archive-source';
import { createImportSource } from './queries/import-source';
import { ImportDialog, type ImportDialogTrigger } from './views/import-dialog';

type Props = {
  teamId: string;
  isAdmin: boolean;
  trigger?: ImportDialogTrigger;
  /** Open straight to the archive upload; the host offers channel picking. */
  historyOnly?: boolean;
};

export function SlackImport(props: Props): JSX.Element {
  const flag = useFeatureFlag(enableSlackArchiveImport);
  return (
    <Show when={props.isAdmin && flag().enabled}>
      <Show when={props.teamId} keyed>
        {(teamId) => (
          <Suspense
            fallback={
              // A host-supplied trigger lives elsewhere (the Chat rail card);
              // only the Settings row has a loading placeholder.
              props.trigger ? null : (
                <SettingsSurface>
                  <IntegrationRow
                    icon={<SlackIcon />}
                    title="Slack export"
                    description="Loading Slack import…"
                    muted
                  />
                </SettingsSurface>
              )
            }
          >
            <SlackImportSettings
              teamId={teamId}
              trigger={props.trigger}
              historyOnly={props.historyOnly}
            />
          </Suspense>
        )}
      </Show>
    </Show>
  );
}

function SlackImportSettings(props: {
  teamId: string;
  trigger?: ImportDialogTrigger;
  historyOnly?: boolean;
}): JSX.Element {
  const channels = useListChannelsQuery();
  const userId = useUserId();
  const client = getGraphqlSoupClient();
  const pipedreamEnabled = usePipedreamMcpFlag();
  const { openSettings } = useSettingsState();
  const connectSlack = () => {
    requestConnectApp(SLACK_CONNECT_SLUG, 'import-slack-channels');
    openSettings('Connected');
  };
  function channelHref(id: string): string | undefined {
    if (
      !channels.isSuccess ||
      !channels.data.some((channel) => channel.id === id)
    )
      return undefined;
    return `/app/channel/${encodeURIComponent(id)}`;
  }
  async function onCompleted(job: ImportJob): Promise<void> {
    if (
      job.status === 'completed' ||
      (job.status === 'completed_with_errors' &&
        job.conversations.some(
          (conversation) => conversation.status === 'completed'
        ))
    ) {
      hideImportPromotion(userId(), 'channels');
    }
    invalidateAllSoup();
    await Promise.all([
      invalidateListChannels(),
      revalidateChannelLists(client),
    ]);
  }
  return (
    <ImportProvider
      context={{
        createSource: (inputs) =>
          createImportSource(
            {
              client: storageServiceClient,
              queryClient,
              gatewayEffect: createConnectionWebsocketEffect,
              upload: uploadSlackImport,
            },
            inputs
          ),
        createArchive: createArchiveSource,
        protectFile: () => protectImportFile(holdAutomaticReload),
        newToken: () => crypto.randomUUID(),
      }}
    >
      <ImportDialog
        teamId={props.teamId}
        trigger={props.trigger}
        onConnect={
          pipedreamEnabled() && !props.historyOnly ? connectSlack : undefined
        }
        onCompleted={onCompleted}
        channelHref={channelHref}
      />
    </ImportProvider>
  );
}