Skip to content

Commit

Permalink
New Lumina website
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertMarashi committed Aug 19, 2024
1 parent ff084c5 commit 987ca95
Show file tree
Hide file tree
Showing 61 changed files with 314 additions and 502 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
-p $SURREAL_PASS \
--ns $PUBLIC_SURREAL_NAMESPACE \
--db lumina \
database_schema.surql
schema.surql
# Define the token using echo to pipe the SQL command
echo "DEFINE TOKEN lumina_token ON SCOPE users TYPE HS256 VALUE '$AUTH_SECRET'" | \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DEFINE SCOPE users;
DEFINE TOKEN lumina_token on SCOPE users type HS256 VALUE "RANDOM_SECRET_KEY_HEX";
```

1. Run the following command to import the database schema from `database_schema.surql` from this project directory
1. Run the following command to import the database schema from `schema.surql` from this project directory

```sh
bun sync
Expand Down
Binary file modified bun.lockb
Binary file not shown.
5 changes: 2 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import stylisticTs from "@stylistic/eslint-plugin-ts"
import ts from "typescript-eslint"
import svelte from "eslint-plugin-svelte"
import globals from "globals"
import sveltets from "svelte-ts"
// import sveltets from "svelte-ts"


/** @type {import('eslint').Linter.Config[]} */
Expand All @@ -27,7 +27,6 @@ export default [
projectService: {
allowDefaultProject: ["*.svelte"]
},
// tsconfigRootDir: import.meta.dirname,
parser: ts.parser,
ecmaVersion: "latest",
extraFileExtensions: [".svelte"],
Expand Down Expand Up @@ -56,7 +55,7 @@ export default [
{
plugins: {
"@stylistic/ts": stylisticTs,
"@svelte-ts": sveltets,
// "@svelte-ts": sveltets,
},
},
{
Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"sync:queries": "surreal-codegen --dir './src/lib/queries/' --schema './database_schema.surql' --output './src/lib/queries/surreal_queries.ts'",
"sync": "surreal import database_schema.surql --endpoint http://127.0.0.1:8000 -u root -p root --ns development --db lumina",
"sync:queries": "surreal-codegen --dir './surql/' --schema './schema.surql' --output './src/lib/queries.ts' --header 'import { type RecordId, Surreal } from \"$lib/pojo_surreal\"'",
"sync": "surreal import schema.surql --endpoint http://127.0.0.1:8000 -u root -p root --ns development --db lumina",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.20",
"@sveltejs/adapter-auto": "^3.2.4",
"@sveltejs/kit": "^2.5.22",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"@types/eslint": "^9.6.0",
"eslint": "^9.8.0",
"eslint": "^9.9.0",
"eslint-plugin-svelte": "^2.43.0",
"globals": "^15.9.0",
"svelte": "5.0.0-next.220",
"svelte": "5.0.0-next.225",
"svelte-check": "^3.8.5",
"tslib": "^2.6.3",
"typescript": "^5.5.4",
"typescript-eslint": "^8.0.1",
"vite": "^5.4.0"
"typescript-eslint": "^8.1.0",
"vite": "^5.4.1"
},
"type": "module",
"dependencies": {
Expand All @@ -35,11 +35,11 @@
"@builder.io/sdk-svelte": "^2.0.3",
"@resvg/resvg-js": "^2.6.2",
"@sendgrid/mail": "^8.1.3",
"@stylistic/eslint-plugin-ts": "^2.6.2",
"@sveltejs/adapter-vercel": "^5.4.1",
"@stylistic/eslint-plugin-ts": "^2.6.4",
"@sveltejs/adapter-vercel": "^5.4.3",
"@types/mdast": "^4.0.4",
"@types/mixpanel-browser": "^2.49.1",
"@types/node": "^22.1.0",
"@types/node": "^22.4.1",
"@types/three": "^0.167.1",
"dedent": "^1.5.3",
"highlight.js": "^11.10.0",
Expand All @@ -53,15 +53,15 @@
"micromark-extension-gfm-strikethrough": "^2.1.0",
"micromark-extension-gfm-table": "^2.1.0",
"mixpanel-browser": "^2.55.0",
"runed": "^0.15.0",
"runed": "^0.15.1",
"satori": "^0.10.14",
"satori-html": "^0.3.2",
"schema-dts": "^1.1.2",
"surrealdb.js": "^1.0.0-beta.9",
"surrealdb.js": "1.0.0-beta.18",
"svelte-material-icons": "^3.0.5",
"three": "^0.167.1",
"three-globe": "^2.31.1",
"vite-plugin-arraybuffer": "0.0.2",
"vite-plugin-arraybuffer": "0.0.8",
"zod": "^3.23.8"
}
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GetUserResult } from "$lib/queries/surreal_queries"
import type { GetUserResult, TypedSurreal } from "$lib/queries"
import type { AlertsStore } from "$lib/stores/alerts"
import type { Surreal } from "surrealdb.js"
import type { TypedSurreal } from "$lib/queries"
// import type { Session } from "@auth/core/types"

// See https://kit.svelte.dev/docs/types#app
Expand All @@ -16,7 +16,7 @@ declare global {

interface PageData {
// Safe client-authenticated SurrealDB
db: Surreal;
db: TypedSurreal;
alerts: AlertsStore;
user: GetUserResult[0][0] | null
}
Expand Down
8 changes: 4 additions & 4 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { sequence } from "@sveltejs/kit/hooks"
import { AsyncLocalStorage } from "node:async_hooks"
import { isolated_global } from "./lib/stores/database"
import { create_resolver } from "$lib/utils/resolver"
import type Surreal from "surrealdb.js"
import type { TypedSurreal } from "$lib/queries"

const local_storage = new AsyncLocalStorage<{
db: Promise<Surreal>
resolve_db: (db: Surreal | PromiseLike<Surreal>) => void
db: Promise<TypedSurreal>
resolve_db: (db: TypedSurreal | PromiseLike<TypedSurreal>) => void
}>()

isolated_global.getStore = () => local_storage.getStore()!
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function handleError({ error }) {
async function create_async_local_storage({
event, resolve
}: Parameters<typeof handle>[0]) {
const resolver = create_resolver<Surreal>()
const resolver = create_resolver<TypedSurreal>()

// let safe_surreal_db_client(event.locals.token)
return local_storage.run({
Expand Down
4 changes: 3 additions & 1 deletion src/lib/blocks/Special/Special.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ let {
const components: Record<string, Component> = {
beacon: Beacon,
}
let Special = $derived(components[type])
</script>
<svelte:component this={ components[type] }/>
<Special/>
10 changes: 8 additions & 2 deletions src/lib/controls/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ let {
hug = true,
disabled,
label,
target,
onclick,
}: {
href?: string
left_icon?: IconComponent | string
right_icon?: IconComponent | string
style?: "translucent" | "transparent" | "branded"
hug?: boolean
target?: "_blank"
disabled?: boolean
label?: string
onclick?: (e: Event) => void
Expand All @@ -34,6 +36,9 @@ function handle_keyup(e: KeyboardEvent) {
}
}
let LeftIcon = $derived(typeof left_icon === "string" ? null : left_icon)
let RightIcon = $derived(typeof right_icon === "string" ? null : right_icon)
</script>

<svelte:element
Expand All @@ -46,11 +51,12 @@ function handle_keyup(e: KeyboardEvent) {
onkeyup={handle_keyup}
role="button"
tabindex={disabled ? -1 : 0}
target={target}
>
{#if left_icon}
<span class="icon">
{#if typeof left_icon !== "string"}
<svelte:component this={ left_icon } />
<LeftIcon />
{:else if typeof left_icon === "string"}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html left_icon}
Expand All @@ -65,7 +71,7 @@ function handle_keyup(e: KeyboardEvent) {
{#if right_icon}
<span class="icon">
{#if typeof right_icon !== "string"}
<svelte:component this={ right_icon } />
<RightIcon />
{:else if typeof right_icon === "string"}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html right_icon}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/controls/IconButton.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import Icon from "$lib/display/Icon.svelte"
import type { IconComponent } from "$lib/utils/icon_type"
let {
Expand All @@ -8,7 +9,7 @@ let {
onclick = () => {}
}: {
href?: string
icon?: IconComponent
icon: IconComponent
opacity?: boolean,
onclick?: (e: Event) => void
} = $props()
Expand All @@ -20,6 +21,8 @@ function handle_keyup(e: KeyboardEvent) {
onclick(e)
}
}
// let Icon = $derived(icon)
</script>

<svelte:element
Expand All @@ -31,7 +34,7 @@ function handle_keyup(e: KeyboardEvent) {
onkeyup={handle_keyup}
role="button"
tabindex="0">
<svelte:component this={ icon } />
<Icon icon={icon}/>
</svelte:element>
<style>
.button {
Expand Down
16 changes: 9 additions & 7 deletions src/lib/display/Icon.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
import type { IconComponent } from "$lib/utils/icon_type"
export let icon: IconComponent
let {
icon,
}: {
icon: IconComponent
} = $props()
let Icon = $derived(icon)
</script>
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="icon"
on:click
on:keypress>
<svelte:component this={ icon }/>
<div class="icon">
<Icon/>
</div>
<style>
Expand Down
94 changes: 94 additions & 0 deletions src/lib/pojo_surreal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {RecordId as BaseRecordId, Surreal as BaseSurreal, type Prettify, type QueryParameters, type RecordIdValue} from "surrealdb.js"
import { z } from "zod"

export interface RecordId<Tb extends string = string> {
tb: Tb
id: string
}

export function new_record<Tb extends string>(tb: Tb, id: string): RecordId<Tb> {
return {
tb,
id
}
}

export function record_to_string(record: RecordId<string>): string {
return `${record.tb}:${record.id}`
}

export class Surreal extends BaseSurreal {
async query<T extends unknown[]>(...args: QueryParameters): Promise<Prettify<T>> {
if (args[1] instanceof Array) return convert_to_record_id(await super.query(...args)) as unknown as Promise<Prettify<T>>
if (args[1]) args[1] = convert_to_record_id_class(args[1]) as Record<string, unknown>
return convert_to_record_id(await super.query(...args)) as unknown as Promise<Prettify<T>>
}
}

export function zod_record_id<Tb extends string>(tb: Tb) {
return z.object({
tb: z.literal(tb),
id: z.string(),
})
}

function convert_to_record_id(value: unknown): unknown {
if (value instanceof BaseRecordId) {
return {
tb: value.tb,
id: value.id,
}
}

if (Array.isArray(value)) return value.map(convert_to_record_id)
if (typeof value === "object" && value !== null) {
const converted: Record<string, unknown> = {}
for (const [key, val] of Object.entries(value)) {
converted[key] = convert_to_record_id(val)
}
return converted
}
return value
}

function convert_to_record_id_class(value: unknown): unknown {
if (value instanceof BaseRecordId) return value
if (Array.isArray(value)) return value.map(convert_to_record_id_class)
if (typeof value === "object" && value !== null) {
if (is_record_id(value)) return new BaseRecordId(value.tb, value.id)
const converted: Record<string, unknown> = {}
for (const [key, val] of Object.entries(value)) {
converted[key] = convert_to_record_id_class(val)
}
return converted
}
return value
}

const typeofs_allowed: Record<string, true | undefined> = {
string: true,
number: true,
boolean: true,
bigint: true,
}

function is_record_id_value(id: unknown): id is RecordIdValue {
if (typeofs_allowed[typeof id]) return true
if (Array.isArray(id)) return true
if (typeof id === "object" && id !== null) return true
return false
}

export function is_record_id(value: unknown): value is RecordId<string> {
if (
typeof value === "object"
&& value !== null
&& "tb" in value
&& typeof value.tb === "string"
&& "id" in value
&& is_record_id_value(value.id)
) {
return true
}
return false
}
Loading

0 comments on commit 987ca95

Please sign in to comment.