/** * @file ConfidentialButtons.tsx * @description Component that provides functionality to add and remove confidential markings * to PowerPoint slides. The markings appear as text at the bottom of each slide. */ import * as React from "react"; import { Button, makeStyles } from "@fluentui/react-components"; import { ShieldLockRegular, DismissRegular } from "@fluentui/react-icons"; import { useStatusContext } from "../context/StatusContext"; import { useSettings } from "../context/SettingsContext"; import { useCommonStyles } from "./commonStyles"; import { getErrorMessage } from "../types/office-types"; import { MARKER_NAMES } from "../constants"; import { addMarkerToCollections, removeMarkerFromCollections } from "../services/markers"; import { isCancellationRequested, resetCancellation } from "../services/cancellation"; import ConfirmButton from "./ConfirmButton"; /** * Configuration constants for confidential markings */ const CONFIDENTIAL_CONFIG = { // Marking text that appears on slides TEXT: "– Confidential –", // Font configuration FONT: { NAME: "Inter", SIZE: 8, COLOR: "#DA1335" // RGB(218, 19, 53) }, // Text box configuration TEXT_BOX: { HEIGHT: 25, POSITION_FROM_BOTTOM: 23 } }; /** * Component-specific styles */ const useStyles = makeStyles({ buttonGrid: { display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: "8px", width: "100%" }, confidentialButton: { width: "100%", minWidth: 0, padding: "8px 4px", "& span": { justifyContent: "center" } } }); /** * Interface for slide processing results */ interface ProcessingResult { processedSlides: number; errorSlides: number; affectedCount: number; // Either added or removed markings } /** * ConfidentialButtons component provides UI and functionality to add or remove * confidential markings on PowerPoint slides. * * @returns React component */ export const ConfidentialButtons: React.FC = () => { const styles = useStyles(); const commonStyles = useCommonStyles(); const { slideSize } = useSettings(); const { setStatusMessage, setStatusType, isProcessing, setIsProcessing, setProgress } = useStatusContext(); /** * Handles errors and updates the status message * * @param error - The error object * @param operation - The operation being performed (for logging) */ const handleError = (error: unknown, operation: string): void => { setStatusMessage(`Error: ${getErrorMessage(error)}`); setStatusType("error"); console.error(`${operation} error:`, error); }; /** * Updates the status message based on processing results * * @param result - The processing result object * @param isAddOperation - Whether this was an add operation (true) or remove operation (false) */ const updateStatusFromResult = (result: ProcessingResult, isAddOperation: boolean): void => { const { errorSlides, affectedCount } = result; if (affectedCount > 0) { const action = isAddOperation ? "Added" : "Removed"; setStatusMessage(`${action} confidential marking to ${affectedCount} slides.`); setStatusType("success"); } else if (errorSlides > 0) { setStatusMessage(`Failed to ${isAddOperation ? "add" : "remove"} markings. Errors on ${errorSlides} slides.`); setStatusType("error"); } else { if (isAddOperation) { setStatusMessage("No slides found to add confidential marking."); } else { setStatusMessage("No confidential markings found to remove."); } setStatusType(isAddOperation ? "warning" : "info"); } }; /** * Adds confidential markings to all slides in the presentation */ const addConfidentialMarking = async (): Promise => { resetCancellation(); setProgress(null); setIsProcessing(true); try { await PowerPoint.run(async (context) => { // Get all slides in the presentation const slides = context.presentation.slides; slides.load("items"); await context.sync(); if (slides.items.length === 0) { setStatusMessage("No slides found in the presentation."); setStatusType("warning"); return; } const result = await processAddMarkings(context, slides.items); if (isCancellationRequested()) { setStatusMessage("Operation cancelled."); setStatusType("warning"); return; } updateStatusFromResult(result, true); }); } catch (error: unknown) { handleError(error, "Add confidential"); } finally { setIsProcessing(false); setProgress(null); } }; /** * Processes all slides to add confidential markings * * @param context - The PowerPoint API context * @param slides - Array of slides to process * @returns Processing result with counts of processed and error slides */ const processAddMarkings = async ( context: PowerPoint.RequestContext, slides: PowerPoint.Slide[] ): Promise => { const positionFromTop = slideSize.height - CONFIDENTIAL_CONFIG.TEXT_BOX.POSITION_FROM_BOTTOM; const result = await addMarkerToCollections( context, slides.map((slide) => slide.shapes), { name: MARKER_NAMES.confidential, text: CONFIDENTIAL_CONFIG.TEXT, font: { name: CONFIDENTIAL_CONFIG.FONT.NAME, size: CONFIDENTIAL_CONFIG.FONT.SIZE, color: CONFIDENTIAL_CONFIG.FONT.COLOR }, box: { left: 0, top: positionFromTop, width: slideSize.width, height: CONFIDENTIAL_CONFIG.TEXT_BOX.HEIGHT }, horizontalAlignment: PowerPoint.ParagraphHorizontalAlignment.center }, (current, total) => setProgress({ current, total }) ); return { processedSlides: slides.length, errorSlides: 0, affectedCount: result.affected }; }; /** * Removes confidential markings from all slides in the presentation */ const removeConfidentialMarking = async (): Promise => { resetCancellation(); setProgress(null); setIsProcessing(true); try { await PowerPoint.run(async (context) => { // Get all slides in the presentation const slides = context.presentation.slides; slides.load("items"); await context.sync(); if (slides.items.length === 0) { setStatusMessage("No slides found in the presentation."); setStatusType("warning"); return; } const result = await processRemoveMarkings(context, slides.items); if (isCancellationRequested()) { setStatusMessage("Operation cancelled."); setStatusType("warning"); return; } updateStatusFromResult(result, false); }); } catch (error: unknown) { handleError(error, "Remove confidential"); } finally { setIsProcessing(false); setProgress(null); } }; /** * Processes all slides to remove confidential markings * * @param context - The PowerPoint API context * @param slides - Array of slides to process * @returns Processing result with counts of processed and affected slides */ const processRemoveMarkings = async ( context: PowerPoint.RequestContext, slides: PowerPoint.Slide[] ): Promise => { const result = await removeMarkerFromCollections( context, slides.map((slide) => slide.shapes), MARKER_NAMES.confidential ); return { processedSlides: slides.length, errorSlides: 0, affectedCount: result.affected }; }; return (
} disabled={isProcessing} title="Remove Confidential" confirmTitle="Remove confidential markings?" confirmMessage="This removes the confidential marking from every slide in the presentation." confirmLabel="Remove" > Remove
); }; export default ConfidentialButtons;