Skip to content

Commit

Permalink
a bunch of stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed May 7, 2024
1 parent 7000b5b commit c06ea13
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 59 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
},
"type": "module",
"dependencies": {
"@snort/worker-relay": "^1.0.10",
"@tailwindcss/forms": "^0.5.6",
"@tailwindcss/typography": "^0.5.9",
"dataloader": "^2.2.2",
Expand Down
37 changes: 24 additions & 13 deletions src/cards/Article.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount } from 'svelte';
import type { Event, EventTemplate } from 'nostr-tools';
import { account, reactionKind, broadcast, _pool } from '$lib/nostr';
import { account, reactionKind, broadcast, _pool, wikiKind } from '$lib/nostr';
import { formatDate, getA, next } from '$lib/utils';
import type { ArticleTab, SearchTab, Tab } from '$lib/types';
import { page } from '$app/stores';
Expand All @@ -18,14 +18,15 @@
export let replaceSelf: (tab: Tab) => void;
let event: Event | null = null;
let copied = false;
let liked = false;
let disliked = false;
let likeStatus: 'liked' | 'disliked' | unknown;
let canLike: boolean | undefined;
const dTag = article[0];
const pubkey = article[1];
let author: NostrUser = bareNostrUser(pubkey);
let seenOn: string[] = [];
const articleTab = tab as ArticleTab;
$: title = event?.tags.find(([k]) => k === 'title')?.[1] || dTag;
$: summary = event?.tags.find(([k]) => k === 'summary')?.[1];
Expand Down Expand Up @@ -55,13 +56,19 @@
id: next(),
type: 'find',
data: dTag,
preferredAuthors: [event!.pubkey] // TODO: add more
preferredAuthors: [] // leave empty so we ensure the list of alternatives will be shown
};
if (ev.button === 1) createChild(nextTab);
else replaceSelf(nextTab);
}
onMount(() => {
if (articleTab.actualEvent) {
event = articleTab.actualEvent;
seenOn = articleTab.relayHints;
return;
}
(async () => {
let relays = await loadRelayList(pubkey);
Expand All @@ -73,12 +80,16 @@
[
{
authors: [pubkey],
'#d': [dTag]
'#d': [dTag],
kinds: [wikiKind]
}
],
{
id: 'article',
receivedEvent(relay, _id) {
seenOn.push(relay.url);
if (seenOn.indexOf(relay.url) === -1) {
seenOn.push(relay.url);
}
},
onevent(evt) {
if (!event || event.created_at < evt.created_at) {
Expand Down Expand Up @@ -178,14 +189,14 @@
class:hidden={$account?.pubkey === event.pubkey}
>
<a
title={canLike ? '' : liked ? 'you considered this a good article' : ''}
title={canLike ? '' : likeStatus === 'like' ? 'you considered this a good article' : ''}
class:cursor-pointer={canLike}
on:click={() => vote('+')}
>
<svg
class:fill-stone-600={canLike}
class:fill-cyan-500={liked}
class:hidden={disliked}
class:fill-cyan-500={likeStatus === 'like'}
class:hidden={likeStatus === 'disliked'}
width="18"
height="18"
viewBox="0 0 18 18"><path d="M1 12h16L9 4l-8 8Z"></path></svg
Expand All @@ -194,16 +205,16 @@
<a
title={canLike
? 'this is a bad article'
: disliked
: likeStatus === 'disliked'
? 'you considered this a bad article'
: ''}
class:cursor-pointer={canLike}
on:click={() => vote('-')}
>
<svg
class:fill-stone-600={canLike}
class:fill-rose-400={disliked}
class:hidden={liked}
class:fill-rose-400={likeStatus === 'disliked'}
class:hidden={likeStatus === 'liked'}
width="18"
height="18"
viewBox="0 0 18 18"><path d="M1 6h16l-8 8-8-8Z"></path></svg
Expand All @@ -216,7 +227,7 @@
<div>
by <UserLabel pubkey={event.pubkey} />
{#if event.created_at}
on {formatDate(event.created_at)}
{formatDate(event.created_at)}
{/if}
<!-- svelte-ignore a11y-no-static-element-interactions a11y-click-events-have-key-events a11y-missing-attribute -->
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/cards/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</div>
<div class="mt-2">
<details>
<summary> Add an explicit summary? </summary>
<summary>Add a summary?</summary>
<label
>Summary
<textarea
Expand Down
6 changes: 3 additions & 3 deletions src/cards/Relay.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
import { onMount } from 'svelte';
import { debounce } from 'debounce';
import type { Event } from 'nostr-tools';
import type { ArticleTab, Tab } from '$lib/types';
import { getTagOr, next } from '$lib/utils';
import { getTagOr, next, urlWithoutScheme } from '$lib/utils';
import { _pool, wikiKind } from '$lib/nostr';
import { debounce } from 'debounce';
import ArticleListItem from '$components/ArticleListItem.svelte';
export let tab: Tab;
Expand Down Expand Up @@ -58,7 +58,7 @@
}
</script>

<div class="mb-0 text-2xl break-all">{tab.data}</div>
<div class="mb-0 text-2xl break-all">{urlWithoutScheme(tab.data)}</div>
{#each results as result (result.id)}
<ArticleListItem event={result} {openArticle} />
{/each}
Expand Down
28 changes: 19 additions & 9 deletions src/cards/Search.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { Event, SubCloser } from 'nostr-tools';
import { _pool, wot, wikiKind, userWikiRelays } from '$lib/nostr';
import type { ArticleTab, Tab } from '$lib/types';
import type { ArticleTab, SearchTab, Tab } from '$lib/types';
import { getTagOr, next, normalizeArticleName } from '$lib/utils';
import { DEFAULT_SEARCH_RELAYS } from '$lib/defaults';
import ArticleListItem from '$components/ArticleListItem.svelte';
Expand All @@ -17,6 +17,8 @@
let results: Event[] = [];
let tried = false;
const searchTab = tab as SearchTab;
onMount(() => {
setTimeout(() => {
tried = true;
Expand All @@ -30,7 +32,7 @@
} else if (getTagOr(b, 'd') === tab.data && getTagOr(a, 'd') !== tab.data) {
return 1;
} else {
return $wot[b.pubkey] - $wot[a.pubkey];
return ($wot[b.pubkey] || 0) - ($wot[a.pubkey] || 0);
}
});
}, 500);
Expand All @@ -41,12 +43,19 @@
$userWikiRelays,
[{ kinds: [wikiKind], '#d': [normalizeArticleName(query)], limit: 25 }],
{
id: 'exactmatch',
id: 'find-exactmatch',
oneose() {
tried = true;
},
onevent(evt) {
tried = true;
if (searchTab.preferredAuthors.includes(evt.pubkey)) {
// we found an exact match that fits the list of preferred authors
// jump straight into it
openArticle(evt);
}
results.push(evt);
update();
},
Expand All @@ -60,7 +69,7 @@
);
search = _pool.subscribeMany(DEFAULT_SEARCH_RELAYS, [{ kinds: [wikiKind], search: query }], {
id: 'search',
id: 'find-search',
onevent(evt) {
results.push(evt);
update();
Expand All @@ -74,15 +83,16 @@
};
});
function openArticle(result: Event, ev: MouseEvent) {
function openArticle(result: Event, ev?: MouseEvent) {
let articleTab: ArticleTab = {
id: next(),
type: 'article',
data: [getTagOr(result, 'd'), result.pubkey],
relayHints: seenCache[result.id]
relayHints: seenCache[result.id],
actualEvent: result
};
if (ev.button === 1) createChild(articleTab);
else replaceSelf(articleTab);
if (ev?.button === 1) createChild(articleTab);
else replaceSelf({ ...articleTab, back: tab });
}
</script>

Expand All @@ -105,7 +115,7 @@
</button>
<button
on:click={() => createChild({ id: next(), type: 'settings' })}
class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
class="ml-1 inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
Add more relays
</button>
Expand Down
16 changes: 15 additions & 1 deletion src/components/ArticleContent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
import WikilinkComponent from './WikilinkComponent.svelte';
import type { Tab } from '$lib/types';
import { normalizeArticleName } from '$lib/utils';
import { onMount } from 'svelte';
import { loadWikiAuthors } from '$lib/lists';
export let event: NostrEvent;
export let createChild: (tab: Tab) => void;
let authorPreferredWikiAuthors: string[] = [];
onMount(() => {
loadWikiAuthors(event.pubkey).then((ps) => {
authorPreferredWikiAuthors = ps;
});
});
const content = event.content.replace(/\[\[(.*?)\]\]/g, (_: any, content: any) => {
let [target, display] = content.split('|');
display = display || target;
Expand All @@ -16,4 +26,8 @@
});
</script>

<SvelteMarkdown source={content} renderers={{ link: WikilinkComponent }} extra={createChild} />
<SvelteMarkdown
source={content}
renderers={{ link: WikilinkComponent }}
extra={{ createChild, preferredAuthors: [event.pubkey, ...authorPreferredWikiAuthors] }}
/>
2 changes: 2 additions & 0 deletions src/components/ArticleListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import MarkdownIt from 'markdown-it';
import UserLabel from './UserLabel.svelte';
import { formatDate } from '$lib/utils';
const mdTxt = new MarkdownIt().use(MarkdownItPlainText);
Expand Down Expand Up @@ -38,6 +39,7 @@
</h1>
<p class="text-xs">
by <UserLabel pubkey={event.pubkey} />
{formatDate(event.created_at)}
</p>
<p class="text-xs text-wrap break-words whitespace-pre-wrap">
{#if event.tags.find((e) => e[0] == 'summary')?.[0] && event.tags.find((e) => e[0] == 'summary')?.[1]}
Expand Down
36 changes: 33 additions & 3 deletions src/components/CardElement.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { tabs } from '$lib/state';
import type { Tab } from '$lib/types';
import { scrollTabIntoView, isElementInViewport, toURL, hashbow } from '$lib/utils';
import type { EditorTab, Tab } from '$lib/types';
import { scrollTabIntoView, isElementInViewport, hashbow, urlWithoutScheme } from '$lib/utils';
import Article from '$cards/Article.svelte';
import Editor from '$cards/Editor.svelte';
import Welcome from '$cards/Welcome.svelte';
Expand Down Expand Up @@ -100,6 +100,20 @@
function scrollIntoViewIfNecessary(ev: MouseEvent & { currentTarget: HTMLElement }) {
if (!isElementInViewport(ev.currentTarget)) scrollTabIntoView(ev.currentTarget, false);
}
function toURL(tab: Tab): string | null {
switch (tab.type) {
case 'find':
return tab.data;
case 'article':
return tab.data.join('*');
case 'relay':
return encodeURIComponent(urlWithoutScheme(tab.data));
case 'editor':
return 'edit:' + (tab as EditorTab).data.title;
}
return null;
}
</script>

<!-- svelte-ignore a11y-no-static-element-interactions a11y-click-events-have-key-events -->
Expand All @@ -121,7 +135,23 @@
: ''}
>
{#if tab.type !== 'welcome' && tab.type !== 'new'}
<div class="flex justify-end">
<div class="flex" class:justify-between={tab.back} class:justify-end={!tab.back}>
{#if tab.back}
<button on:click={close}>
<svg
fill="#000000"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 60.731 60.73"
xml:space="preserve"
><g stroke-linecap="round" stroke-linejoin="round"></g><g>
<polygon
points="0,30.365 29.737,60.105 29.737,42.733 60.731,42.729 60.731,18.001 29.737,17.999 29.737,0.625 "
></polygon>
</g></svg
>
</button>
{/if}
<button on:click={close}>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
8 changes: 4 additions & 4 deletions src/components/WikilinkComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<script lang="ts">
import type { Tab } from '$lib/types';
import type { SearchTab, Tab } from '$lib/types';
import { next } from '$lib/utils';
export let href: string;
export let title: string;
export let text: string;
export let extra: (tab: Tab) => void;
const createChild = extra;
export let extra: { preferredAuthors: string[]; createChild: (tab: Tab) => void };
const { preferredAuthors, createChild } = extra;
let wikitarget: string;
if (href.startsWith('wikilink:')) {
wikitarget = href.substring(9);
}
function handleWikilinkClick() {
createChild({ id: next(), type: 'find', data: wikitarget });
createChild({ id: next(), type: 'find', data: wikitarget, preferredAuthors } as SearchTab);
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion src/lib/nostr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SimplePool } from 'nostr-tools/pool';
import { loadNostrUser, type NostrUser } from './metadata';
import { DEFAULT_WIKI_RELAYS } from './defaults';
import { loadContactList, loadWikiAuthors, loadWikiRelaysList } from './lists';
import { unique } from './utils';

const startTime = Math.round(Date.now() / 1000);

Expand All @@ -31,7 +32,7 @@ export const signer = {
let setUserWikiRelays: (_: string) => Promise<void>;
export const userWikiRelays = readable<string[]>(DEFAULT_WIKI_RELAYS, (set) => {
setUserWikiRelays = async (pubkey: string) => {
const rl = await loadWikiRelaysList(pubkey);
const rl = unique(await loadWikiRelaysList(pubkey));
if (rl.length > 0) {
set(rl);
}
Expand Down
Loading

0 comments on commit c06ea13

Please sign in to comment.