Skip to content

Commit

Permalink
converted all svelte components to TS, moved debug settings into nost…
Browse files Browse the repository at this point in the history
…rize settings
  • Loading branch information
dhalsim committed Oct 3, 2024
1 parent bd274c1 commit 9201d9b
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 148 deletions.
11 changes: 5 additions & 6 deletions src/components/checkbox/custom-checkbox.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script>
export let id;
export let text;
export let checked;
export let onclick;
<script lang="ts">
export let id: string | null = null;
export let text: string | null = null;
export let checked: boolean;
export let onclick: ((checked: boolean) => void) | null = null;
export let tooltip = "";
function handleClick() {
Expand All @@ -29,7 +29,6 @@
class="custom-checkbox"
on:click={handleClick}
class:checked
for={id}
data-title={tooltip}
class:hastooltip={tooltip !== ""}
></button>
Expand Down
2 changes: 1 addition & 1 deletion src/components/loading.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script lang="ts">
export let text = "Loading...";
export let size = 40;
export let strokeWidth = 2.5;
Expand Down
6 changes: 3 additions & 3 deletions src/components/qrCode.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script lang="ts">
import { toString as toSvgString } from "qrcode/lib/browser.js";
import { onMount } from "svelte";
Expand All @@ -7,7 +7,7 @@
export let value = "";
export let width = 256;
let svg = null;
let svg: string | null = null;
async function generateQRCode() {
svg = await toSvgString(value);
Expand All @@ -31,5 +31,5 @@
{@html svg}
</div>
{:else}
<Loading text={null} size={width} />
<Loading size={width} />
{/if}
22 changes: 16 additions & 6 deletions src/components/tooltip/tooltip.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<script>
<script lang="ts">
export let text;
export let title;
export let title: string | undefined = "";
export let iconText = "";
export let iconClick = null;
function showTooltip(e) {
const tooltip = e.target.nextElementSibling;
function showTooltip(e: Event) {
const tooltip = (e.target as HTMLElement).nextElementSibling;
if (!(tooltip instanceof HTMLElement)) {
throw new Error("Tooltip element not found");
}
tooltip.style.display = "block";
// Check if tooltip is going off the top of the screen
Expand All @@ -18,8 +23,13 @@
}
}
function hideTooltip(e) {
const tooltip = e.target.nextElementSibling;
function hideTooltip(e: Event) {
const tooltip = (e.target as HTMLElement).nextElementSibling;
if (!(tooltip instanceof HTMLElement)) {
throw new Error("Tooltip element not found");
}
tooltip.style.display = "none";
tooltip.classList.remove("below");
}
Expand Down
11 changes: 6 additions & 5 deletions src/helpers/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { Either } from "./either";
import { Some } from "./some";

export const defaultSettings: Settings = {
version: 5,
debug: {
log: true,
namespace: "[N]",
},
version: 6,
nostrSettings: {
mode: "anon",
relays: {
Expand Down Expand Up @@ -44,6 +40,7 @@ export const defaultSettings: Settings = {
},
nostrConnect: {
url: "",
bunkerUrl: "",
customRelay: "",
username: "",
provider: "",
Expand All @@ -62,6 +59,10 @@ export const defaultSettings: Settings = {
},
nostrizeSettings: {
alwaysOpenInNewTab: false,
debug: {
enableLogging: false,
namespace: "[N]",
},
},
};

Expand Down
11 changes: 5 additions & 6 deletions src/helpers/accounts.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export type DebugSettings = {
log: boolean;
namespace: string;
};

export type RelayConfig = {
relay: string;
enabled: boolean;
Expand All @@ -21,6 +16,7 @@ export type RelaySettings = {

export type NostrConnectSettings = {
url: string;
bunkerUrl: string;
customRelay: string;
username: string;
provider: string;
Expand Down Expand Up @@ -53,11 +49,14 @@ export type LightsatsSettings = {

export type NostrizeSettings = {
alwaysOpenInNewTab: boolean;
debug: {
enableLogging: boolean;
namespace: string;
};
};

export type Settings = {
version: number;
debug: DebugSettings;
nostrSettings: NostrSettings;
lightsatsSettings: LightsatsSettings;
nostrizeSettings: NostrizeSettings;
Expand Down
48 changes: 39 additions & 9 deletions src/helpers/nip65.js → src/helpers/nip65.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { SimplePool } from "nostr-tools";
import { SimplePool, type Event } from "nostr-tools";

import { getOrInsertPageCache } from "./local-cache.js";

function toReadWriteRelays(relays) {
interface Relay {
relay: string;
read: boolean;
write: boolean;
}

interface ReadWriteRelays {
readRelays: string[];
writeRelays: string[];
flatRelays: Relay[];
}

function toReadWriteRelays(relays: Event): ReadWriteRelays {
const relayTags = relays.tags.filter((tag) => tag[0] === "r");

const readRelays = relayTags
Expand All @@ -22,31 +34,49 @@ function toReadWriteRelays(relays) {
return { readRelays, writeRelays, flatRelays };
}

export async function getNip65Relays({ pubkey, relays, updatedCallback }) {
const response = await getOrInsertPageCache({
interface GetNip65RelaysParams {
pubkey: string;
relays: string[];
updatedCallback?: (relays: ReadWriteRelays) => void;
}

export async function getNip65Relays({
pubkey,
relays,
updatedCallback,
}: GetNip65RelaysParams): Promise<ReadWriteRelays> {
const response = (await getOrInsertPageCache({
key: `nostrize-nip65-${pubkey}-${relays.join("")}`,
insertCallback: () => fetchRelays({ pubkey, relays }),
skipEmpty: true,
updateCache: true,
updatedCallback: (response) => {
updatedCallback: (response: Event | null) => {
if (updatedCallback) {
if (response == null) {
updatedCallback({ readRelays: [], writeRelays: [] });
updatedCallback({ readRelays: [], writeRelays: [], flatRelays: [] });
} else {
updatedCallback(toReadWriteRelays(response));
}
}
},
});
})) as Event | null;

if (response == null) {
return { readRelays: [], writeRelays: [] };
return { readRelays: [], writeRelays: [], flatRelays: [] };
}

return toReadWriteRelays(response);
}

async function fetchRelays({ pubkey, relays }) {
interface FetchRelaysParams {
pubkey: string;
relays: string[];
}

async function fetchRelays({
pubkey,
relays,
}: FetchRelaysParams): Promise<Event | null> {
const pool = new SimplePool();

return pool.get(relays, {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/relays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getNip07Relays } from "./nip07.js";
import { getNip65Relays } from "./nip65.js";
import { getNip65Relays } from "./nip65.ts";
import { uniqueArrays } from "./utils.js";

export async function getNostrizeUserRelays({ settings, pubkey }) {
Expand Down
49 changes: 29 additions & 20 deletions src/settings/bunker.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<script>
<script lang="ts">
import { finalizeEvent, nip04, Relay } from "nostr-tools";
import Tooltip from "../components/tooltip/tooltip.svelte";
import "./common.css";
import type { NostrConnectSettings } from "../helpers/accounts.types";
import { createKeyPair } from "../helpers/crypto";
import { generateRandomHexString } from "../helpers/utils";
import Loading from "../components/loading.svelte";
import Tooltip from "../components/tooltip/tooltip.svelte";
export let nostrConnectSettings;
import "./common.css";
export let nostrConnectSettings: NostrConnectSettings;
let bunkerError = null;
let bunkerSuccess = null;
let bunkerError: string | null = null;
let bunkerSuccess: string | null = null;
let bunkerLoading = false;
async function connectToBunker() {
Expand Down Expand Up @@ -69,7 +70,8 @@
const signedEvent = finalizeEvent(
eventTemplate,
nostrConnectSettings.ephemeralKey,
// actually it also accepts string
nostrConnectSettings.ephemeralKey as unknown as Uint8Array,
);
const relay = new Relay(providerRelay);
Expand Down Expand Up @@ -100,7 +102,11 @@
try {
parsed = JSON.parse(decrypted);
} catch (e) {
bunkerError = e.message;
if (e instanceof Error) {
bunkerError = e.message;
} else {
bunkerError = String(e);
}
reject(e);
Expand All @@ -121,10 +127,13 @@
bunkerLoading = false;
resolve();
resolve(null);
} catch (e) {
bunkerError = e.message;
if (e instanceof Error) {
bunkerError = e.message;
} else {
bunkerError = String(e);
}
bunkerLoading = false;
reject(e);
Expand All @@ -145,7 +154,11 @@
);
});
} catch (error) {
bunkerError = error.message;
if (error instanceof Error) {
bunkerError = error.message;
} else {
bunkerError = String(error);
}
bunkerLoading = false;
}
Expand Down Expand Up @@ -206,7 +219,8 @@
const signedEvent = finalizeEvent(
eventTemplate,
nostrConnectSettings.ephemeralKey,
// actually it also accepts string
nostrConnectSettings.ephemeralKey as unknown as Uint8Array,
);
const r = new Relay(nostrConnectSettings.providerRelay);
Expand Down Expand Up @@ -300,12 +314,7 @@
</button>

{#if bunkerLoading}
<Loading
size={20}
strokeWidth={4}
text={null}
strokeColor="rgba(130 80 223 / 75%)"
/>
<Loading size={20} strokeWidth={4} strokeColor="rgba(130 80 223 / 75%)" />
{/if}
</div>

Expand Down
51 changes: 0 additions & 51 deletions src/settings/debug.svelte

This file was deleted.

Loading

0 comments on commit 9201d9b

Please sign in to comment.