aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Feed.svelte
blob: 7de087a724b834658ed88f83b5db61338b8b0353 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<script>
	import { t } from '$lib/translations';
	import { onMount } from 'svelte';
	import { Event } from '$comp';
	import { browser } from '$app/environment';
	import VirtualScroll from 'svelte-virtual-scroll-list';
	import { fail } from '$lib/utils';
	import { sign, send } from '$lib/nostr';

	export let events;
	export let user = undefined;

	let message;

	let submit = async () => {
		try {
			let mnemonic, key, seed, entropy, child, privkey;

			let event = {
				pubkey: user.pubkey,
				created_at: Math.floor(Date.now() / 1000),
				kind: 1,
				content: message,
				tags: []
			};

			await sign({ event, user });
			await send(event);

			event.user = user;
			event.seen = event.created_at;

			events[event.id] = event;
			events = events;
		} catch (e) {
			console.log(e, e.stack);
			fail('Problem posting event');
		}
	};

	$: sorted = Object.values(events)
		.filter((ev) => ev?.pubkey && ev?.kind === 1)
		.sort((a, b) => b.seen - a.seen);
</script>

{#if user}
	<form on:submit|preventDefault={submit} class="flex justify-center gap-4 max-w-2xl mx-auto">
		<div class="grow">
			<input
				name="message"
				bind:value={message}
				placeholder="What's happening?"
				class="my-5 mx-auto"
			/>
		</div>
		<button class="rounded-full border py-4 px-5 font-bold hover:opacity-80 w-40 my-5">Post</button>
	</form>
{/if}

{#if browser}
	{#if sorted.length}
		<VirtualScroll data={sorted} key="id" let:data pageMode={true} estimateSize={200}>
			<a href={`/${data.user.pubkey}/event/${data.id}`}>
				<Event event={data} />
			</a>
		</VirtualScroll>
	{:else}
		<div class="flex w-full">
			<div class="mx-auto text-secondary">{$t('user.noNotes')}</div>
		</div>
	{/if}
{/if}