All files / src/utils userClass.ts

98.26% Statements 170/173
88% Branches 22/25
100% Functions 5/5
98.26% Lines 170/173

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 1741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 5x 5x 2x 5x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 5x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 5x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 3x 2x 2x 2x 3x  
import { getUserClasses } from "../api/user-api";
import { getAuthContext } from "../services/auth-service";
import { UserClass } from "../api/types/user";
import * as vscode from "vscode";
import { getColorCircle } from ".";
 
let tempSelectedClass: UserClass | null = null;
 
/**
 * Retrieves the currently selected class.
 *
 * @returns The currently selected UserClass object, or `null` if no class is selected.
 */
export function getSelectedClass(): UserClass | null {
  return tempSelectedClass;
}
 
/**
 * Sets the currently selected class.
 *
 * @param cls - The UserClass object to set as selected, or `null` to deselect.
 */
function setSelectedClass(cls: UserClass | null) {
  tempSelectedClass = cls;
}
 
/**
 * Registers the "Select Class" command in the VS Code command palette.
 *
 * Allows users to pick a class from their registered classes, or to navigate
 * to CLOVER if they have no classes registered. Updates the status bar based on the selection.
 *
 * @param context - The extension context for managing subscriptions.
 * @param statusBarItem - The status bar item to update based on class selection.
 */
export function registerClassSelectorCommand(
  context: vscode.ExtensionContext,
  statusBarItem: vscode.StatusBarItem
) {
  const disposable = vscode.commands.registerCommand(
    "your-extension.selectClass",
    async () => {
      const { context: user, error: authError } = await getAuthContext();
 
      if (authError || user === undefined) {
        vscode.window.showErrorMessage(
          `Failed to get user context: ${authError}`
        );
        return;
      }
 
      const { data: userClasses, error: classError } = await getUserClasses(
        user.id
      );
 
      if (
        classError?.includes("No classes found") ||
        !userClasses ||
        userClasses.length === 0
      ) {
        const selection = await vscode.window.showInformationMessage(
          "You have no registered classes. Would you like to register one now?",
          { modal: true },
          "Open CLOVER"
        );
 
        if (selection === "Open CLOVER") {
          vscode.env.openExternal(
            vscode.Uri.parse("https://clover.nickrucinski.com/")
          );
        }
 
        setSelectedClass(null);
        statusBarItem.text = `📘 SELECT CLASS ⌄`;
        statusBarItem.color = "#FF8C00";
        return;
      }
 
      if (classError) {
        vscode.window.showErrorMessage(`Error fetching classes: ${classError}`);
        return;
      }
 
      const picked = await vscode.window.showQuickPick(
        [
          {
            label: "🚫 No class",
            description: "You are not assigned to any class",
          },
          ...userClasses.map((c) => ({
            label: `${getColorCircle(c.classHexColor as string)} ${
              c.classTitle
            }`,
            originalTitle: c.classTitle,
            description: c.classCode,
            color: c.classHexColor,
          })),
        ],
        { placeHolder: "Select the class you are working on" }
      );
 
      if (picked) {
        if (picked.label === "🚫 No class") {
          setSelectedClass(null);
          statusBarItem.text = `📘 SELECT CLASS ⌄`;
        }
 
        const selectedClass = userClasses.find(
          (c) => c.classTitle === (picked as any).originalTitle
        );
        if (selectedClass) {
          setSelectedClass(selectedClass);
          statusBarItem.text = `📘 CLASS: ${selectedClass.classTitle.toUpperCase()}`;
          statusBarItem.color = selectedClass.classHexColor || "#FF8C00";
        }
 
        await updateClassStatus(statusBarItem);
      }
 
      if (tempSelectedClass) {
        statusBarItem.text = `📘 CLASS: ${tempSelectedClass.classTitle.toUpperCase()}`;
        statusBarItem.color = tempSelectedClass.classHexColor;
      } else {
        statusBarItem.text = `📘 SELECT CLASS ⌄`;
        statusBarItem.color = "#FF8C00";
      }
    }
  );
 
  context.subscriptions.push(disposable);
}
/**
 * Sets up and initializes the class selection status bar item.
 *
 * Configures the status bar button that allows users to quickly view or change their active class.
 *
 * @returns A promise that resolves with the created StatusBarItem.
 */
export async function setupClassStatusBarItem(): Promise<vscode.StatusBarItem> {
  const statusBarItem = vscode.window.createStatusBarItem(
    vscode.StatusBarAlignment.Left,
    1
  );
 
  statusBarItem.tooltip = "📘 Click to change the active class";
  statusBarItem.command = "your-extension.selectClass";
  statusBarItem.show();
 
  await updateClassStatus(statusBarItem);
 
  return statusBarItem;
}
 
/**
 * Updates the text and color of the class selection status bar item.
 *
 * Reflects the currently selected class if available, otherwise prompts the user to select a class.
 *
 * @param statusBarItem - The status bar item to update.
 */
async function updateClassStatus(statusBarItem: vscode.StatusBarItem) {
  const selectedClass = getSelectedClass();
 
  if (selectedClass) {
    statusBarItem.text = `📘 CLASS: ${selectedClass.classTitle.toUpperCase()}`;
    if (selectedClass.classHexColor) {
      statusBarItem.color = selectedClass.classHexColor || "#FF8C00";
    }
  } else {
    statusBarItem.text = `📘 SELECT CLASS ⌄`;
    statusBarItem.color = "#FF8C00";
  }
}