Files
classroomio/apps/dashboard/src/lib/utils/services/userjot/index.ts
T

94 lines
2.1 KiB
TypeScript
Raw Normal View History

import { dev } from '$app/environment';
import { get } from 'svelte/store';
import { globalStore } from '$lib/utils/store/app';
const USERJOT_APP_ID = 'cm4a6vcmp00jpmdb5n66rmkzz';
let isInitialized = false;
function isWidgetAllowed(): boolean {
if (dev) return false;
if (get(globalStore).isOrgSite) return false;
return true;
}
function ensureSdkLoaded(): void {
if (window.uj) return;
window.$ujq = window.$ujq || [];
window.uj =
window.uj ||
(new Proxy({} as Window['uj'], {
get:
(_, prop) =>
(...args: unknown[]) =>
window.$ujq.push([prop, ...args])
}) as Window['uj']);
const script = document.createElement('script');
script.type = 'module';
script.async = true;
script.src = 'https://cdn.userjot.com/sdk/v2/uj.js';
const nonce = document.querySelector('script[nonce]')?.getAttribute('nonce');
if (nonce) script.setAttribute('nonce', nonce);
document.head.appendChild(script);
}
export function initUserJot(): void {
if (isInitialized) return;
if (!isWidgetAllowed()) return;
ensureSdkLoaded();
window.uj.init(USERJOT_APP_ID, {
trigger: 'custom',
position: 'right',
theme: 'auto'
});
isInitialized = true;
}
type UserJotIdentity = {
id: string;
email?: string;
fullname?: string | null;
avatarUrl?: string | null;
};
export function identifyUserJotUser({ id, email, fullname, avatarUrl }: UserJotIdentity): void {
if (!isWidgetAllowed()) return;
ensureSdkLoaded();
const [firstName, ...rest] = (fullname ?? '').trim().split(/\s+/);
const lastName = rest.join(' ');
window.uj.identify({
id,
email,
firstName: firstName || undefined,
lastName: lastName || undefined,
avatar: avatarUrl ?? undefined
});
}
export function clearUserJotUser(): void {
if (!isWidgetAllowed()) return;
ensureSdkLoaded();
window.uj.identify(null);
}
export type UserJotWidgetSection = 'feedback' | 'roadmap' | 'updates';
export function showUserJotWidget(section: UserJotWidgetSection): void {
if (!isWidgetAllowed()) return;
ensureSdkLoaded();
window.uj.showWidget({ section });
}