import { MutinyWallet } from "@mutinywallet/mutiny-wasm";
import { createAsync, useNavigate } from "@solidjs/router";
import { Search } from "lucide-solid";
import {
createEffect,
createResource,
For,
Match,
Show,
Switch
} from "solid-js";
import { ButtonCard, NiceP } from "~/components/layout";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import { fetchZaps, getPrimalImageUrl } from "~/utils";
import { timeAgo } from "~/utils/prettyPrintTime";
import { GenericItem } from "./GenericItem";
export function Avatar(props: { image_url?: string; large?: boolean }) {
return (
?
);
}
export function NostrActivity() {
const i18n = useI18n();
const [state, _actions] = useMegaStore();
const npub = createAsync(async () => state.mutiny_wallet?.get_npub());
const [data, { refetch }] = createResource(npub, fetchZaps);
function nameFromHexpub(hexpub: string): string {
const profile = data.latest?.profiles[hexpub];
if (!profile) return hexpub;
const parsed = JSON.parse(profile.content);
const name = parsed.display_name || parsed.name;
return name;
}
function imageFromHexpub(hexpub: string): string | undefined {
const profile = data.latest?.profiles[hexpub];
if (!profile) return;
const parsed = JSON.parse(profile.content);
const image_url = parsed.image || parsed.picture;
return getPrimalImageUrl(image_url);
}
createEffect(() => {
// Should re-run after every sync
if (!state.is_syncing) {
refetch();
}
});
const navigate = useNavigate();
// 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);
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 ln_address = parsed.lud16 || undefined;
const lnurl = parsed.lud06 || undefined;
const contactId = await state.mutiny_wallet?.create_new_contact(
name,
npub,
ln_address,
lnurl,
image_url
);
if (!contactId) {
throw new Error("no contact id returned");
}
const tagItem = await state.mutiny_wallet?.get_tag_item(contactId);
if (!tagItem) {
throw new Error("no contact returned");
}
navigate(`/chat/${contactId}`);
} catch (e) {
console.error(e);
}
}
return (
navigate("/search")}>
{i18n.t("home.find")}
{(zap) => (
<>
{
newContactFromHexpub(zap.from_hexpub);
}}
secondaryAvatarUrl={
imageFromHexpub(zap.to_hexpub) || ""
}
secondaryName={nameFromHexpub(zap.to_hexpub)}
secondaryOnClick={() => {
newContactFromHexpub(zap.to_hexpub);
}}
verb={"zapped"}
amount={zap.amount_sats}
message={zap.content ? zap.content : undefined}
date={timeAgo(zap.timestamp, data.latest?.until)}
visibility={
zap.kind === "public" ? "public" : "private"
}
genericAvatar={
zap.kind === "anonymous" ||
zap.kind === "private"
}
forceSecondary
link={`https://njump.me/e/${zap.event_id}`}
/>
>
)}
);
}