All files / src/components/widgets/implementationguide SecurityResourcesWidget.tsx

79.54% Statements 70/88
81.69% Branches 58/71
80.76% Functions 21/26
79.54% Lines 70/88

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566                                            3x 3x             3x 3x                                                                                                     2x                           14x     14x 14x 14x 14x     14x 14x       14x 14x 14x 10x       10x                       11x           13x                         13x             11x     13x 13x 13x 13x 13x         73x     38x     38x 38x   13x     63x 63x                       14x 14x 14x     14x       13x 38x     24x     13x 14x 14x 13x                                                               4x       4x 8x 14x 14x       13x 13x 13x       14x   14x 14x             14x       1x     14x     14x     14x                         13x   11x 11x                                                                                                                                                                                                     11x                 11x                                                                                                                                                                     5x                                 1x                         1x   1x                       3x                                                              
import React, { useCallback, useMemo, useState } from "react";
import { WIDGET_ICONS, WIDGET_TITLES } from "../../../constants/appConstants";
import { SECURITY_RESOURCES_WIDGET_IDS } from "../../../constants/testIds";
import { SECURITY_RESOURCES_TEST_IDS } from "../../../constants/testIds";
import { useCIAContentService } from "../../../hooks/useCIAContentService";
import { SecurityLevel } from "../../../types/cia";
import { SecurityResource } from "../../../types/securityResources";
import { SecurityResourcesWidgetProps } from "../../../types/widget-props";
import {
  isArray,
  isNullish,
  isObject,
  isString,
} from "../../../utils/typeGuards";
import { getWidgetAriaDescription } from "../../../utils/accessibility";
import { WidgetClasses, cn } from "../../../utils/tailwindClassHelpers";
import ResourceCard from "../../common/ResourceCard";
import WidgetContainer from "../../common/WidgetContainer";
import WidgetErrorBoundary from "../../common/WidgetErrorBoundary";
import ImplementationGuidancePanel from "./ImplementationGuidancePanel";
 
// Constants for top resources filtering
const TOP_RESOURCES_PERCENTAGE = 0.5; // 50%
const MIN_TOP_RESOURCES = 5;
 
/**
 * Helper function to extract relevance score from a security resource
 * @param resource - The security resource to extract score from
 * @returns The relevance score, defaulting to 0 if not available
 */
const getResourceRelevanceScore = (resource: SecurityResource): number => {
  return isObject(resource) && "relevance" in resource && typeof resource.relevance === "number"
    ? resource.relevance
    : 0;
};
 
/**
 * Widget that displays security resources and implementation guides
 * 
 * This component provides security practitioners with relevant resources,
 * implementation guides, and best practices to help implement appropriate
 * security controls for the selected security levels. It bridges the gap
 * between security requirements and practical implementation.
 * 
 * ## Features
 * - Dynamic resource filtering based on security levels
 * - Categorized display of resources by CIA component
 * - Support for limiting displayed resources
 * - Top resources filtering for focused guidance
 * - Loading and error states
 * - Accessible and responsive design
 * 
 * ## Business Perspective
 * 
 * This widget accelerates security implementation by providing practitioners
 * with immediately actionable guidance, reducing research time and ensuring
 * best practices are followed. It improves security ROI by helping teams
 * implement controls correctly the first time. 📚
 * 
 * @component
 * 
 * @example
 * ```tsx
 * // Basic usage with uniform security levels
 * <SecurityResourcesWidget
 *   availabilityLevel="Moderate"
 *   integrityLevel="Moderate"
 *   confidentialityLevel="Moderate"
 * />
 * 
 * // Advanced usage with custom configuration
 * <SecurityResourcesWidget
 *   availabilityLevel="High"
 *   integrityLevel="Very High"
 *   confidentialityLevel="Moderate"
 *   maxItems={12}
 *   showTopResourcesOnly={true}
 *   className="border-2 border-gray-200 p-md"
 *   testId="main-security-resources"
 * />
 * ```
 */
