Browse Source

Fix #133 make anon profiles better, include avatar in the link

master
Nuša Pukšič 1 year ago
parent
commit
7ce41bbeb9
  1. 28
      src/lib/components/util/InlineProfile.svelte

28
src/lib/components/util/InlineProfile.svelte

@ -1,12 +1,13 @@
<script lang='ts'> <script lang='ts'>
import { Avatar } from 'flowbite-svelte'; import { Avatar } from 'flowbite-svelte';
import { type NDKUserProfile } from '@nostr-dev-kit/ndk'; import { type NDKUserProfile } from "@nostr-dev-kit/ndk";
import { ndkInstance } from '$lib/ndk'; import { ndkInstance } from '$lib/ndk';
let { pubkey, title = null } = $props(); let { pubkey, title = null } = $props();
const externalProfileDestination = 'https://njump.me/' const externalProfileDestination = 'https://njump.me/'
let loading = $state(true); let loading = $state(true);
let anon = $state(false);
let npub = $state(''); let npub = $state('');
let profile = $state<NDKUserProfile | null>(null); let profile = $state<NDKUserProfile | null>(null);
@ -14,14 +15,16 @@
let username = $derived(profile?.name); let username = $derived(profile?.name);
async function fetchUserData(pubkey: string) { async function fetchUserData(pubkey: string) {
const user = $ndkInstance let user;
.getUser({ pubkey: pubkey ?? undefined }); user = $ndkInstance
.getUser({ pubkey: pubkey ?? undefined });
npub = user.npub; npub = user.npub;
user.fetchProfile() user.fetchProfile()
.then(userProfile => { .then(userProfile => {
profile = userProfile; profile = userProfile;
if (!profile?.name) anon = true;
loading = false; loading = false;
}); });
} }
@ -33,17 +36,24 @@
} }
}); });
function shortenNpub(long: string|undefined) {
if (!long) return '';
return long.slice(0, 8) + '…' + long.slice(-4);
}
</script> </script>
{#if loading} {#if loading}
<span></span> {title ?? '…'}
{:else if pubkey} {:else if anon }
<Avatar rounded <a class='underline' href='{externalProfileDestination}{npub}' title={title ?? npub} target='_blank'>{shortenNpub(npub)}</a>
{:else if npub }
<a href='{externalProfileDestination}{npub}' title={title ?? username} target='_blank'>
<Avatar rounded
class='h-6 w-6 mx-1 cursor-pointer inline' class='h-6 w-6 mx-1 cursor-pointer inline'
src={pfp} src={pfp}
alt={username} /> alt={username} />
<a class='underline' href='{externalProfileDestination}{npub}' title={title ?? username} target='_blank'>{username}</a> <span class='underline'>{username ?? shortenNpub(npub)}</span>
</a>
{:else} {:else}
<span>Not found</span> {title ?? pubkey}
{/if} {/if}
Loading…
Cancel
Save