Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

10.8 KB
Raw Blame
<div id="feedbackModal" class="fixed inset-0 z-[1000] hidden">
  <div class="fixed inset-0 bg-black/50 transition-opacity"></div>
  <div class="fixed inset-0 flex items-center justify-center p-4">
    <div
      class="bg-white rounded-xl shadow-lg max-w-[670px] w-full h-[390px] p-6 m-4 overflow-auto sm:h-auto"
    >
      <div class="flex justify-between items-center">
        <h2 class="text-xl font-semibold text-[#1E1E1E] mb-2">Leave Feedback</h2>
        <button
          class="__btnClose bg-transparent rounded-lg hover:text-gray-900 hover:bg-gray-100 p-2 -mr-2 -mt-2"
          id="cancelFeedback"
          aria-label="Close modal"
        >
          <svg
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M20.7457 3.32851C20.3552 2.93798 19.722 2.93798 19.3315 3.32851L12.0371 10.6229L4.74275 3.32851C4.35223 2.93798 3.71906 2.93798 3.32854 3.32851C2.93801 3.71903 2.93801 4.3522 3.32854 4.74272L10.6229 12.0371L3.32856 19.3314C2.93803 19.722 2.93803 20.3551 3.32856 20.7457C3.71908 21.1362 4.35225 21.1362 4.74277 20.7457L12.0371 13.4513L19.3315 20.7457C19.722 21.1362 20.3552 21.1362 20.7457 20.7457C21.1362 20.3551 21.1362 19.722 20.7457 19.3315L13.4513 12.0371L20.7457 4.74272C21.1362 4.3522 21.1362 3.71903 20.7457 3.32851Z"
              fill="#0F0F0F"
            />
          </svg>
        </button>
      </div>
      <p class="text-[14px] leading-[20px] text-[#1E1E1E] mb-4">
        We'd love to hear what went well or how we can improve the product experience.
      </p>
      <textarea
        id="feedbackText"
        rows="6"
        class="w-full bg-white border text-gray-900 rounded outline-none focus:outline-none focus:ring-0 focus:ring-offset-0 focus:ring-shadow-none text-sm font-normal placeholder:text-sm placeholder:font-normal py-2 px-3 border-gray-300 border-b-gray-500 focus:border-b-2 focus:border-b-blue-500 focus-visible:border-b-2 focus-visible:border-b-blue-500 mb-4 resize-none"
        placeholder="Your Feedback"
      ></textarea>
      <div class="flex gap-2 mb-6">
        <button
          class="w-10 h-10 rounded-lg border border-[#D0D5DD] flex items-center justify-center hover:border-smyth-blue hover:bg-[#F5FAFF] transition-all"
          data-emoji="sad"
        >
          <svg
            width="20"
            height="20"
            viewBox="0 0 20 20"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M10 18.333c4.602 0 8.333-3.73 8.333-8.333 0-4.602-3.73-8.333-8.333-8.333C5.398 1.667 1.667 5.398 1.667 10c0 4.602 3.73 8.333 8.333 8.333Z"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
            <path
              d="M6.667 13.333s1.666-1.666 3.333-1.666c1.667 0 3.333 1.666 3.333 1.666M7.5 7.5h.008M12.5 7.5h.008"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
          </svg>
        </button>
        <button
          class="w-10 h-10 rounded-lg border border-[#D0D5DD] flex items-center justify-center hover:border-smyth-blue hover:bg-[#F5FAFF] transition-all"
          data-emoji="neutral"
        >
          <svg
            width="20"
            height="20"
            viewBox="0 0 20 20"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M10 18.333c4.602 0 8.333-3.73 8.333-8.333 0-4.602-3.73-8.333-8.333-8.333C5.398 1.667 1.667 5.398 1.667 10c0 4.602 3.73 8.333 8.333 8.333Z"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
            <path
              d="M7.5 7.5h.008M12.5 7.5h.008M6.667 11.667h6.666"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
          </svg>
        </button>
        <button
          class="w-10 h-10 rounded-lg border border-[#D9D9D9] flex items-center justify-center hover:border-smyth-blue hover:bg-[#F5FAFF] transition-all"
          data-emoji="happy"
        >
          <svg
            width="20"
            height="20"
            viewBox="0 0 20 20"
            fill="none"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M10 18.333c4.602 0 8.333-3.73 8.333-8.333 0-4.602-3.73-8.333-8.333-8.333C5.398 1.667 1.667 5.398 1.667 10c0 4.602 3.73 8.333 8.333 8.333Z"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
            <path
              d="M7.5 7.5h.008M12.5 7.5h.008M13.333 11.667s-1.666 1.666-3.333 1.666c-1.667 0-3.333-1.666-3.333-1.666"
              stroke="#667085"
              stroke-width="1.67"
              stroke-linecap="round"
              stroke-linejoin="round"
            />
          </svg>
        </button>
      </div>
      <div class="flex justify-end gap-3">
        <button
          id="submitFeedback"
          class="flex items-center justify-center font-normal border border-solid text-base px-8 py-2 text-center rounded-sm transition-all duration-200 outline-none focus:outline-none focus:ring-0 focus:ring-offset-0 focus:ring-shadow-none bg-smythos-blue-500 text-white border-transparent hover:bg-smyth-blue disabled:opacity-50 disabled:cursor-not-allowed"
          disabled
        >
          <span>Send</span>
          <div id="submitLoader" class="hidden ml-2">
            <svg
              class="animate-spin h-4 w-4"
              xmlns="http://www.w3.org/2000/svg"
              fill="none"
              viewBox="0 0 24 24"
            >
              <circle
                class="opacity-25"
                cx="12"
                cy="12"
                r="10"
                stroke="currentColor"
                stroke-width="4"
              ></circle>
              <path
                class="opacity-75"
                fill="currentColor"
                d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
              ></path>
            </svg>
          </div>
        </button>
      </div>
    </div>
  </div>
