feat: use memo for some components

This commit is contained in:
reya 2024-06-26 17:49:36 +07:00
parent 717c3e17df
commit 5c9b599b1e
6 changed files with 519 additions and 731 deletions

View File

@ -4,14 +4,14 @@ import { cn } from "@lume/utils";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { getCurrent } from "@tauri-apps/api/webviewWindow";
import { useCallback, useEffect, useRef, useState } from "react";
import { memo, useCallback, useEffect, useRef, useState } from "react";
type WindowEvent = {
scroll: boolean;
resize: boolean;
};
export function Column({
export const Column = memo(function Column({
column,
account,
}: {
@ -98,7 +98,7 @@ export function Column({
</div>
</div>
);
}
});
function Header({ label, name }: { label: string; name: string }) {
const [title, setTitle] = useState(name);

View File

@ -1,10 +1,10 @@
import { ThreadIcon } from "@lume/icons";
import { Note } from "@/components/note";
import { cn } from "@lume/utils";
import { ThreadIcon } from "@lume/icons";
import type { LumeEvent } from "@lume/system";
import { useMemo } from "react";
import { cn } from "@lume/utils";
import { memo, useMemo } from "react";
export function Conversation({
export const Conversation = memo(function Conversation({
event,
className,
}: {
@ -44,4 +44,4 @@ export function Conversation({
</Note.Root>
</Note.Provider>
);
}
});

View File

@ -1,9 +1,10 @@
import { QuoteIcon } from "@lume/icons";
import { Note } from "@/components/note";
import { cn } from "@lume/utils";
import { QuoteIcon } from "@lume/icons";
import type { LumeEvent } from "@lume/system";
import { cn } from "@lume/utils";
import { memo } from "react";
export function Quote({
export const Quote = memo(function Quote({
event,
className,
}: {
@ -40,4 +41,4 @@ export function Quote({
</Note.Root>
</Note.Provider>
);
}
});

View File

@ -1,11 +1,12 @@
import { Spinner } from "@lume/ui";
import { Note } from "@/components/note";
import { User } from "@/components/user";
import { type LumeEvent, NostrQuery } from "@lume/system";
import { Spinner } from "@lume/ui";
import { cn } from "@lume/utils";
import { useQuery } from "@tanstack/react-query";
import { type LumeEvent, NostrQuery } from "@lume/system";
import { memo } from "react";
export function RepostNote({
export const RepostNote = memo(function RepostNote({
event,
className,
}: {
@ -78,4 +79,4 @@ export function RepostNote({
)}
</Note.Root>
);
}
});

View File

@ -1,14 +1,17 @@
import { cn } from "@lume/utils";
import { Note } from "@/components/note";
import type { LumeEvent } from "@lume/system";
import { cn } from "@lume/utils";
import { memo } from "react";
export function TextNote({
export const TextNote = memo(function TextNote({
event,
className,
}: {
event: LumeEvent;
className?: string;
}) {
console.log("Rendered at: ", event.id, new Date().toLocaleTimeString());
return (
<Note.Provider event={event}>
<Note.Root
@ -31,4 +34,4 @@ export function TextNote({
</Note.Root>
</Note.Provider>
);
}
});

View File

@ -1,3 +1,4 @@
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.
/** user-defined commands **/
@ -13,10 +14,7 @@ export const commands = {
},
async connectRelay(relay: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("connect_relay", { relay }),
};
return { status: "ok", data: await TAURI_INVOKE("connect_relay", { relay }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -24,10 +22,7 @@ export const commands = {
},
async removeRelay(relay: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("remove_relay", { relay }),
};
return { status: "ok", data: await TAURI_INVOKE("remove_relay", { relay }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -43,10 +38,7 @@ export const commands = {
},
async saveBootstrapRelays(relays: string) : Promise<Result<null, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("save_bootstrap_relays", { relays }),
};
return { status: "ok", data: await TAURI_INVOKE("save_bootstrap_relays", { relays }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -68,29 +60,17 @@ export const commands = {
else return { status: "error", error: e as any };
}
},
async saveAccount(
nsec: string,
password: string,
): Promise<Result<string, string>> {
async saveAccount(nsec: string, password: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("save_account", { nsec, password }),
};
return { status: "ok", data: await TAURI_INVOKE("save_account", { nsec, password }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getEncryptedKey(
npub: string,
password: string,
): Promise<Result<string, string>> {
async getEncryptedKey(npub: string, password: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_encrypted_key", { npub, password }),
};
return { status: "ok", data: await TAURI_INVOKE("get_encrypted_key", { npub, password }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -98,10 +78,7 @@ export const commands = {
},
async getPrivateKey(npub: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_private_key", { npub }),
};
return { status: "ok", data: await TAURI_INVOKE("get_private_key", { npub }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -109,24 +86,15 @@ export const commands = {
},
async connectRemoteAccount(uri: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("connect_remote_account", { uri }),
};
return { status: "ok", data: await TAURI_INVOKE("connect_remote_account", { uri }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async loadAccount(
npub: string,
bunker: string | null,
): Promise<Result<boolean, string>> {
async loadAccount(npub: string, bunker: string | null) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("load_account", { npub, bunker }),
};
return { status: "ok", data: await TAURI_INVOKE("load_account", { npub, bunker }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -158,39 +126,15 @@ export const commands = {
},
async setContactList(publicKeys: string[]) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("set_contact_list", { publicKeys }),
};
return { status: "ok", data: await TAURI_INVOKE("set_contact_list", { publicKeys }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async createProfile(
name: string,
displayName: string,
about: string,
picture: string,
banner: string,
nip05: string,
lud16: string,
website: string,
): Promise<Result<string, string>> {
async createProfile(name: string, displayName: string, about: string, picture: string, banner: string, nip05: string, lud16: string, website: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("create_profile", {
name,
displayName,
about,
picture,
banner,
nip05,
lud16,
website,
}),
};
return { status: "ok", data: await TAURI_INVOKE("create_profile", { name, displayName, about, picture, banner, nip05, lud16, website }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -198,10 +142,7 @@ export const commands = {
},
async isContactListEmpty() : Promise<Result<boolean, null>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("is_contact_list_empty"),
};
return { status: "ok", data: await TAURI_INVOKE("is_contact_list_empty") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -209,24 +150,15 @@ export const commands = {
},
async checkContact(hex: string) : Promise<Result<boolean, null>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("check_contact", { hex }),
};
return { status: "ok", data: await TAURI_INVOKE("check_contact", { hex }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async toggleContact(
hex: string,
alias: string | null,
): Promise<Result<string, string>> {
async toggleContact(hex: string, alias: string | null) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("toggle_contact", { hex, alias }),
};
return { status: "ok", data: await TAURI_INVOKE("toggle_contact", { hex, alias }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -240,15 +172,9 @@ export const commands = {
else return { status: "error", error: e as any };
}
},
async setNstore(
key: string,
content: string,
): Promise<Result<string, string>> {
async setNstore(key: string, content: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("set_nstore", { key, content }),
};
return { status: "ok", data: await TAURI_INVOKE("set_nstore", { key, content }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -278,31 +204,17 @@ export const commands = {
else return { status: "error", error: e as any };
}
},
async zapProfile(
id: string,
amount: string,
message: string,
): Promise<Result<boolean, string>> {
async zapProfile(id: string, amount: string, message: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("zap_profile", { id, amount, message }),
};
return { status: "ok", data: await TAURI_INVOKE("zap_profile", { id, amount, message }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async zapEvent(
id: string,
amount: string,
message: string,
): Promise<Result<boolean, string>> {
async zapEvent(id: string, amount: string, message: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("zap_event", { id, amount, message }),
};
return { status: "ok", data: await TAURI_INVOKE("zap_event", { id, amount, message }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -310,10 +222,7 @@ export const commands = {
},
async friendToFriend(npub: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("friend_to_friend", { npub }),
};
return { status: "ok", data: await TAURI_INVOKE("friend_to_friend", { npub }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -337,24 +246,15 @@ export const commands = {
},
async setNewSettings(settings: string) : Promise<Result<null, null>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("set_new_settings", { settings }),
};
return { status: "ok", data: await TAURI_INVOKE("set_new_settings", { settings }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async verifyNip05(
key: string,
nip05: string,
): Promise<Result<boolean, string>> {
async verifyNip05(key: string, nip05: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("verify_nip05", { key, nip05 }),
};
return { status: "ok", data: await TAURI_INVOKE("verify_nip05", { key, nip05 }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -362,10 +262,7 @@ export const commands = {
},
async getEventMeta(content: string) : Promise<Result<Meta, null>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_event_meta", { content }),
};
return { status: "ok", data: await TAURI_INVOKE("get_event_meta", { content }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -379,15 +276,9 @@ export const commands = {
else return { status: "error", error: e as any };
}
},
async getEventFrom(
id: string,
relayHint: string,
): Promise<Result<RichEvent, string>> {
async getEventFrom(id: string, relayHint: string) : Promise<Result<RichEvent, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_event_from", { id, relayHint }),
};
return { status: "ok", data: await TAURI_INVOKE("get_event_from", { id, relayHint }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -403,10 +294,7 @@ export const commands = {
},
async listenEventReply(id: string) : Promise<Result<null, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("listen_event_reply", { id }),
};
return { status: "ok", data: await TAURI_INVOKE("listen_event_reply", { id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -414,108 +302,63 @@ export const commands = {
},
async unlistenEventReply(id: string) : Promise<Result<null, null>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("unlisten_event_reply", { id }),
};
return { status: "ok", data: await TAURI_INVOKE("unlisten_event_reply", { id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getEventsBy(
publicKey: string,
asOf: string | null,
): Promise<Result<RichEvent[], string>> {
async getEventsBy(publicKey: string, asOf: string | null) : Promise<Result<RichEvent[], string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_events_by", { publicKey, asOf }),
};
return { status: "ok", data: await TAURI_INVOKE("get_events_by", { publicKey, asOf }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getLocalEvents(
until: string | null,
): Promise<Result<RichEvent[], string>> {
async getLocalEvents(until: string | null) : Promise<Result<RichEvent[], string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_local_events", { until }),
};
return { status: "ok", data: await TAURI_INVOKE("get_local_events", { until }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getGroupEvents(
publicKeys: string[],
until: string | null,
): Promise<Result<RichEvent[], string>> {
async getGroupEvents(publicKeys: string[], until: string | null) : Promise<Result<RichEvent[], string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_group_events", { publicKeys, until }),
};
return { status: "ok", data: await TAURI_INVOKE("get_group_events", { publicKeys, until }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getGlobalEvents(
until: string | null,
): Promise<Result<RichEvent[], string>> {
async getGlobalEvents(until: string | null) : Promise<Result<RichEvent[], string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_global_events", { until }),
};
return { status: "ok", data: await TAURI_INVOKE("get_global_events", { until }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getHashtagEvents(
hashtags: string[],
until: string | null,
): Promise<Result<RichEvent[], string>> {
async getHashtagEvents(hashtags: string[], until: string | null) : Promise<Result<RichEvent[], string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("get_hashtag_events", { hashtags, until }),
};
return { status: "ok", data: await TAURI_INVOKE("get_hashtag_events", { hashtags, until }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async publish(
content: string,
warning: string | null,
difficulty: number | null,
): Promise<Result<string, string>> {
async publish(content: string, warning: string | null, difficulty: number | null) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("publish", { content, warning, difficulty }),
};
return { status: "ok", data: await TAURI_INVOKE("publish", { content, warning, difficulty }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async reply(
content: string,
to: string,
root: string | null,
): Promise<Result<string, string>> {
async reply(content: string, to: string, root: string | null) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("reply", { content, to, root }),
};
return { status: "ok", data: await TAURI_INVOKE("reply", { content, to, root }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -531,10 +374,7 @@ export const commands = {
},
async eventToBech32(id: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("event_to_bech32", { id }),
};
return { status: "ok", data: await TAURI_INVOKE("event_to_bech32", { id }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -542,10 +382,7 @@ export const commands = {
},
async userToBech32(user: string) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("user_to_bech32", { user }),
};
return { status: "ok", data: await TAURI_INVOKE("user_to_bech32", { user }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -556,10 +393,7 @@ export const commands = {
},
async createColumn(column: Column) : Promise<Result<string, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("create_column", { column }),
};
return { status: "ok", data: await TAURI_INVOKE("create_column", { column }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -567,40 +401,23 @@ export const commands = {
},
async closeColumn(label: string) : Promise<Result<boolean, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("close_column", { label }),
};
return { status: "ok", data: await TAURI_INVOKE("close_column", { label }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async repositionColumn(
label: string,
x: number,
y: number,
): Promise<Result<null, string>> {
async repositionColumn(label: string, x: number, y: number) : Promise<Result<null, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("reposition_column", { label, x, y }),
};
return { status: "ok", data: await TAURI_INVOKE("reposition_column", { label, x, y }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async resizeColumn(
label: string,
width: number,
height: number,
): Promise<Result<null, string>> {
async resizeColumn(label: string, width: number, height: number) : Promise<Result<null, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("resize_column", { label, width, height }),
};
return { status: "ok", data: await TAURI_INVOKE("resize_column", { label, width, height }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -608,10 +425,7 @@ export const commands = {
},
async openWindow(window: Window) : Promise<Result<null, string>> {
try {
return {
status: "ok",
data: await TAURI_INVOKE("open_window", { window }),
};
return { status: "ok", data: await TAURI_INVOKE("open_window", { window }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@ -622,59 +436,26 @@ export const commands = {
},
async setBadge(count: number) : Promise<void> {
await TAURI_INVOKE("set_badge", { count });
},
};
}
}
/** user-defined events **/
/** user-defined statics **/
/** user-defined types **/
export type Account = { npub: string; nsec: string };
export type Column = {
label: string;
url: string;
x: number;
y: number;
width: number;
height: number;
};
export type Meta = {
content: string;
images: string[];
videos: string[];
events: string[];
mentions: string[];
hashtags: string[];
};
export type Relays = {
connected: string[];
read: string[] | null;
write: string[] | null;
both: string[] | null;
};
export type RichEvent = { raw: string; parsed: Meta | null };
export type Settings = {
proxy: string | null;
image_resize_service: string | null;
use_relay_hint: boolean;
content_warning: boolean;
display_avatar: boolean;
display_zap_button: boolean;
display_repost_button: boolean;
display_media: boolean;
};
export type Window = {
label: string;
title: string;
url: string;
width: number;
height: number;
maximizable: boolean;
minimizable: boolean;
hidden_title: boolean;
};
export type Account = { npub: string; nsec: string }
export type Column = { label: string; url: string; x: number; y: number; width: number; height: number }
export type Meta = { content: string; images: string[]; videos: string[]; events: string[]; mentions: string[]; hashtags: string[] }
export type Relays = { connected: string[]; read: string[] | null; write: string[] | null; both: string[] | null }
export type RichEvent = { raw: string; parsed: Meta | null }
export type Settings = { proxy: string | null; image_resize_service: string | null; use_relay_hint: boolean; content_warning: boolean; display_avatar: boolean; display_zap_button: boolean; display_repost_button: boolean; display_media: boolean }
export type Window = { label: string; title: string; url: string; width: number; height: number; maximizable: boolean; minimizable: boolean; hidden_title: boolean }
/** tauri-specta globals **/
@ -684,10 +465,10 @@ import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webview
type __EventObj__<T> = {
listen: (
cb: TAURI_API_EVENT.EventCallback<T>,
cb: TAURI_API_EVENT.EventCallback<T>
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
cb: TAURI_API_EVENT.EventCallback<T>
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: T extends null
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
@ -699,7 +480,7 @@ export type Result<T, E> =
| { status: "error"; error: E };
function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>,
mappings: Record<keyof T, string>
) {
return new Proxy(
{} as unknown as {
@ -729,6 +510,8 @@ function __makeEvents__<T extends Record<string, any>>(
},
});
},
},
}
);
}