Skip to content

Commit

Permalink
feat(svelte-ui): add basic collections grid
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiang committed Nov 24, 2024
1 parent bc02b36 commit e22ccf9
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 28 deletions.
5 changes: 4 additions & 1 deletion svelte-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@
"vitest": "^2.0.4"
},
"dependencies": {
"@supabase/sql-formatter": "^4.0.3",
"@tailwindcss/forms": "^0.5.9",
"drizzle-orm": "^0.36.4",
"pg": "^8.13.1"
"highlight.js": "^11.10.0",
"pg": "^8.13.1",
"svelte-highlight": "^7.7.0"
}
}
28 changes: 27 additions & 1 deletion svelte-ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions svelte-ui/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
<body
class="bg-white text-gray-900 selection:bg-gray-200 selection:text-gray-900 dark:bg-gray-950 dark:text-gray-50 dark:selection:bg-gray-800 dark:selection:text-gray-50"
data-sveltekit-preload-data="hover"
>
<div class="content">%sveltekit.body%</div>
</body>
</html>
10 changes: 10 additions & 0 deletions svelte-ui/src/lib/curious.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Logs a "Stay Curious" message to the console. A little easter egg for other
* developers. :)
* @see {@link https://logartis.info}
*/
export function logStayCurious() {
console.log(
"\n ██████▄▄▄█████▓▄▄▄ ▓██ ██▓ \n ▒██ ▒▓ ██▒ ▓▒████▄ ▒██ ██▒ \n ░ ▓██▄ ▒ ▓██░ ▒▒██ ▀█▄ ▒██ ██░ \n ▒ ██░ ▓██▓ ░░██▄▄▄▄██ ░ ▐██▓░ \n ▒██████▒▒ ▒██▒ ░ ▓█ ▓██▒░ ██▒▓░ \n ▒ ▒▓▒ ▒ ░ ▒ ░░ ▒▒ ▓▒█░ ██▒▒▒ \n ░ ░▒ ░ ░ ░ ▒ ▒▒ ▓██ ░▒░ \n ░ ░ ░ ░ ░ ▒ ▒ ▒ ░░ \n ░ ░ ░ ░ \n ▄████▄ █ ██ ██▀███ ██▓▒█████ █ ██ ██████ \n ▒██▀ ▀█ ██ ▓██▓██ ▒ ██▓██▒██▒ ██▒██ ▓██▒██ ▒ \n ▒▓█ ▄▓██ ▒██▓██ ░▄█ ▒██▒██░ ██▓██ ▒██░ ▓██▄ \n ▒▓▓▄ ▄██▓▓█ ░██▒██▀▀█▄ ░██▒██ ██▓▓█ ░██░ ▒ ██▒\n ▒ ▓███▀ ▒▒█████▓░██▓ ▒██░██░ ████▓▒▒▒█████▓▒██████▒▒\n ░ ░▒ ▒ ░▒▓▒ ▒ ▒░ ▒▓ ░▒▓░▓ ░ ▒░▒░▒░░▒▓▒ ▒ ▒▒ ▒▓▒ ▒ ░\n ░ ▒ ░░▒░ ░ ░ ░▒ ░ ▒░▒ ░ ░ ▒ ▒░░░▒░ ░ ░░ ░▒ ░ ░\n ░ ░░░ ░ ░ ░░ ░ ▒ ░ ░ ░ ▒ ░░░ ░ ░░ ░ ░ \n ░ ░ ░ ░ ░ ░ ░ ░ ░ \n ░ \n ",
)
}
6 changes: 5 additions & 1 deletion svelte-ui/src/lib/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ const schema = { ...tables, ...relations }

if (!env.DATABASE_URL) throw new Error("DATABASE_URL is not set")

export const db = drizzle({ schema, connection: env.DATABASE_URL })
export const db = drizzle({
schema,
logger: true,
connection: env.DATABASE_URL,
})
2 changes: 2 additions & 0 deletions svelte-ui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import "../app.css"
import { logStayCurious } from "$lib/curious"
let { children } = $props()
$effect(logStayCurious)
</script>

{@render children()}
53 changes: 48 additions & 5 deletions svelte-ui/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
import { asc, ilike, sql } from "drizzle-orm"
import * as tables from "$lib/server/db/schema/tables"
import { db } from "$lib/server/db"

export async function load() {
return {
collections: db.query.collection.findMany({
with: {
brand: true,
const collectionsPageQuery = db.query.collection
.findMany({
columns: { id: true, name: true, level: true, location: true, sex: true },
with: {
brand: {
columns: {
id: true,
name: true,
},
},
season: {
columns: { id: true, year: true, name: true },
},
looks: {
columns: { id: true, number: true },
with: {
images: {
limit: 1,
columns: { id: true, url: true },
},
},
orderBy: [asc(tables.look.number)],
},
},
where: ilike(tables.collection.name, sql.placeholder("search")),
orderBy: [
asc(tables.collection.name),
asc(tables.collection.level),
asc(tables.collection.sex),
],
limit: sql.placeholder("limit"),
})
.prepare("collectionsPageQuery")

export async function load({ url }) {
const search = url.searchParams.get("q") ?? ""
const query = collectionsPageQuery.getQuery()
let sql = query.sql
for (let i = 0; i < query.params.length; i++) {
sql = sql.replace(`$${i + 1}`, `${query.params[i]}`)
}
return {
search,
sql,
collections: collectionsPageQuery.execute({
search: `%${search}%`,
limit: search ? 100_000 : 100,
}),
}
}
88 changes: 71 additions & 17 deletions svelte-ui/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,75 @@
<script lang="ts">
import { Highlight } from "svelte-highlight";
import sql from "svelte-highlight/languages/sql";
import "svelte-highlight/styles/github-dark.css";
import { format } from "@supabase/sql-formatter"
let { data } = $props()
let value = $state("")
let code = $derived(format(data.sql))
</script>

<h1 class="font-semibold">Collections</h1>
<ul>
{#await data.collections}
<li>Loading...</li>
{:then collections}
{#each collections as collection (collection.id)}
<li>
<a href={`/${collection.id}`}>{collection.name}</a>
</li>
{/each}
{#if collections.length === 0}
<li>No collections found.</li>
{/if}
{:catch error}
<li>Error: {error.message}</li>
{/await}
</ul>
<header
class="flex h-10 items-center gap-6 border-b border-gray-200 px-6 dark:border-gray-800"
>
<h1 class="text-lg">collections</h1>
</header>
<div class="flex flex-col gap-6 p-6">
<form method="get">
<input
name="q"
type="search"
class="block w-full border-gray-200 bg-transparent focus:ring-0 dark:border-gray-700"
placeholder="Search collections"
bind:value
/>
</form>
<div class="flex flex-col gap-2">
{#await data.collections}
<p>Executing query...</p>
<Highlight class="text-xs" language={sql} {code} />
{:then collections}
<p>{collections.length} collections found</p>
{:catch error}
<p>Error: {error.message}</p>
{/await}
</div>
<ul
class="grid gap-x-2 gap-y-8 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 2xl:grid-cols-6"
>
{#await data.collections then collections}
{#each collections as collection (collection.id)}
<li class="flex flex-col gap-2 text-xs">
<div
class="flex aspect-person w-full items-center justify-center bg-gray-100 dark:bg-gray-800"
>
{#if collection.looks[0]?.images[0]?.url}
<img
src={collection.looks[0].images[0].url}
alt={collection.name}
class="h-full w-full object-cover flex items-center justify-center"
loading="lazy"
decoding="async"
/>
{:else}
<span class="text-gray-200 dark:text-gray-700"
>No look images to show</span
>
{/if}
</div>
<div>
<h2 class="font-semibold uppercase">{collection.brand.name}</h2>
<h3>{collection.season.name}</h3>
{#if collection.location}
<p class="text-gray-400 dark:text-gray-500">
{collection.location}
</p>
{/if}
</div>
</li>
{/each}
{#if collections.length === 0}
<li>No collections found.</li>
{/if}
{/await}
</ul>
</div>
9 changes: 8 additions & 1 deletion svelte-ui/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import forms from "@tailwindcss/forms"
import type { Config } from "tailwindcss"
import colors from "tailwindcss/colors"

export default {
content: ["./src/**/*.{html,js,svelte,ts}"],

theme: {
extend: {},
extend: {
colors: { gray: colors.zinc },
aspectRatio: {
person: "9 / 16",
product: "4 / 5",
},
},
},

plugins: [forms],
Expand Down

0 comments on commit e22ccf9

Please sign in to comment.