Minor refactors
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as Common from "common";
|
||||
import * as Function from "common/Function";
|
||||
import { unsafeCoerce } from "effect";
|
||||
import { t } from "elysia";
|
||||
|
||||
export interface AccessTokenPayload {
|
||||
@@ -24,8 +24,8 @@ export interface IdTokenPayload {
|
||||
}
|
||||
|
||||
const brandedString = <T>() => t.Transform(t.String())
|
||||
.Decode(Function.unsafeCoerce<string, T>)
|
||||
.Encode(Function.unsafeCoerce<T, string>);
|
||||
.Decode(unsafeCoerce<string, T>)
|
||||
.Encode(unsafeCoerce<T, string>);
|
||||
|
||||
export const Sha256_Hex = brandedString<Common.Sha256_Hex>();
|
||||
export const AttachmentId = brandedString<Common.AttachmentId>();
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
".": { "import": "./src/index.ts" },
|
||||
"./*": { "import": "./src/*.ts" }
|
||||
},
|
||||
"dependencies": {
|
||||
"effect": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "catalog:"
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import * as Function from "./Function";
|
||||
import * as Types from "./Types";
|
||||
|
||||
declare const BrandTypeId: unique symbol;
|
||||
export type BrandTypeId = typeof BrandTypeId;
|
||||
|
||||
declare const ConstructorTypeId: unique symbol;
|
||||
export type ConstructorTypeId = typeof ConstructorTypeId;
|
||||
|
||||
export interface Brand<in out K extends string | symbol> {
|
||||
readonly [BrandTypeId]: {
|
||||
readonly [k in K]: K;
|
||||
};
|
||||
}
|
||||
|
||||
export declare namespace Brand {
|
||||
export interface Constructor<in out A extends Brand<any>> {
|
||||
readonly [ConstructorTypeId]: ConstructorTypeId;
|
||||
(args: Brand.Unbranded<A>): A;
|
||||
}
|
||||
|
||||
export type Unbranded<P> = P extends infer Q & Brands<P> ? Q : P;
|
||||
|
||||
export type Brands<P> = P extends Brand<any>
|
||||
? Types.UnionToIntersection<{
|
||||
[k in keyof P[BrandTypeId]]: k extends string | symbol ? Brand<k> : never
|
||||
}[keyof P[BrandTypeId]]>
|
||||
: never;
|
||||
}
|
||||
|
||||
export type Branded<A, K extends string | symbol> = A & Brand<K>;
|
||||
|
||||
export const nominal = <A extends Brand<any>>(): Brand.Constructor<A> => {
|
||||
return Function.identity as Brand.Constructor<A>;
|
||||
};
|
||||
@@ -1,4 +0,0 @@
|
||||
export const identity = <A>(a: A): A => a;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- unsafe by design
|
||||
export const unsafeCoerce: <A, B>(a: A) => B = identity as any;
|
||||
@@ -1 +0,0 @@
|
||||
export type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
|
||||
@@ -1,4 +1,4 @@
|
||||
import * as Brand from "./Brand";
|
||||
import { Brand } from "effect";
|
||||
|
||||
export type UUID = Brand.Branded<string, "UUID">;
|
||||
export const UUID = Brand.nominal<UUID>();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { API_URL_PREFIX } from "@/client";
|
||||
import { mapProp, Update, Updater, useStore } from "@/hooks/useStore";
|
||||
import { mapProp, Update, Updater } from "@/hooks/useStore";
|
||||
import { Treaty } from "@elysiajs/eden";
|
||||
import { Effect, Fiber, pipe } from "effect";
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
@@ -45,8 +45,6 @@ export function useLoading<R extends Record<number, unknown>>(fn: () => Promise<
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
const setUser = useStore(state => state.setUser);
|
||||
|
||||
const [result, setResult] = useState<LoadingResult<R>>(IS_LOADING);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -82,7 +80,7 @@ export function useLoading<R extends Record<number, unknown>>(fn: () => Promise<
|
||||
|
||||
return () => { cancelled = true; };
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [navigate, setUser, ...deps]);
|
||||
}, [navigate, ...deps]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { UserId } from "common";
|
||||
import * as Function from "common/Function";
|
||||
import { identity } from "effect";
|
||||
import { useLayoutEffect, useState } from "react";
|
||||
|
||||
export type Update<T> = T | ((prev: T) => T);
|
||||
@@ -19,14 +19,16 @@ export namespace Store {
|
||||
|
||||
export interface Store {
|
||||
readonly user: Store.User | null;
|
||||
readonly setUser: Updater<Store.User | null>;
|
||||
}
|
||||
|
||||
let store: Store = Object.freeze<Store>({
|
||||
user: null,
|
||||
setUser: (action) => set(mapProp("user", action)),
|
||||
});
|
||||
|
||||
export function setUser(action: Update<Store.User | null>) {
|
||||
set(mapProp("user", action));
|
||||
}
|
||||
|
||||
// --- STORE IMPLEMENTATION ----------------------------------------------------
|
||||
|
||||
class Listener<T> {
|
||||
@@ -64,7 +66,7 @@ function set(action: Partial<Store> | ((store: Store) => Partial<Store>), replac
|
||||
}
|
||||
}
|
||||
|
||||
export function useStore<T = Store>(selector: Selector<T> = Function.identity as Selector<T>): T {
|
||||
export function useStore<T = Store>(selector: Selector<T> = identity as Selector<T>): T {
|
||||
|
||||
const [state, setState] = useState(() => selector(store));
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { API_URL_PREFIX, client } from "@/client";
|
||||
import { Button, buttonVariants } from "@/components/ui/button";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
import { useStore } from "@/hooks/useStore";
|
||||
import { setUser, useStore } from "@/hooks/useStore";
|
||||
import { Settings, User } from "lucide-react";
|
||||
import { useEffect } from "react";
|
||||
import { Link, Outlet } from "react-router-dom";
|
||||
@@ -9,7 +9,6 @@ import { Link, Outlet } from "react-router-dom";
|
||||
export function Root() {
|
||||
|
||||
const user = useStore(state => state.user);
|
||||
const setUser = useStore(state => state.setUser);
|
||||
|
||||
const init = async () => {
|
||||
if (user !== null) return;
|
||||
|
||||
4
pnpm-lock.yaml
generated
4
pnpm-lock.yaml
generated
@@ -167,6 +167,10 @@ importers:
|
||||
version: 5.7.2
|
||||
|
||||
packages/common:
|
||||
dependencies:
|
||||
effect:
|
||||
specifier: 'catalog:'
|
||||
version: 3.11.4
|
||||
devDependencies:
|
||||
typescript:
|
||||
specifier: 'catalog:'
|
||||
|
||||
Reference in New Issue
Block a user