_layout.svelte 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import { goto } from "@sapper/app";
  5. // TODO: Clean up this awful mess
  6. export async function preload(
  7. this: PreloadContext,
  8. { path }: PageData,
  9. session: AppSession
  10. ) {
  11. const authResult = await this.fetch("/login/check", { credentials: "include" });
  12. const result = (await authResult.json()) as AuthInfo;
  13. if (path.startsWith("/login/")) {
  14. if (result.loggedIn) {
  15. return this.redirect(302, "/");
  16. } else {
  17. return result;
  18. }
  19. }
  20. if (path != "/" && !result.loggedIn) {
  21. return this.redirect(302, "/");
  22. }
  23. if (result.loggedIn) {
  24. if (!path.startsWith("/rules")) {
  25. return this.redirect(302, "/rules");
  26. }
  27. if (!result.moderator && path != "/rules") {
  28. return this.redirect(302, "/rules");
  29. }
  30. }
  31. return result;
  32. }
  33. </script>
  34. <script lang="typescript">
  35. import { stores } from "@sapper/app";
  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", moderator: false, name: "Rules" },
  43. { route: "rules/edit", moderator: true, name: "Edit rules" },
  44. { route: "logs", moderator: 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. nav {
  55. @apply fixed w-screen h-12 shadow-lg flex flex-row items-center px-4 z-10;
  56. background-color: #0a0d13;
  57. a {
  58. @apply text-white px-5 py-3;
  59. &:hover {
  60. @apply bg-gray-800 cursor-pointer;
  61. }
  62. &.active {
  63. @apply bg-gray-800;
  64. }
  65. }
  66. }
  67. .viewport {
  68. @apply bg-gray-700 h-screen w-full pb-8 px-20 pt-16;
  69. margin-top: 3rem;
  70. height: calc(100vh - 3rem);
  71. overflow-y: auto;
  72. }
  73. @screen lg {
  74. .viewport {
  75. @apply w-2/4;
  76. }
  77. }
  78. </style>
  79. {#if loggedIn}
  80. <nav>
  81. {#each ROUTES_NAV as route (route.route) }
  82. {#if !route.moderator || (route.moderator && moderator) }
  83. <a href="/{route.route}" class:active={route.route == path}>{route.name}</a>
  84. {/if}
  85. {/each}
  86. <span class="mx-auto"></span>
  87. <a href="/logout">Log out</a>
  88. </nav>
  89. {/if}
  90. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  91. <slot />
  92. </div>