"use client";

import { useEffect, useState } from "react";

import {
  LAB3_DEFAULT_TERMINAL_PATH,
  fetchCoursewareRuntimeConfig,
} from "~/lib/courseware-runtime";

export function TerminalNavLink() {
  const [terminalPath, setTerminalPath] = useState(LAB3_DEFAULT_TERMINAL_PATH);

  useEffect(() => {
    let isCancelled = false;

    void fetchCoursewareRuntimeConfig()
      .then((runtimeConfig) => {
        if (isCancelled) return;
        setTerminalPath(runtimeConfig.lab3TerminalUrl);
      })
      .catch(() => {
        if (isCancelled) return;
        setTerminalPath(LAB3_DEFAULT_TERMINAL_PATH);
      });

    return () => {
      isCancelled = true;
    };
  }, []);

  return (
    <a
      className="hover:text-[#F89C27]"
      href={terminalPath}
      rel="noreferrer"
      target="_blank"
    >
      Terminal
    </a>
  );
}