const SecurityResourcesWidget: React.FC<SecurityResourcesWidgetProps> = ({
  availabilityLevel,
  integrityLevel,
  confidentialityLevel,
  className = "",
  testId = SECURITY_RESOURCES_TEST_IDS.WIDGET,
  maxItems = 8,
  showTopResourcesOnly = false,
}) => {
  // Use the CIA content service
  const {
    ciaContentService,
    error: serviceError,
    isLoading,
  } = useCIAContentService();
 
  // State for resource filtering and pagination
  const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
  const [searchTerm, setSearchTerm] = useState("");
  const [currentPage, setCurrentPage] = useState(1);
  const [resourcesPerPage, setResourcesPerPage] = useState(maxItems);
 
  // Update resourcesPerPage when maxItems changes
  React.useEffect(() => {
    setResourcesPerPage(maxItems);
  }, [maxItems]);
 
  // Calculate security resources with proper error handling and type safety
  const securityResources = useMemo((): SecurityResource[] => {
    try {
      Eif (isNullish(ciaContentService)) {
        return [];
      }
 
      // Get resources for each security level
      const availabilityResources = isArray(
        ciaContentService.getSecurityResources?.(
          "availability",
          availabilityLevel
        )
      )
        ? ciaContentService.getSecurityResources?.(
            "availability",
            availabilityLevel
          )
        : [];
 
      const integrityResources = isArray(
        ciaContentService.getSecurityResources?.("integrity", integrityLevel)
      )
        ? ciaContentService.getSecurityResources?.("integrity", integrityLevel)
        : [];
 
      const confidentialityResources = isArray(
        ciaContentService.getSecurityResources?.(
          "confidentiality",
          confidentialityLevel
        )
      )
        ? ciaContentService.getSecurityResources?.(
            "confidentiality",
            confidentialityLevel
          )
        : [];
 
      // Combine all resources
      const allResources = [
        ...(availabilityResources || []),
        ...(integrityResources || []),
        ...(confidentialityResources || []),
      ];
 
      // Deduplicate resources by URL (or title if URL is not available)
      const uniqueResources: SecurityResource[] = [];
      const resourceKeys = new Set();
 
      allResources.forEach((resource) => {
        const key = isString(resource.url) ? resource.url : resource.title;
        Eif (!resourceKeys.has(key)) {
          resourceKeys.add(key);
          uniqueResources.push(resource);
        }
      });
 
      // Sort by relevance score if available, otherwise by title
      return uniqueResources.sort((a, b) => {
        // Extract scores using helper (0 is default for missing scores)
        const aScore = getResourceRelevanceScore(a);
        const bScore = getResourceRelevanceScore(b);
 
        // Sort by score (higher first), then by title
        Eif (aScore !== bScore) {
          return bScore - aScore;
        }
        return a.title.localeCompare(b.title);
      });
    } catch (err) {
      console.error("Error getting security resources:", err);
      return [];
    }
  }, [
    ciaContentService,
    availabilityLevel,
    integrityLevel,
    confidentialityLevel,
  ]);
 
  // Get unique resource categories for filtering
  const resourceCategories = useMemo(() => {
    const categories = new Set<string>();
    securityResources.forEach((resource) => {
      Eif (resource.category) {
        categories.add(resource.category);
      }
    });
    return Array.from(categories).sort();
  }, [securityResources]);
 
  // Filter resources based on category, search term, and top resources flag
  const filteredResources = useMemo(() => {
    let filtered = securityResources;
 
    // Filter to show only top priority resources if requested
    if (showTopResourcesOnly) {
      // Filter resources with high relevance scores
      // Shows top 50% with a minimum of 5 resources (or all if less than 10 total)
      const sortedByRelevance = [...filtered].sort((a, b) => {
        const aScore = getResourceRelevanceScore(a);
        const bScore = getResourceRelevanceScore(b);
        return bScore - aScore;
      });
      
      // Take top percentage of resources with a minimum to ensure meaningful results
      const topCount = Math.max(
        Math.ceil(sortedByRelevance.length * TOP_RESOURCES_PERCENTAGE), 
        MIN_TOP_RESOURCES
      );
      filtered = sortedByRelevance.slice(0, topCount);
    }
 
    // Apply category filter
    if (selectedCategory) {
      filtered = filtered.filter(
        (resource) => resource.category === selectedCategory
      );
    }
 
    // Apply search filter
    if (searchTerm.trim()) {
      const normalizedSearch = searchTerm.toLowerCase().trim();
      filtered = filtered.filter(
        (resource) =>
          resource.title.toLowerCase().includes(normalizedSearch) ||
          resource.description?.toLowerCase().includes(normalizedSearch) ||
          (resource.tags &&
            resource.tags.some((tag) =>
              tag.toLowerCase().includes(normalizedSearch)
            ))
      );
    }
 
    return filtered;
  }, [securityResources, selectedCategory, searchTerm, showTopResourcesOnly]);
 
  // Paginate resources
  const currentResources = useMemo(() => {
    const indexOfLastResource = currentPage * resourcesPerPage;
    const indexOfFirstResource = indexOfLastResource - resourcesPerPage;
    return filteredResources.slice(indexOfFirstResource, indexOfLastResource);
  }, [filteredResources, currentPage, resourcesPerPage]);
 
  // Handle category selection
  const handleCategorySelect = useCallback((category: string | null) => {
    setSelectedCategory(category);
    setCurrentPage(1); // Reset to first page when changing filters
  }, []);
 
  // Handle search input change
  const handleSearchChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      setSearchTerm(e.target.value);
      setCurrentPage(1); // Reset to first page when changing search
    },
    []
  );
 
  // Handle page change
  const handlePageChange = useCallback((pageNumber: number) => {
    setCurrentPage(pageNumber);
  }, []);
 
  // Determine total pages
  const totalPages = Math.ceil(filteredResources.length / resourcesPerPage);
 
  // Get implementation guides - component-specific guidance
  const implementationGuides = useMemo(() => {
    try {
      if (isNullish(ciaContentService)) {
        return [];
      }
 
      return [
        ciaContentService.getTechnicalImplementation?.(
          "availability",
          availabilityLevel
        ),
        ciaContentService.getTechnicalImplementation?.(
          "integrity",
          integrityLevel
        ),
        ciaContentService.getTechnicalImplementation?.(
          "confidentiality",
          confidentialityLevel
        ),
      ].filter((guide) => guide !== undefined);
    } catch (err) {
      console.error("Error getting implementation guides:", err);
      return [];
    }
  }, [
    ciaContentService,
    availabilityLevel,
    integrityLevel,
    confidentialityLevel,
  ]);
 
  return (
    <WidgetErrorBoundary widgetName="Security Resources">
      <WidgetContainer
        title={WIDGET_TITLES.SECURITY_RESOURCES || "Security Resources"}
        icon={WIDGET_ICONS.SECURITY_RESOURCES || "📚"}
        className={className}
        testId={testId}
        isLoading={isLoading}
        error={serviceError}
      >
      <div 
        className="p-sm sm:p-md"
        role="region"
        aria-label={getWidgetAriaDescription(
          "Security Resources",
          "Curated security resources and implementation guides for selected CIA security levels"
        )}
      >
        {/* Widget introduction */}
        <section 
          className={cn(
            WidgetClasses.section,
            "p-sm rounded-lg",
            "bg-blue-50 dark:bg-blue-900 dark:bg-opacity-20"
          )}
          aria-labelledby="resources-intro-heading"
        >
          <h2 id="resources-intro-heading" className="sr-only">Security Resources Introduction</h2>
          <p className={WidgetClasses.body}>
            This widget provides curated security resources to help implement
            controls that align with your selected security levels across the
            CIA triad.
          </p>
        </section>
 
        <div className={WidgetClasses.flexRow}>
          {/* Filters and search - left column on larger screens */}
          <aside 
            className="md:w-1/4"
            aria-label="Resource filters and search"
          >
            <div className="mb-md">
              <label
                htmlFor="resource-search"
                className={cn(WidgetClasses.body, "block font-medium mb-xs")}
              >
                Search Resources
              </label>
              <div className="relative">
                <input
                  id="resource-search"
                  type="text"
                  className={cn(
                    "w-full px-3 py-2 border border-gray-300 dark:border-gray-700 rounded-md dark:bg-gray-800",
                    WidgetClasses.focusVisible
                  )}
                  placeholder="Search by title, description..."
                  value={searchTerm}
                  onChange={handleSearchChange}
                  data-testid={SECURITY_RESOURCES_WIDGET_IDS.input('search')}
                  aria-label="Search security resources by title or description"
                />
                <span 
                  className="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500 dark:text-gray-400"
                  aria-hidden="true"
                >
                  🔍
                </span>
              </div>
            </div>
 
            {resourceCategories.length > 0 && (
              <nav 
                className="mb-md"
                aria-label="Resource categories"
              >
                <h3 className={cn(WidgetClasses.body, "font-medium mb-sm")}>Categories</h3>
                <ul 
                  className="space-y-2"
                  role="list"
                >
                  <li>
                    <button
                      className={cn(
                        "w-full text-left px-3 py-2 rounded-md",
                        WidgetClasses.textResponsive,
                        selectedCategory === null
                          ? "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:bg-opacity-30 dark:text-blue-300"
                          : "bg-gray-100 hover:bg-gray-200 text-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                      )}
                      onClick={() => handleCategorySelect(null)}
                      data-testid={SECURITY_RESOURCES_WIDGET_IDS.button('category-all')}
                      aria-pressed={selectedCategory === null}
                    >
                    All Resources
                  </button>
                  </li>
 
                  {resourceCategories.map((category, index) => (
                    <li key={category}>
                      <button
                        className={cn(
                          "w-full text-left px-3 py-2 rounded-md",
                          WidgetClasses.textResponsive,
                          selectedCategory === category
                            ? "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:bg-opacity-30 dark:text-blue-300"
                            : "bg-gray-100 hover:bg-gray-200 text-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                        )}
                        onClick={() => handleCategorySelect(category)}
                        data-testid={SECURITY_RESOURCES_WIDGET_IDS.button(`category-${index}`)}
                        aria-pressed={selectedCategory === category}
                      >
                        {category}
                      </button>
                    </li>
                  ))}
                </ul>
              </nav>
            )}
 
            {/* Implementation Guidelines */}
            <section className="mb-md" aria-labelledby="implementation-guidelines-heading">
              <h3 id="implementation-guidelines-heading" className={cn(WidgetClasses.body, "font-medium mb-sm")}>
                Implementation Guidelines
              </h3>
              <div className={cn(WidgetClasses.card, "bg-gray-50 dark:bg-gray-800 shadow-none")}>
                <p className={cn(WidgetClasses.body, "mb-sm font-medium")}>Selected Security Levels:</p>
                <dl className={cn(WidgetClasses.labelNormal, "mb-sm")}>
                  <div className="flex justify-between">
                    <dt>Confidentiality:</dt>
                    <dd className="font-medium">{confidentialityLevel}</dd>
                  </div>
                  <div className="flex justify-between mb-xs">
                    <dt>Integrity:</dt>
                    <dd className="font-medium">{integrityLevel}</dd>
                  </div>
                  <div className="flex justify-between mb-xs">
                    <dt>Availability:</dt>
                    <dd className="font-medium">{availabilityLevel}</dd>
                  </div>
                </dl>
 
                <p className="mb-sm text-xs text-gray-600 dark:text-gray-400">
                  Focus on implementing controls that satisfy all three
                  components for a balanced security posture.
                </p>
              </div>
            </section>
          </aside>
 
          {/* Resources grid - right column on larger screens */}
          <div className="md:w-3/4">
            {/* Resources list */}
            <div className="mb-md">
              <div className="flex justify-between items-center mb-sm">
                <h3 className="text-lg font-medium">Security Resources</h3>
                <div className="text-sm text-gray-600 dark:text-gray-400">
                  {filteredResources.length}{" "}
                  {filteredResources.length === 1 ? "resource" : "resources"}{" "}
                  found
                </div>
              </div>
 
              {/* Empty state */}
              {filteredResources.length === 0 && (
                <div
                  className="p-md bg-gray-50 dark:bg-gray-800 rounded-lg text-center text-gray-500 dark:text-gray-400"
                  data-testid={SECURITY_RESOURCES_WIDGET_IDS.label('no-resources')}
                >
                  <p className="mb-sm">No resources found.</p>
                  <p className="text-sm">
                    {searchTerm
                      ? "Try adjusting your search terms or clearing filters."
                      : "Resources will appear here when available."}
                  </p>
                </div>
              )}
 
              {/* Resources grid */}
              {filteredResources.length > 0 && (
                <div className="grid grid-cols-1 lg:grid-cols-2 gap-sm">
                  {currentResources.map((resource, index) => (
                    <ResourceCard
                      key={`${resource.url || ""}-${index}`}
                      resource={resource}
                      testId={`${testId}-resource-${index}`}
                    />
                  ))}
                </div>
              )}
 
              {/* Pagination controls */}
              {totalPages > 1 && (
                <div className="mt-md flex justify-center">
                  <nav
                    className="relative z-0 inline-flex rounded-md shadow-sm -space-x-px"
                    aria-label="Pagination"
                  >
                    <button
                      onClick={() => handlePageChange(currentPage - 1)}
                      disabled={currentPage === 1}
                      className={`relative inline-flex items-center px-2 py-2 rounded-l-md border ${
                        currentPage === 1
                          ? "bg-gray-100 text-gray-400 dark:bg-gray-800 dark:text-gray-600 cursor-not-allowed"
                          : "bg-white text-gray-500 hover:bg-gray-50 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                      } text-sm font-medium`}
                    >
                      Previous
                    </button>
 
                    {/* Page numbers */}
                    {Array.from({ length: totalPages }).map((_, index) => (
                      <button
                        key={index}
                        onClick={() => handlePageChange(index + 1)}
                        className={`relative inline-flex items-center px-4 py-2 border text-sm font-medium ${
                          currentPage === index + 1
                            ? "z-10 bg-blue-50 border-blue-500 text-blue-600 dark:bg-blue-900 dark:bg-opacity-30 dark:text-blue-300"
                            : "bg-white text-gray-500 hover:bg-gray-50 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                        }`}
                      >
                        {index + 1}
                      </button>
                    ))}
 
                    <button
                      onClick={() => handlePageChange(currentPage + 1)}
                      disabled={currentPage === totalPages}
                      className={`relative inline-flex items-center px-2 py-2 rounded-r-md border ${
                        currentPage === totalPages
                          ? "bg-gray-100 text-gray-400 dark:bg-gray-800 dark:text-gray-600 cursor-not-allowed"
                          : "bg-white text-gray-500 hover:bg-gray-50 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                      } text-sm font-medium`}
                    >
                      Next
                    </button>
                  </nav>
                </div>
              )}
            </div>
 
            {/* Implementation Guidance Panel */}
            <ImplementationGuidancePanel
              implementationGuides={implementationGuides}
              availabilityLevel={availabilityLevel}
              integrityLevel={integrityLevel}
              confidentialityLevel={confidentialityLevel}
            />
          </div>
        </div>
      </div>
    </WidgetContainer>
    </WidgetErrorBoundary>
  );
};
 
export default SecurityResourcesWidget;