_layout.svelte 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. console.log(result);
  14. console.log(path);
  15. if (path.startsWith("/login/")) {
  16. if (result.loggedIn) {
  17. return this.redirect(302, "/");
  18. } else {
  19. return result;
  20. }
  21. }
  22. if (path != "/" && !result.loggedIn) {
  23. return this.redirect(302, "/");
  24. }
  25. if (result.loggedIn) {
  26. if (!path.startsWith("/rules")) {
  27. return this.redirect(302, "/rules");
  28. }
  29. if (!result.moderator && path != "/rules") {
  30. return this.redirect(302, "/rules");
  31. }
  32. }
  33. return result;
  34. }
  35. </script>
  36. <script lang="typescript">
  37. import { stores } from "@sapper/app";
  38. const { page } = stores();
  39. export let loggedIn!: boolean;
  40. export let moderator!: boolean;
  41. let path: string;
  42. $: path = $page.path.slice(1);
  43. const ROUTES_NAV = [
  44. { route: "rules", moderator: false, name: "Rules" },
  45. { route: "rules/edit", moderator: true, name: "Edit rules" },
  46. { route: "logs", moderator: true, name: "Event logs" },
  47. ];
  48. </script>
  49. <style global>
  50. @import "tailwindcss/base";
  51. @import "tailwindcss/components";
  52. @import "tailwindcss/utilities";
  53. * {
  54. @apply font-sans;
  55. }
  56. nav {
  57. @apply fixed w-screen h-12 shadow-lg flex flex-row items-center px-4 z-10;
  58. background-color: #0a0d13;
  59. a {
  60. @apply text-white px-5 py-3;
  61. &:hover {
  62. @apply bg-gray-800 cursor-pointer;
  63. }
  64. &.active {
  65. @apply bg-gray-800;
  66. }
  67. }
  68. }
  69. .viewport {
  70. @apply bg-gray-700 h-screen w-full pb-8 px-20 pt-16;
  71. margin-top: 3rem;
  72. height: calc(100vh - 3rem);
  73. overflow-y: auto;
  74. }
  75. @screen lg {
  76. .viewport {
  77. @apply w-2/4;
  78. }
  79. }
  80. </style>
  81. {#if loggedIn}
  82. <nav>
  83. {#each ROUTES_NAV as route (route.route) }
  84. {#if !route.moderator || (route.moderator && moderator) }
  85. <a href="/{route.route}" class:active={route.route == path}>{route.name}</a>
  86. {/if}
  87. {/each}
  88. <span class="mx-auto"></span>
  89. <a href="/logout">Log out</a>
  90. </nav>
  91. {/if}
  92. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  93. <slot />
  94. </div>