Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit 728e84f

Browse files
committed
edit / delete contact and nav from zap page
1 parent dda2f7a commit 728e84f

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/components/ActualSearch.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ export function ActualSearch(props: { initialValue?: string }) {
157157

158158
async function createContact(contact: ContactFormValues) {
159159
try {
160+
// First check if the contact already exists
161+
const existingContact = contacts()?.find(
162+
(c) => c.npub === contact.npub?.trim().toLowerCase()
163+
);
164+
165+
if (existingContact) {
166+
sendToContact(existingContact);
167+
return;
168+
}
169+
160170
const contactId = await state.mutiny_wallet?.create_new_contact(
161171
contact.name,
162172
contact.npub ? contact.npub.trim() : undefined,

src/components/NostrActivity.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,25 @@ export function NostrActivity() {
7777
// TODO: can this be part of mutiny wallet?
7878
async function newContactFromHexpub(hexpub: string) {
7979
try {
80+
const npub = await MutinyWallet.hexpub_to_npub(hexpub);
81+
82+
if (!npub) {
83+
throw new Error("No npub for that hexpub");
84+
}
85+
86+
const existingContact =
87+
await state.mutiny_wallet?.get_contact_for_npub(npub);
88+
89+
if (existingContact) {
90+
navigate(`/chat/${existingContact.id}`);
91+
return;
92+
}
93+
8094
const profile = data.latest?.profiles[hexpub];
8195
if (!profile) return;
8296
const parsed = JSON.parse(profile.content);
8397
const name = parsed.display_name || parsed.name || profile.pubkey;
8498
const image_url = parsed.image || parsed.picture || undefined;
85-
const npub = await MutinyWallet.hexpub_to_npub(hexpub);
8699
const ln_address = parsed.lud16 || undefined;
87100
const lnurl = parsed.lud06 || undefined;
88101

src/routes/Chat.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
BackPop,
2727
Button,
2828
ContactButton,
29+
ContactFormValues,
2930
ContactViewer,
3031
HackActivityType,
3132
IActivityItem,
@@ -37,7 +38,7 @@ import {
3738
} from "~/components";
3839
import { ParsedParams, toParsedParams } from "~/logic/waila";
3940
import { useMegaStore } from "~/state/megaStore";
40-
import { createDeepSignal, timeAgo } from "~/utils";
41+
import { createDeepSignal, eify, hexpubFromNpub, timeAgo } from "~/utils";
4142

4243
type CombinedMessagesAndActivity =
4344
| { kind: "message"; content: FakeDirectMessage }
@@ -425,6 +426,40 @@ export function Chat() {
425426
});
426427
}
427428

429+
async function saveContact(id: string, contact: ContactFormValues) {
430+
console.log("saving contact", id, contact);
431+
const hexpub = await hexpubFromNpub(contact.npub?.trim());
432+
try {
433+
const existing = state.mutiny_wallet?.get_tag_item(id);
434+
// This shouldn't happen
435+
if (!existing) throw new Error("No existing contact");
436+
await state.mutiny_wallet?.edit_contact(
437+
id,
438+
contact.name,
439+
hexpub ? hexpub : undefined,
440+
contact.ln_address ? contact.ln_address.trim() : undefined,
441+
existing.lnurl,
442+
existing.image_url
443+
);
444+
} catch (e) {
445+
console.error(e);
446+
showToast(eify(e));
447+
}
448+
449+
// TODO: refetch contact
450+
refetch();
451+
}
452+
453+
async function deleteContact(id: string) {
454+
try {
455+
await state.mutiny_wallet?.delete_contact(id);
456+
} catch (e) {
457+
console.error(e);
458+
showToast(eify(e));
459+
}
460+
navigate("/search");
461+
}
462+
428463
return (
429464
<Transition
430465
mode="outin"
@@ -461,7 +496,11 @@ export function Chat() {
461496
{/* <BackLink href="/" /> */}
462497
<BackPop default="/search" />
463498
<Show when={contact()}>
464-
<ContactViewer>
499+
<ContactViewer
500+
contact={contact()!}
501+
saveContact={saveContact}
502+
deleteContact={deleteContact}
503+
>
465504
<ContactButton
466505
contact={contact()!}
467506
onClick={() => {}}

0 commit comments

Comments
 (0)