-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<script lang="ts"> | ||
import AcmButton from '$lib/components/button/button.svelte'; | ||
</script> | ||
|
||
<section class="lucky-container"> | ||
<div class="lucky-inner-container"> | ||
<div class="lucky-text"> | ||
<h1 class="brand-header size-xl">I'm feeling lucky!</h1> | ||
<div class="lucky-button"> | ||
<AcmButton text="Visit a random page?" link="/random" redirect /> | ||
</div> | ||
</div> | ||
|
||
<img | ||
src="/assets/png/capy-lucky.png" | ||
alt="Chip the Capybara surrounded by all ACM Team badges" | ||
/> | ||
</div> | ||
</section> | ||
|
||
<style> | ||
section { | ||
display: grid; | ||
} | ||
section .lucky-inner-container { | ||
display: grid; | ||
grid-template-columns: 1fr; | ||
align-items: center; | ||
gap: 3em; | ||
} | ||
section .lucky-inner-container .lucky-text { | ||
display: grid; | ||
align-items: center; | ||
text-align: center; | ||
gap: 3em; | ||
} | ||
section .lucky-inner-container .lucky-text .lucky-button { | ||
display: grid; | ||
justify-self: center; | ||
} | ||
section .lucky-inner-container img { | ||
max-width: clamp(20rem, 17.342rem + 10.13vw, 30rem); | ||
justify-self: center; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { RequestEvent } from '@sveltejs/kit'; | ||
|
||
/** | ||
* The server-side load function for https://acmcsuf.com/random. | ||
*/ | ||
export async function GET({ url }: RequestEvent) { | ||
const modules = Object.keys( | ||
await import.meta.glob('../**/+page.svelte', { eager: true, as: 'raw' }) | ||
); | ||
const pages = getPages(modules); | ||
const randomPage = pages[~~(Math.random() * pages.length)]; | ||
const destination = new URL(randomPage, url); | ||
console.log({ modules, pages }); | ||
return Response.redirect(destination, 302); | ||
} | ||
|
||
/** | ||
* getPages gets the SvelteKit pages given a list of module names. | ||
* | ||
* Reference: | ||
* - https://github.com/sveltejs/kit/issues/923#issuecomment-1567052262 | ||
*/ | ||
function getPages(modules: string[]): string[] { | ||
return modules.map(toPathname).filter((pathname) => pathname !== null) as string[]; | ||
} | ||
|
||
function toPathname(input: string): string | null { | ||
// Removes dynamic pages that cannot be dynamically retrieved at random. | ||
// Check for [ and ] symbols in the input string and return null if found | ||
if (input.includes('[') || input.includes(']')) { | ||
return null; | ||
} | ||
|
||
// Deconstruct the pathname parts. | ||
const parts = input.split('/').filter((part) => !!part && part !== '.' && part !== '..'); | ||
|
||
// Reconstruct the pathname while ignoring last part. | ||
const pathname = parts.slice(0, parts.length - 1).join('/'); | ||
return `/${pathname}`; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.