Files
classroomio/apps/dashboard/src/routes/+layout.svelte
T

185 lines
5.2 KiB
Svelte
Raw Normal View History

2023-10-29 12:07:34 +00:00
<script lang="ts">
import { page } from '$app/state';
import { OrgLandingPage, PlayQuiz } from '$features/org';
import { Snackbar } from '$features/ui';
import { UpgradeModal, PageLoadProgress, PageRestricted } from '$features/ui';
import { user } from '$lib/utils/store/user';
import { setupAnalytics } from '$lib/utils/functions/appSetup';
2024-12-26 07:23:46 +01:00
import { setTheme } from '$lib/utils/functions/theme';
import { initOrgAnalytics } from '$lib/utils/services/posthog';
import { globalStore } from '$lib/utils/store/app';
import { currentOrg } from '$lib/utils/store/org';
import { appInitApi } from '$features/app/init.svelte';
2024-12-26 07:23:46 +01:00
import merge from 'lodash/merge';
import { onMount } from 'svelte';
import { MetaTags } from 'svelte-meta-tags';
import { authClient } from '$lib/utils/services/auth/client';
import { isPublicRoute } from '$lib/utils/functions/routes/isPublicRoute';
2020-12-24 20:32:05 +02:00
2025-11-01 16:18:29 +01:00
import { ModeWatcher } from '@cio/ui/base/dark-mode';
import '../app.css';
2025-11-01 15:49:29 +01:00
let { data, children } = $props();
2023-08-14 06:22:03 +01:00
let path = $derived(page.url.pathname);
2021-07-31 23:06:14 +03:00
function intialAppSetup() {
2023-05-13 07:16:32 +01:00
console.log(
'Welcome to ClassroomIO, we are grateful you chose us.',
page.url.host,
2025-11-08 05:34:17 +01:00
`\nIs student domain: ${data.isOrgSite}`,
data
2023-05-13 07:16:32 +01:00
);
2023-10-30 06:04:53 +00:00
setupAnalytics();
if (data.locals.user) {
user.set({
...$user,
isLoggedIn: true,
currentSession: data.locals.user
});
}
2021-09-17 06:33:49 +03:00
console.log('user', $user);
// Authentication Steps
if (!data.isOrgSite || !data.org) return;
$globalStore.orgSiteName = data.orgSiteName;
$globalStore.isOrgSite = data.isOrgSite;
currentOrg.set(data.org);
// Setup internal analytics
initOrgAnalytics(data.orgSiteName);
setTheme(data.org?.theme);
}
onMount(() => {
intialAppSetup();
if (data.skipAuth) return;
2021-07-31 23:06:14 +03:00
});
const session = authClient.useSession();
const isSessionReady = $derived(!$session.isPending && !$session.isRefetching && $session.data);
2020-12-24 20:32:05 +02:00
// The goal of this effect is to make sure that we do a redirect to /login if the session data is expired
// This should be happening on the hooks.server.ts but we've made the home page public so we can show a loading spinner while the app is initializing OR an error message if the backend is down.
$effect(() => {
2025-12-25 05:09:27 +01:00
if ($session.isPending || $session.isRefetching || !!$session.data) {
console.log('session is pending or refetching');
return;
}
console.log('path', path);
console.log('isPublicRoute', isPublicRoute(path));
2025-12-25 05:09:27 +01:00
console.log('data', $session.data)
// No need to require login for public routes
2025-12-25 05:09:27 +01:00
if (isPublicRoute(path) && path !== '/') return;
if (!$session.data && path !== '/login') {
console.log('session data is not available, go to login');
window.location.href = '/login';
}
});
$effect(() => {
// this means the session cookie 'classroomio.session_data' expired and we need to trigger a new session
// triggering a new session will update the session data in the cookies so that our hooks.server.ts doesn't always have to hit the DB when checking if user is logged in or not. Without the session cookie, every page navigation or route would always hit the database to check if user is logged in or not.
if (!data.locals.fromSessions && isSessionReady) {
authClient.getSession().then(() => {
console.log('triggered new session');
});
}
});
$effect(() => {
if (isSessionReady && !appInitApi.isInitializedAndReady && !appInitApi.loading) {
console.log('setting up account with session data');
appInitApi.setupApp($session.data as App.Locals, {
isOrgSite: data.isOrgSite,
orgSiteName: data.orgSiteName
});
}
});
const metaTags = $derived(merge(data.baseMetaTags, page.data.pageMetaTags));
</script>
2025-11-01 16:18:29 +01:00
<ModeWatcher />
2024-08-01 14:04:50 +01:00
<MetaTags {...metaTags} />
2024-04-07 06:08:34 +01:00
<UpgradeModal />
2021-08-28 22:44:06 +03:00
<Snackbar />
2021-05-07 03:43:37 +03:00
{#if data.org?.isRestricted || $currentOrg.isRestricted}
<PageRestricted />
2024-08-24 20:44:16 +01:00
{:else if data.skipAuth}
2023-06-17 19:11:29 +00:00
<PlayQuiz />
{:else if data.isOrgSite && data.org && path === '/'}
<OrgLandingPage orgSiteName={data.orgSiteName} org={data.org} />
{:else}
<PageLoadProgress zIndex={10000} />
2024-11-19 19:45:31 +01:00
{@render children?.()}
2021-07-31 23:06:14 +03:00
{/if}
2021-05-07 03:43:37 +03:00
2020-12-24 20:32:05 +02:00
<style>
2021-05-06 08:22:25 +03:00
:global(:root) {
2021-07-28 22:29:23 +03:00
--main-primary-color: rgba(29, 78, 216, 1);
2021-05-06 08:22:25 +03:00
--border-color: #eaecef;
2023-09-26 11:23:54 +01:00
}
2023-04-23 22:28:18 +00:00
2023-08-20 21:28:55 +01:00
:global(.dark svg.dark) {
fill: #fff;
}
:global(.border-c) {
border: 1px solid var(--border-color);
}
:global(.border-bottom-c) {
border-bottom: 1px solid var(--border-color);
}
2023-08-07 09:53:07 +00:00
2023-08-11 09:44:50 +01:00
:global(.plyr__controls) {
2023-12-04 19:39:39 +03:00
background:
url(/logo-192.png) 99% 70% no-repeat,
2023-08-11 09:44:50 +01:00
linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.5)) !important;
2023-12-04 19:39:39 +03:00
background-size:
50px auto,
auto !important;
2023-08-11 09:44:50 +01:00
}
:global(.cards-container) {
width: 90%;
margin: 0 auto;
padding: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
column-gap: 12px;
row-gap: 12px;
}
@media screen and (max-width: 768px) {
:global(.cards-container) {
width: 95%;
margin: 0 auto;
padding: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
column-gap: 12px;
row-gap: 12px;
}
}
2020-12-24 20:32:05 +02:00
</style>