2025-05-03 14:46:01 +01:00
|
|
|
import type { CurrentOrg, OrgAudience, OrgTeamMember } from '../types/org';
|
2025-07-21 21:33:50 +01:00
|
|
|
import { browser, dev } from '$app/environment';
|
|
|
|
|
import { derived, writable } from 'svelte/store';
|
|
|
|
|
|
|
|
|
|
import { PLAN } from 'shared/src/plans/constants';
|
|
|
|
|
import { ROLE } from '$lib/utils/constants/roles';
|
|
|
|
|
import { STEPS } from '../constants/quiz';
|
|
|
|
|
import type { UserLessonDataType } from '$lib/utils/types';
|
|
|
|
|
import type { Writable } from 'svelte/store';
|
|
|
|
|
import { env } from '$env/dynamic/public';
|
2023-01-03 09:26:08 +01:00
|
|
|
|
2023-12-05 08:31:26 +03:00
|
|
|
export const defaultCurrentOrgState: CurrentOrg = {
|
2023-04-22 20:00:37 +00:00
|
|
|
id: '',
|
|
|
|
|
name: '',
|
|
|
|
|
shortName: '',
|
|
|
|
|
siteName: '',
|
2023-05-02 23:55:26 +00:00
|
|
|
avatar_url: '',
|
2023-08-21 15:44:15 +00:00
|
|
|
memberId: '',
|
2024-06-08 06:19:20 +01:00
|
|
|
role_id: 0,
|
2023-08-21 15:44:15 +00:00
|
|
|
landingpage: {},
|
2024-05-17 01:28:59 +01:00
|
|
|
customization: {
|
|
|
|
|
apps: { poll: true, comments: true },
|
|
|
|
|
course: { grading: true, newsfeed: true },
|
|
|
|
|
dashboard: { exercise: true, community: true, bannerText: '', bannerImage: '' }
|
|
|
|
|
},
|
2024-04-07 06:08:34 +01:00
|
|
|
theme: '',
|
2024-08-24 20:44:16 +01:00
|
|
|
organization_plan: [],
|
|
|
|
|
is_restricted: false
|
2023-12-05 08:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const orgs = writable<CurrentOrg[]>([]);
|
|
|
|
|
export const currentOrg: Writable<CurrentOrg> = writable(defaultCurrentOrgState);
|
2023-10-30 04:52:00 +00:00
|
|
|
export const orgAudience = writable<OrgAudience[]>([]);
|
2023-09-07 10:47:25 +00:00
|
|
|
export const orgTeam = writable<OrgTeamMember[]>([]);
|
2024-05-22 05:49:10 +01:00
|
|
|
export const isOrgAdmin = derived(currentOrg, ($currentOrg) => $currentOrg.role_id === ROLE.ADMIN);
|
|
|
|
|
|
2024-09-12 23:54:04 +01:00
|
|
|
const getActivePlan = (org: CurrentOrg) => {
|
|
|
|
|
return org.organization_plan.find((p) => p.is_active);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const currentOrgPlan = derived(currentOrg, ($currentOrg) => getActivePlan($currentOrg));
|
2024-09-05 15:19:45 +01:00
|
|
|
|
2023-08-05 05:48:32 +01:00
|
|
|
export const currentOrgPath = derived(currentOrg, ($currentOrg) =>
|
|
|
|
|
$currentOrg.siteName ? `/org/${$currentOrg.siteName}` : ''
|
2023-05-20 23:53:28 +00:00
|
|
|
);
|
2024-09-05 15:19:45 +01:00
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
export const currentOrgDomain = derived(currentOrg, ($currentOrg) => {
|
|
|
|
|
const browserOrigin = dev && browser && window.location.origin;
|
2025-07-21 21:33:50 +01:00
|
|
|
|
|
|
|
|
// Get the root domain from window.location
|
|
|
|
|
let rootDomain = '';
|
|
|
|
|
if (browser && typeof window !== 'undefined') {
|
|
|
|
|
const host = window.location.hostname;
|
|
|
|
|
const parts = host.split('.');
|
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
|
rootDomain = parts.slice(-2).join('.');
|
|
|
|
|
} else {
|
|
|
|
|
rootDomain = host;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 04:52:00 +00:00
|
|
|
return browserOrigin
|
|
|
|
|
? browserOrigin
|
2024-09-05 15:19:45 +01:00
|
|
|
: $currentOrg.customDomain && $currentOrg.isCustomDomainVerified
|
|
|
|
|
? `https://${$currentOrg.customDomain}`
|
|
|
|
|
: $currentOrg.siteName
|
2025-07-21 21:33:50 +01:00
|
|
|
? `https://${$currentOrg.siteName}.${rootDomain}`
|
2024-09-05 15:19:45 +01:00
|
|
|
: '';
|
2023-10-30 04:52:00 +00:00
|
|
|
});
|
2023-06-17 19:11:29 +00:00
|
|
|
|
2024-09-12 23:54:04 +01:00
|
|
|
export const isFreePlan = derived(currentOrg, ($currentOrg) => {
|
2024-09-30 01:20:19 +01:00
|
|
|
if (!$currentOrg.id || env.IS_SELFHOSTED === 'true') return false;
|
2024-09-09 01:14:55 +01:00
|
|
|
|
2024-09-12 23:54:04 +01:00
|
|
|
const plan = getActivePlan($currentOrg);
|
|
|
|
|
|
|
|
|
|
return !plan || plan.plan_name === PLAN.BASIC;
|
2024-09-09 01:14:55 +01:00
|
|
|
});
|
2024-09-12 23:54:04 +01:00
|
|
|
|
2024-04-07 06:08:34 +01:00
|
|
|
export const currentOrgMaxAudience = derived(currentOrgPlan, ($plan) =>
|
|
|
|
|
!$plan
|
2024-07-31 09:48:26 +01:00
|
|
|
? 20
|
2024-04-07 06:08:34 +01:00
|
|
|
: $plan.plan_name === PLAN.EARLY_ADOPTER
|
|
|
|
|
? 10000
|
|
|
|
|
: $plan.plan_name === PLAN.ENTERPRISE
|
|
|
|
|
? Number.MAX_SAFE_INTEGER
|
2024-07-31 09:48:26 +01:00
|
|
|
: 20
|
2024-04-07 06:08:34 +01:00
|
|
|
);
|
|
|
|
|
|
2023-06-17 19:11:29 +00:00
|
|
|
// Quiz
|
|
|
|
|
export const createQuizModal = writable({
|
|
|
|
|
open: false,
|
|
|
|
|
openEdit: false,
|
|
|
|
|
title: '',
|
2023-08-05 05:48:32 +01:00
|
|
|
id: null
|
2023-06-17 19:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const deleteModal = writable({
|
|
|
|
|
id: null,
|
|
|
|
|
open: false,
|
2023-08-05 05:48:32 +01:00
|
|
|
isQuestion: false
|
2023-06-17 19:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
2025-05-03 14:46:01 +01:00
|
|
|
interface QuizOption {
|
|
|
|
|
id: number;
|
|
|
|
|
label: string;
|
|
|
|
|
isCorrect: boolean;
|
|
|
|
|
}
|
2023-06-17 19:11:29 +00:00
|
|
|
|
2025-05-03 14:46:01 +01:00
|
|
|
interface QuizQuestion {
|
|
|
|
|
id: number;
|
|
|
|
|
label: string;
|
|
|
|
|
type: string;
|
|
|
|
|
options: QuizOption[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface QuizStore {
|
|
|
|
|
uuid: string;
|
|
|
|
|
title: string;
|
|
|
|
|
questions: QuizQuestion[];
|
|
|
|
|
timelimit: string;
|
|
|
|
|
theme: string;
|
|
|
|
|
pin: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const quizesStore = writable<QuizStore[]>([]);
|
|
|
|
|
|
|
|
|
|
export const quizStore = writable<QuizStore>({
|
2023-06-17 19:11:29 +00:00
|
|
|
uuid: '',
|
|
|
|
|
title: '',
|
|
|
|
|
questions: [],
|
|
|
|
|
timelimit: '10s',
|
|
|
|
|
theme: 'standard',
|
2023-08-05 05:48:32 +01:00
|
|
|
pin: ''
|
2023-06-17 19:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const playQuizStore = writable({
|
2023-08-05 05:48:32 +01:00
|
|
|
step: STEPS.CONNECT_TO_PLAY
|
2023-06-17 19:11:29 +00:00
|
|
|
});
|
2023-10-22 09:47:13 +01:00
|
|
|
|
|
|
|
|
export const userUpcomingData = writable<UserLessonDataType[]>([]);
|