diff --git a/app/views/app.css b/app/views/app.css index e737ae3..df37b10 100644 --- a/app/views/app.css +++ b/app/views/app.css @@ -31,6 +31,7 @@ header h1 > :is(a, a:hover) { } header nav > * { + margin: 0.75rem; font-size: 1.25rem; } diff --git a/app/views/app.tsx b/app/views/app.tsx index dcb3bca..126fee7 100644 --- a/app/views/app.tsx +++ b/app/views/app.tsx @@ -2,11 +2,13 @@ import "./app.css"; import { createSignal } from "solid-js"; import Index from "./pages/index"; import Page from "./pages/page"; +import Random from "./pages/random"; import random from "./helpers/random"; import { decodeJwt } from "jose"; const routes = { "/": Index, + "/random": Random, }; async function updateUser(jwt: string): Promise { @@ -70,6 +72,7 @@ export default () => { {routes[pathname()] ?? ( diff --git a/app/views/pages/random.tsx b/app/views/pages/random.tsx new file mode 100644 index 0000000..170b538 --- /dev/null +++ b/app/views/pages/random.tsx @@ -0,0 +1,21 @@ +import { createEffect, createResource } from "solid-js"; +import type Pages from "../../protocol/pages"; +import random from "../helpers/random"; + +async function randomPage(): Promise { + const jwt = window.localStorage.getItem("jwt"); + const res = await fetch( + `${import.meta.env.QUOT_API_ENDPOINT}/pages?order=random&limit=1`, + { headers: jwt ? { authorization: `Bearer ${jwt}` } : {} } + ); + const data = (await res.json()) as Pages.Response; + return data[0]!; +} + +export default () => { + const [page] = createResource(random(), randomPage); + createEffect(() => { + if (!page.loading) window.location.replace(`/${page()!.id.toString(16)}`); + }); + return null; +}; diff --git a/db/migrations/20220907130637_random_pages.sql b/db/migrations/20220907130637_random_pages.sql new file mode 100644 index 0000000..7b193eb --- /dev/null +++ b/db/migrations/20220907130637_random_pages.sql @@ -0,0 +1,7 @@ +-- migrate:up +CREATE FUNCTION random(pages) RETURNS DOUBLE PRECISION LANGUAGE SQL AS $$ + SELECT random(); +$$; + +-- migrate:down +DROP FUNCTION random(pages); diff --git a/db/schema.sql b/db/schema.sql index 33f7499..9e259ed 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -9,20 +9,6 @@ SET xmloption = content; SET client_min_messages = warning; SET row_security = off; --- --- Name: update_timestamp(); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION public.update_timestamp() RETURNS trigger - LANGUAGE plpgsql - AS $$ - BEGIN - NEW.updated = CURRENT_TIMESTAMP; - RETURN NEW; - END -$$; - - SET default_tablespace = ''; SET default_table_access_method = heap; @@ -42,6 +28,31 @@ CREATE TABLE public.pages ( ); +-- +-- Name: random(public.pages); Type: FUNCTION; Schema: public; Owner: - +-- + +CREATE FUNCTION public.random(public.pages) RETURNS double precision + LANGUAGE sql + AS $$ + SELECT random(); +$$; + + +-- +-- Name: update_timestamp(); Type: FUNCTION; Schema: public; Owner: - +-- + +CREATE FUNCTION public.update_timestamp() RETURNS trigger + LANGUAGE plpgsql + AS $$ + BEGIN + NEW.updated = CURRENT_TIMESTAMP; + RETURN NEW; + END +$$; + + -- -- Name: pages_id_seq; Type: SEQUENCE; Schema: public; Owner: - -- @@ -186,4 +197,5 @@ CREATE POLICY users_policy ON public.users USING ((id = ((current_setting('reque -- INSERT INTO public.schema_migrations (version) VALUES - ('0'); + ('0'), + ('20220907130637');