</div>

<script>
  const feedbackBtn = document.getElementById('feedback-button');
  const feedbackModal = document.getElementById('feedbackModal');
  const cancelFeedback = document.getElementById('cancelFeedback');
  const submitFeedback = document.getElementById('submitFeedback');
  const emojiButtons = document.querySelectorAll('[data-emoji]');
  const feedbackTextarea = document.getElementById('feedbackText');
  const userEmail = '<%= user?.email || "[email protected]" %>';

  let selectedEmoji = null;

  function updateSubmitButton() {
    const hasText = feedbackTextarea?.value?.trim();
    submitFeedback.disabled = !(hasText && selectedEmoji);
  }

  function openFeedbackModal() {
    feedbackModal?.classList.remove('hidden');
    updateSubmitButton();
  }

  function closeFeedbackModal() {
    feedbackModal?.classList.add('hidden');
    resetFeedbackForm();
  }

  function resetFeedbackForm() {
    if (feedbackTextarea) feedbackTextarea.value = '';
    selectedEmoji = null;
    emojiButtons?.forEach((btn) => {
      btn.classList.remove('border-[#84CAFF]', 'bg-[#F5FAFF]', 'shadow-[0_0_0_4px_#EBF5FF]');
    });
    updateSubmitButton();
  }

  async function submitToHubSpot(feedbackData) {
    try {
      const response = await fetch('/api/page/feedback/submit', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          feedbackData,
          userEmail,
        }),
      });

      if (!response.ok) {
        throw new Error('Failed to submit feedback');
      }

      const result = await response.json();
      return result;
    } catch (error) {
      console.error('Error submitting feedback:', error);
      return { success: false, error: error.message };
    }
  }

  function setLoadingState(isLoading) {
    const submitBtn = document.getElementById('submitFeedback');
    const cancelBtn = document.getElementById('cancelFeedback');
    const submitLoader = document.getElementById('submitLoader');
    const submitText = submitBtn?.querySelector('span');

    if (isLoading) {
      if (submitBtn) submitBtn.disabled = true;
      if (cancelBtn) cancelBtn.disabled = true;
      submitLoader?.classList.remove('hidden');
      if (submitText) submitText.textContent = 'Sending...';
    } else {
      if (cancelBtn) cancelBtn.disabled = false;
      submitLoader?.classList.add('hidden');
      if (submitText) submitText.textContent = 'Send';
      updateSubmitButton();
    }
  }

  function validateFeedback(text, emoji) {
    const errors = [];
    if (!text?.trim()) errors.push('Please enter your feedback');
    if (!emoji) errors.push('Please select an emoji rating');
    return errors;
  }

  feedbackBtn?.addEventListener('click', openFeedbackModal);
  cancelFeedback?.addEventListener('click', closeFeedbackModal);

  submitFeedback?.addEventListener('click', async () => {
    const feedbackText = feedbackTextarea?.value || '';
    const errors = validateFeedback(feedbackText, selectedEmoji);

    if (errors.length > 0) {
      if (typeof errorToast === 'function') {
        errorToast(`${errors.join(' and ')}`);
      }
      return;
    }

    const feedbackData = {
      text: feedbackText,
      emoji: selectedEmoji,
    };

    setLoadingState(true);

    const result = await submitToHubSpot(feedbackData);

    setLoadingState(false);

    if (result && result.success) {
      closeFeedbackModal();
      if (typeof successToast === 'function') {
        successToast('Thank you for your feedback!');
      }
      console.log('Feedback submitted successfully to HubSpot');
    } else {
      if (typeof errorToast === 'function') {
        errorToast('Failed to submit feedback. Please try again.');
      }
      console.error('Failed to submit feedback to HubSpot');
    }
  });

  emojiButtons?.forEach((button) => {
    button?.addEventListener('click', () => {
      emojiButtons?.forEach((btn) => {
        btn.classList.remove('border-[#84CAFF]', 'bg-[#F5FAFF]', 'shadow-[0_0_0_4px_#EBF5FF]');
      });
      button.classList.add('border-[#84CAFF]', 'bg-[#F5FAFF]', 'shadow-[0_0_0_4px_#EBF5FF]');
      selectedEmoji = button.dataset?.emoji;
      updateSubmitButton();
    });
  });

  feedbackTextarea?.addEventListener('input', updateSubmitButton);

  feedbackModal?.addEventListener('click', (event) => {
    if (event?.target === feedbackModal) {
      closeFeedbackModal();
    }
  });
</script>