Skip to content

The open alternative to Salesforce, designed for AI.

10.3 KB
Raw Blame
import 'twenty-ui/style.css';
import 'twenty-ui/theme-dark.css';
import 'twenty-ui/theme-light.css';

import styled from '@emotion/styled';
import { isNonEmptyString } from '@sniptt/guards';
import { useState } from 'react';
import { enqueueSnackbar, useColorScheme } from 'twenty-sdk/front-component';
import { isDefined } from 'twenty-sdk/utils';
import { Callout } from 'twenty-ui/components/feedback';
import { Button } from 'twenty-ui/primitives/input';
import { Section } from 'twenty-ui/components/layout';
import { ThemeProvider, themeCssVariables } from 'twenty-ui/theme';

import { SlackUserLinkForm } from 'src/front-components/components/SlackUserLinkForm';
import { SlackUserLinksList } from 'src/front-components/components/SlackUserLinksList';
import { SlackUserLinksSettingsSkeleton } from 'src/front-components/components/SlackUserLinksSettingsSkeleton';
import { UnlinkedSlackUsersList } from 'src/front-components/components/UnlinkedSlackUsersList';
import { useCanManageSlackUserLinks } from 'src/front-components/hooks/use-can-manage-slack-user-links';
import { useMatchSlackUserLinks } from 'src/front-components/hooks/use-match-slack-user-links';
import { useSlackConnectionStatus } from 'src/front-components/hooks/use-slack-connection-status';
import { useRemoveSlackUserLink } from 'src/front-components/hooks/use-remove-slack-user-link';
import { useResendSlackUserLinkConsent } from 'src/front-components/hooks/use-resend-slack-user-link-consent';
import { useSlackUserLinks } from 'src/front-components/hooks/use-slack-user-links';
import { useUnlinkedSlackUsers } from 'src/front-components/hooks/use-unlinked-slack-users';
import { type SlackUserLinkRecord } from 'src/front-components/types/slack-user-link-record.type';
import { SLACK_CONNECTION_HEALTH } from 'src/logic-functions/constants/slack-connection-health';

const StyledContainer = styled.div`
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: ${() => themeCssVariables.spacing[8]};
  width: 100%;
`;

const StyledMatchAction = styled.div`
  align-items: center;
  display: flex;
  gap: ${() => themeCssVariables.spacing[3]};
  justify-content: flex-end;
  padding-top: ${() => themeCssVariables.spacing[2]};
`;

const StyledMatchSummary = styled.span`
  color: ${() => themeCssVariables.font.color.secondary};
  font-family: ${() => themeCssVariables.font.family};
  font-size: ${() => themeCssVariables.font.size.xs};
`;

const StyledDisclosure = styled.div`
  align-self: flex-start;
`;

const StyledCenteredState = styled.div`
  align-items: center;
  box-sizing: border-box;
  color: ${() => themeCssVariables.font.color.tertiary};
  display: flex;
  font-family: ${() => themeCssVariables.font.family};
  font-size: ${() => themeCssVariables.font.size.sm};
  height: 100%;
  justify-content: center;
  padding: ${() => themeCssVariables.spacing[4]};
  width: 100%;
`;

