_layout.svelte 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <script lang="typescript" context="module">
  2. import type { AppSession, PageData, PreloadContext } from "src/utils/session";
  3. import type { AuthInfo } from "src/routes/login/check";
  4. // TODO: Clean up this awful mess
  5. export async function preload(
  6. this: PreloadContext,
  7. { path }: PageData,
  8. session: AppSession
  9. ) {
  10. const authResult = await this.fetch("/login/check", { credentials: "include" });
  11. const result = (await authResult.json()) as AuthInfo;
  12. if (path.startsWith("/login/")) {
  13. if (result.loggedIn) {
  14. return this.redirect(302, "/");
  15. } else {
  16. return result;
  17. }
  18. }
  19. if (path != "/" && !result.loggedIn) {
  20. return this.redirect(302, "/");
  21. }
  22. if (result.loggedIn) {
  23. if (path == "/") {
  24. return this.redirect(302, "/rules");
  25. }
  26. if (!result.moderator && path != "/rules") {
  27. return this.redirect(302, "/rules");
  28. }
  29. }
  30. return result;
  31. }
  32. </script>
  33. <script lang="typescript">
  34. import { stores } from "@sapper/app";
  35. import Nav from "../components/Nav.svelte";
  36. const { page } = stores();
  37. export let loggedIn!: boolean;
  38. export let moderator!: boolean;
  39. let path: string;
  40. $: path = $page.path.slice(1);
  41. const ROUTES_NAV = [
  42. { route: "rules", moderatorOnly: false, name: "Rules" },
  43. { route: "rules/edit", moderatorOnly: true, name: "Edit rules" },
  44. { route: "logs", moderatorOnly: true, name: "Event logs" },
  45. ];
  46. </script>
  47. <style global>
  48. @import "tailwindcss/base";
  49. @import "tailwindcss/components";
  50. @import "tailwindcss/utilities";
  51. * {
  52. @apply font-sans;
  53. }
  54. .viewport {
  55. @apply bg-gray-700 h-screen w-full pb-8 px-20 pt-16;
  56. margin-top: 3rem;
  57. height: calc(100vh - 3rem);
  58. overflow-y: auto;
  59. }
  60. @screen lg {
  61. .viewport {
  62. @apply w-2/4;
  63. }
  64. }
  65. </style>
  66. {#if loggedIn}
  67. <Nav routes={ROUTES_NAV} currentPath={path} isModerator={moderator} />
  68. {/if}
  69. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  70. <slot />
  71. </div>