Skip to content

Commit

Permalink
edit / delete contact and nav from zap page
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Feb 6, 2024
1 parent dda2f7a commit 728e84f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/components/ActualSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ export function ActualSearch(props: { initialValue?: string }) {

async function createContact(contact: ContactFormValues) {
try {
// First check if the contact already exists
const existingContact = contacts()?.find(
(c) => c.npub === contact.npub?.trim().toLowerCase()
);

if (existingContact) {
sendToContact(existingContact);
return;
}

const contactId = await state.mutiny_wallet?.create_new_contact(
contact.name,
contact.npub ? contact.npub.trim() : undefined,
Expand Down
15 changes: 14 additions & 1 deletion src/components/NostrActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,25 @@ export function NostrActivity() {
// TODO: can this be part of mutiny wallet?
async function newContactFromHexpub(hexpub: string) {
try {
const npub = await MutinyWallet.hexpub_to_npub(hexpub);

if (!npub) {
throw new Error("No npub for that hexpub");
}

const existingContact =
await state.mutiny_wallet?.get_contact_for_npub(npub);

Check failure on line 87 in src/components/NostrActivity.tsx

View workflow job for this annotation

GitHub Actions / Build iOS

Property 'get_contact_for_npub' does not exist on type 'MutinyWallet'.

Check failure on line 87 in src/components/NostrActivity.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Property 'get_contact_for_npub' does not exist on type 'MutinyWallet'.

Check failure on line 87 in src/components/NostrActivity.tsx

View workflow job for this annotation

GitHub Actions / Build APK

Property 'get_contact_for_npub' does not exist on type 'MutinyWallet'.

if (existingContact) {
navigate(`/chat/${existingContact.id}`);
return;
}

const profile = data.latest?.profiles[hexpub];
if (!profile) return;
const parsed = JSON.parse(profile.content);
const name = parsed.display_name || parsed.name || profile.pubkey;
const image_url = parsed.image || parsed.picture || undefined;
const npub = await MutinyWallet.hexpub_to_npub(hexpub);
const ln_address = parsed.lud16 || undefined;
const lnurl = parsed.lud06 || undefined;

Expand Down
43 changes: 41 additions & 2 deletions src/routes/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BackPop,
Button,
ContactButton,
ContactFormValues,
ContactViewer,
HackActivityType,
IActivityItem,
Expand All @@ -37,7 +38,7 @@ import {
} from "~/components";
import { ParsedParams, toParsedParams } from "~/logic/waila";
import { useMegaStore } from "~/state/megaStore";
import { createDeepSignal, timeAgo } from "~/utils";
import { createDeepSignal, eify, hexpubFromNpub, timeAgo } from "~/utils";

type CombinedMessagesAndActivity =
| { kind: "message"; content: FakeDirectMessage }
Expand Down Expand Up @@ -425,6 +426,40 @@ export function Chat() {
});
}

async function saveContact(id: string, contact: ContactFormValues) {
console.log("saving contact", id, contact);
const hexpub = await hexpubFromNpub(contact.npub?.trim());
try {
const existing = state.mutiny_wallet?.get_tag_item(id);
// This shouldn't happen
if (!existing) throw new Error("No existing contact");
await state.mutiny_wallet?.edit_contact(
id,
contact.name,
hexpub ? hexpub : undefined,
contact.ln_address ? contact.ln_address.trim() : undefined,
existing.lnurl,
existing.image_url
);
} catch (e) {
console.error(e);
showToast(eify(e));
}

// TODO: refetch contact
refetch();
}

async function deleteContact(id: string) {
try {
await state.mutiny_wallet?.delete_contact(id);
} catch (e) {
console.error(e);
showToast(eify(e));
}
navigate("/search");
}

return (
<Transition
mode="outin"
Expand Down Expand Up @@ -461,7 +496,11 @@ export function Chat() {
{/* <BackLink href="/" /> */}
<BackPop default="/search" />
<Show when={contact()}>
<ContactViewer>
<ContactViewer
contact={contact()!}
saveContact={saveContact}
deleteContact={deleteContact}
>
<ContactButton
contact={contact()!}
onClick={() => {}}
Expand Down

0 comments on commit 728e84f

Please sign in to comment.