const SlackUserLinksSettingsContent = () => {
  const { canManage, isPermissionLoading } = useCanManageSlackUserLinks();
  const {
    isSlackConnected,
    installedSlackTeamId,
    connectionHealth,
    hasRosterMatchFailed,
    isConnectionStatusLoading,
  } = useSlackConnectionStatus();
  const {
    slackUserLinks,
    isSlackUserLinksLoading,
    hasLoadedSlackUserLinks,
    errorMessage,
    hasMoreSlackUserLinks,
    refetchSlackUserLinks,
  } = useSlackUserLinks();
  const { removeSlackUserLink, removingLinkId } = useRemoveSlackUserLink();
  const { resendConsent, resendingLinkId } = useResendSlackUserLinkConsent();
  const {
    unlinkedSlackUsers,
    hasMoreUnlinkedSlackUsers,
    isUnlinkedSlackUsersLoading,
    hasLoadedUnlinkedSlackUsers,
    unlinkedErrorMessage,
    refetchUnlinkedSlackUsers,
  } = useUnlinkedSlackUsers({ isEnabled: canManage });
  const { matchSlackUserLinks, isMatching } = useMatchSlackUserLinks();
  const [matchSummary, setMatchSummary] = useState<string | undefined>(
    undefined,
  );
  const [hasLastMatchRunFailed, setHasLastMatchRunFailed] = useState<
    boolean | undefined
  >(undefined);
  const [isManualFormOpen, setIsManualFormOpen] = useState(false);

  const handleLinkSaved = async () => {
    await Promise.all([refetchSlackUserLinks(), refetchUnlinkedSlackUsers()]);
  };

  const handleManualLinkSaved = async () => {
    setIsManualFormOpen(false);
    await handleLinkSaved();
  };

  const handleMatchByEmail = async () => {
    const result = await matchSlackUserLinks();

    if (!result.success) {
      setMatchSummary(undefined);
      setHasLastMatchRunFailed(true);
      enqueueSnackbar({
        message: isNonEmptyString(result.error) ? result.error : result.message,
        variant: 'error',
      });

      return;
    }

    setMatchSummary(result.message);
    setHasLastMatchRunFailed(result.failedCount > 0);
    await handleLinkSaved();
  };

  const handleRemove = async (slackUserLink: SlackUserLinkRecord) => {
    const result = await removeSlackUserLink(slackUserLink.id);

    enqueueSnackbar({
      message: isNonEmptyString(result.error) ? result.error : result.message,
      variant: result.success ? 'success' : 'error',
    });

    if (result.success) {
      await handleLinkSaved();
    }
  };

  const handleResend = async (slackUserLink: SlackUserLinkRecord) => {
    if (
      !isNonEmptyString(slackUserLink.slackTeamId) ||
      !isNonEmptyString(slackUserLink.slackUserId)
    ) {
      enqueueSnackbar({
        message:
          'This link is missing its Slack ids, so the request cannot be resent.',
        variant: 'error',
      });

      return;
    }

    const result = await resendConsent({
      id: slackUserLink.id,
      slackTeamId: slackUserLink.slackTeamId,
      slackUserId: slackUserLink.slackUserId,
    });

    enqueueSnackbar({
      message: isNonEmptyString(result.error) ? result.error : result.message,
      variant: result.success ? 'success' : 'error',
    });

    if (result.success) {
      await refetchSlackUserLinks();
    }
  };

  const isConnectionBroken =
    isDefined(connectionHealth) &&
    connectionHealth !== SLACK_CONNECTION_HEALTH.OK;

  if (isConnectionStatusLoading || isPermissionLoading) {
    return <SlackUserLinksSettingsSkeleton />;
  }

  // A broken connection is reported by the app health banner, which stays
  // visible next to the connection itself; the tools below need a working one.
  if (!isSlackConnected || isConnectionBroken) {
    return null;
  }

  if (!hasLoadedSlackUserLinks || (canManage && !hasLoadedUnlinkedSlackUsers)) {
    return <SlackUserLinksSettingsSkeleton />;
  }

  const shouldWarnAboutRosterMatch =
    canManage && (hasLastMatchRunFailed ?? hasRosterMatchFailed);

  return (
    <StyledContainer>
      {!canManage && (
        <Callout
          status="warning"
          title="You need the roles permission"
          description="Only members with the roles permission can create or change Slack user links. You can review the existing links below."
        />
      )}
      {shouldWarnAboutRosterMatch && (
        <Callout
          status="warning"
          title="Email auto-link did not finish"
          description="The last automatic email match failed before linking everyone. Press Auto-link by email below to run it again."
        />
      )}
      {canManage && (
        <Section.Root>
          <Section.Header
            title="Unlinked Slack users"
            description="Only Slack users linked to a workspace member can use the assistant. Pick a workspace member on a row to link them in place, or auto-link everyone whose Slack email matches a workspace member."
          />
          {isUnlinkedSlackUsersLoading && unlinkedSlackUsers.length === 0 ? (
            <StyledCenteredState>
              Loading unlinked Slack users…
            </StyledCenteredState>
          ) : isDefined(unlinkedErrorMessage) ? (
            <StyledCenteredState>{unlinkedErrorMessage}</StyledCenteredState>
          ) : (
            <UnlinkedSlackUsersList
              unlinkedSlackUsers={unlinkedSlackUsers}
              hasMore={hasMoreUnlinkedSlackUsers}
              onLinkSaved={handleLinkSaved}
            />
          )}
          <StyledMatchAction>
            {isDefined(matchSummary) && (
              <StyledMatchSummary>{matchSummary}</StyledMatchSummary>
            )}
            <Button
              type="button"
              size="sm"
              variant="outline"
              disabled={isMatching}
              onClick={handleMatchByEmail}
            >
              {isMatching ? 'Auto-linking…' : 'Auto-link by email'}
            </Button>
          </StyledMatchAction>
        </Section.Root>
      )}
      <Section.Root>
        <Section.Header
          title="Slack user links"
          description="Each link maps a Slack account to the workspace member whose permissions the assistant borrows."
        />
        {isSlackUserLinksLoading && slackUserLinks.length === 0 ? (
          <StyledCenteredState>Loading links…</StyledCenteredState>
        ) : isDefined(errorMessage) ? (
          <StyledCenteredState>{errorMessage}</StyledCenteredState>
        ) : (
          <SlackUserLinksList
            slackUserLinks={slackUserLinks}
            canManage={canManage}
            installedSlackTeamId={installedSlackTeamId}
            hasMore={hasMoreSlackUserLinks}
            onRemove={handleRemove}
            onResend={handleResend}
            removingLinkId={removingLinkId}
            resendingLinkId={resendingLinkId}
          />
        )}
      </Section.Root>
      {canManage &&
        (isManualFormOpen ? (
          <SlackUserLinkForm
            existingLinks={slackUserLinks}
            onLinkSaved={handleManualLinkSaved}
          />
        ) : (
          <StyledDisclosure>
            <Button
              type="button"
              size="sm"
              variant="outline"
              onClick={() => setIsManualFormOpen(true)}
            >
              Link someone not listed above
            </Button>
          </StyledDisclosure>
        ))}
    </StyledContainer>
  );
};

// twenty-ui components and this app's styled rules read every token off
// ThemeContext as a var() reference. The sandbox document does not inherit the
// host stylesheet, so the theme variable sheets are imported here for the
// provider to resolve them against.
export const SlackUserLinksSettings = () => {
  const colorScheme = useColorScheme();

  return (
    <ThemeProvider colorScheme={colorScheme}>
      <SlackUserLinksSettingsContent />
    </ThemeProvider>
  );
};