web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit fd2833baf7cc281a8b987453e83a69e430e29a53
parent 82bf917cd9faed05eefc6d43170fc06f0b666cd1
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri,  4 Oct 2024 09:35:47 +0000

apps-lib: edit query param stores, add nostr types, move ndk utils to `@radroots/utils`

Diffstat:
Mapps-lib/src/lib/index.ts | 2+-
Mapps-lib/src/lib/stores/client.ts | 13+++++++------
Aapps-lib/src/lib/types/nostr.ts | 5+++++
Dapps-lib/src/lib/utils/ndk.ts | 55-------------------------------------------------------
Mapps-lib/src/lib/utils/routes.ts | 10+++++++---
5 files changed, 20 insertions(+), 65 deletions(-)

diff --git a/apps-lib/src/lib/index.ts b/apps-lib/src/lib/index.ts @@ -15,12 +15,12 @@ export * from "./types/trellis"; export * from "./types/components"; export * from "./types/client"; export * from "./types/ui"; +export * from "./types/nostr"; export * from "./stores/ndk"; export * from "./stores/client"; export * from "./utils/routes"; export * from "./utils/dom"; export * from "./utils/styles"; -export * from "./utils/ndk"; export * from "./utils/client"; export * from "./utils/time"; export * from "./utils/geo"; diff --git a/apps-lib/src/lib/stores/client.ts b/apps-lib/src/lib/stores/client.ts @@ -1,4 +1,4 @@ -import { type AppLayoutKey, type IToast, type NavigationPreviousParam, type NavigationRouteParamId, type NavigationRouteParamLat, type NavigationRouteParamLng, type NavigationRouteParamNostrPublicKey, type NavigationRouteParamRecordKey } from "$lib"; +import { type AppLayoutKey, type IToast, type NavigationPreviousParam } from "$lib"; import { writable } from "svelte/store"; import { queryParam, queryParameters } from "sveltekit-search-params"; @@ -7,11 +7,11 @@ const kv_name = import.meta.env.VITE_PUBLIC_KV_NAME; if (!kv_name) throw new Error('Error: VITE_PUBLIC_KV_NAME is required'); export const qp = queryParameters(); -export const qp_nostr_pk = queryParam<NavigationRouteParamNostrPublicKey>("nostr_pk"); -export const qp_rkey = queryParam<NavigationRouteParamRecordKey>("rkey"); -export const qp_id = queryParam<NavigationRouteParamId>("id"); -export const qp_lat = queryParam<NavigationRouteParamLat>("lat"); -export const qp_lng = queryParam<NavigationRouteParamLng>("lng"); +export const qp_nostr_pk = queryParam<string>("nostr_pk"); +export const qp_rkey = queryParam<string>("rkey"); +export const qp_id = queryParam<string>("id"); +export const qp_lat = queryParam<string>("lat"); +export const qp_lng = queryParam<string>("lng"); export let kv: Keyva; if (typeof window !== 'undefined') kv = new Keyva({ name: kv_name }); @@ -37,4 +37,5 @@ export const nostr_ndk_configured = writable<boolean>(false); export const nostr_relays_poll_documents = writable<boolean>(false); export const nostr_relays_poll_documents_count = writable<number>(0); export const nostr_relays_connected = writable<string[]>([]); +export const nostr_sync_prevent = writable<boolean>(false); diff --git a/apps-lib/src/lib/types/nostr.ts b/apps-lib/src/lib/types/nostr.ts @@ -0,0 +1,4 @@ +import type { NDKEvent } from "@nostr-dev-kit/ndk"; +import type { ExtendedBaseType, NDKEventStore } from "@nostr-dev-kit/ndk-svelte"; + +export type NostrEventStore = NDKEventStore<ExtendedBaseType<NDKEvent>> +\ No newline at end of file diff --git a/apps-lib/src/lib/utils/ndk.ts b/apps-lib/src/lib/utils/ndk.ts @@ -1,55 +0,0 @@ -import NDK, { NDKEvent, NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk'; -import { time_now_ms } from "./client"; - -export const ndk_init = async (opts: { - $ndk: NDK; - secret_key: string; -}): Promise<NDKUser | undefined> => { - try { - const { $ndk: ndk, secret_key } = opts; - const signer = new NDKPrivateKeySigner(secret_key); - ndk.signer = signer; - - const user = await signer.user(); - if (user) { - user.ndk = ndk; - return user; - } - } catch (e) { - console.log(`(error) ndk_setup_privkey `, e); - } -}; - -export const ndk_event = async (opts: { - $ndk: NDK; - $ndk_user: NDKUser; - basis: { - kind: number; - content: string; - tags?: string[][]; - } -}): Promise<NDKEvent | undefined> => { - try { - const { $ndk: ndk, $ndk_user: ndk_user, basis } = opts; - const time_now = time_now_ms(); - - const tags: string[][] = [ - ['published_at', time_now.toString()], - ]; - - for (const tag of basis.tags || []) { - tags.push(tag); - }; - - const event: NDKEvent = new NDKEvent(ndk, { - kind: basis.kind, - pubkey: ndk_user.pubkey, - content: basis.content, - created_at: time_now, - tags - }); - return event; - } catch (e) { - console.log(`(error) ndk_event `, e); - } -}; diff --git a/apps-lib/src/lib/utils/routes.ts b/apps-lib/src/lib/utils/routes.ts @@ -1,5 +1,7 @@ export type NavigationRoute = | "/" + | "/map" + | "/map/choose-location" | "/models/location-gcs" | "/models/location-gcs/view-map" | "/models/nostr-profile" @@ -12,17 +14,19 @@ export type NavigationRoute = | "/models/trade-product/add/preview" | "/nostr" | "/nostr/keys" + | "/nostr/nip-99/view" | "/nostr/notes" | "/nostr/notes/post" | "/nostr/profile" | "/settings" | "/test" - | "/init" - | "/map"; + | "/init"; export function parse_route(route: string): NavigationRoute { switch (route) { case "/": + case "/map": + case "/map/choose-location": case "/models/location-gcs": case "/models/location-gcs/view-map": case "/models/nostr-profile": @@ -35,13 +39,13 @@ export function parse_route(route: string): NavigationRoute { case "/models/trade-product/add/preview": case "/nostr": case "/nostr/keys": + case "/nostr/nip-99/view": case "/nostr/notes": case "/nostr/notes/post": case "/nostr/profile": case "/settings": case "/test": case "/init": - case "/map": return route; default: return "/";