All files / components/TabComponent SlackTabComponent.jsx

100% Statements 8/8
100% Branches 10/10
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                        5x             3x                             18x 18x 18x   18x                 18x                 18x                                                                                                                                                                          
import React from "react";
import { Alert } from "react-bootstrap";
import { useBackend } from "main/utils/useBackend";
import SlackUsersTable from "main/components/Slack/SlackUsersTable";
import SlackMissingMembersTable from "main/components/Slack/SlackMissingMembersTable";
import SlackSectionChannelsCard from "main/components/Slack/SlackSectionChannelsCard";
import SlackTeamChannelsCard from "main/components/Slack/SlackTeamChannelsCard";
import SlackPrivateChannelsCard from "main/components/Slack/SlackPrivateChannelsCard";
import { slackAdminUrl } from "main/utils/slackUtils";
 
// Each of these queries makes the backend call the Slack API, so they are not retried on
// failure, and not repeated every time the browser window regains focus.
const slackQueryOptions = {
  retry: false,
  // Stryker disable next-line BooleanLiteral : window focus refetching is not observable in tests
  refetchOnWindowFocus: false,
};
 
function SlackQueryError({ error, testId }) {
  return (
    <Alert variant="danger" data-testid={testId}>
      {error.response?.data?.message ??
        `Error getting information from Slack: ${error}`}
    </Alert>
  );
}
 
export default function SlackTabComponent({
  courseId,
  testIdPrefix,
  slackTeamName,
  slackTeamUrl,
  showSectionChannels,
}) {
  const adminUrl = slackAdminUrl(slackTeamUrl);
  const usersUrl = `/api/courses/slack/users?courseId=${courseId}`;
  const missingUrl = `/api/courses/slack/missing?courseId=${courseId}`;
 
  const { data: users, error: usersError } = useBackend(
    [usersUrl],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: usersUrl },
    [],
    true,
    slackQueryOptions,
  );
 
  const { data: missing, error: missingError } = useBackend(
    [missingUrl],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: missingUrl },
    [],
    true,
    slackQueryOptions,
  );
 
  return (
    <div data-testid={`${testIdPrefix}-slack-tab-component`}>
      <p className="fs-5">
        Slack workspace:{" "}
        <a
          href={slackTeamUrl}
          target="_blank"
          rel="noopener noreferrer"
          data-testid={`${testIdPrefix}-slack-workspace-link`}
        >
          {slackTeamName}
        </a>
        {adminUrl && (
          <a
            href={adminUrl}
            target="_blank"
            rel="noopener noreferrer"
            className="ms-3"
            data-testid={`${testIdPrefix}-slack-admin-link`}
          >
            Admin
          </a>
        )}
      </p>
 
      <h5 data-testid={`${testIdPrefix}-slack-users-heading`}>
        Active Slack users ({users.length})
      </h5>
      <p className="text-muted">
        Course Role shows whether the email of the Slack user matches the
        instructor, a staff member, or a student on the roster of this course.
        Click a column header to sort. If the Email column is empty, the Slack
        app is missing the <code>users:read.email</code> scope.
      </p>
      {usersError ? (
        <SlackQueryError
          error={usersError}
          testId={`${testIdPrefix}-slack-users-error`}
        />
      ) : (
        <SlackUsersTable
          users={users}
          testIdPrefix={`${testIdPrefix}-slack-users-table`}
        />
      )}
 
      <h5
        className="mt-4"
        data-testid={`${testIdPrefix}-slack-missing-heading`}
      >
        Roster students and staff not active in Slack ({missing.length})
      </h5>
      <p className="text-muted">
        Students (other than dropped students) and staff of this course whose
        email does not match an active Slack user.
      </p>
      {missingError ? (
        <SlackQueryError
          error={missingError}
          testId={`${testIdPrefix}-slack-missing-error`}
        />
      ) : (
        <SlackMissingMembersTable
          members={missing}
          testIdPrefix={`${testIdPrefix}-slack-missing-table`}
        />
      )}
 
      {showSectionChannels && (
        <SlackSectionChannelsCard
          courseId={courseId}
          testIdPrefix={`${testIdPrefix}-slack`}
        />
      )}
      <SlackTeamChannelsCard
        courseId={courseId}
        testIdPrefix={`${testIdPrefix}-slack`}
      />
      <SlackPrivateChannelsCard
        courseId={courseId}
        testIdPrefix={`${testIdPrefix}-slack`}
      />
    </div>
  );
}