From ea07b55491e278ea0a4d6e70b5fe436055cad337 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 24 May 2024 13:12:06 +0100 Subject: [PATCH] feat: event previews for issues, patches and repos to be displayed when they are embedded into events --- .../components/events/EventWrapperLite.svelte | 2 +- .../events/content/IssuePreview.svelte | 21 ++ .../events/content/ParsedContent.svelte | 2 +- .../components/events/content/Patch.svelte | 335 +++++++++--------- src/lib/components/events/content/Repo.svelte | 19 + src/lib/components/events/content/utils.ts | 41 ++- src/lib/wrappers/EventCard.svelte | 24 +- src/lib/wrappers/EventPreview.svelte | 102 +++--- 8 files changed, 325 insertions(+), 221 deletions(-) create mode 100644 src/lib/components/events/content/IssuePreview.svelte create mode 100644 src/lib/components/events/content/Repo.svelte diff --git a/src/lib/components/events/EventWrapperLite.svelte b/src/lib/components/events/EventWrapperLite.svelte index 33b9622..501bbcd 100644 --- a/src/lib/components/events/EventWrapperLite.svelte +++ b/src/lib/components/events/EventWrapperLite.svelte @@ -18,6 +18,6 @@ - {created_at_ago} + {created_at_ago} diff --git a/src/lib/components/events/content/IssuePreview.svelte b/src/lib/components/events/content/IssuePreview.svelte new file mode 100644 index 0000000..11b9a42 --- /dev/null +++ b/src/lib/components/events/content/IssuePreview.svelte @@ -0,0 +1,21 @@ + + + + Git Issue for {pointer?.identifier}: {event.content?.split('\n')[0]} by + diff --git a/src/lib/components/events/content/ParsedContent.svelte b/src/lib/components/events/content/ParsedContent.svelte index b4d189c..6f1a155 100644 --- a/src/lib/components/events/content/ParsedContent.svelte +++ b/src/lib/components/events/content/ParsedContent.svelte @@ -43,7 +43,7 @@ {:else if isParsedNevent(part) || isParsedNote(part) || isParsedNaddr(part)} - + {:else if isParsedText(part)} {part.value} {/if} diff --git a/src/lib/components/events/content/Patch.svelte b/src/lib/components/events/content/Patch.svelte index 9180e60..ef2e22f 100644 --- a/src/lib/components/events/content/Patch.svelte +++ b/src/lib/components/events/content/Patch.svelte @@ -1,20 +1,28 @@ -
-
-
- -
-
commit
-
+{#if preview} + + Git Patch for {pointer?.identifier}: {commit_title} by + +{:else} +
+
+
+ +
+
commit
+
-
-
Changes:
-
- {commit_id_shorthand} +
+
Changes:
+
+ {commit_id_shorthand} +
-
- {#each files as file, index} -
-
- + - - -
- {#if expand_files[index]} -
-
- {#each file.chunks as chunk, index} - {#if index !== 0} -
-
- ... -
-
- {/if} - {#each chunk.changes as change, i} -
-
- {isAddChange(change) && - i !== 0 && - isDeleteChange(chunk.changes[i - 1]) - ? ' ' - : extractChangeLine(change)} -
-
- {/each} - {/each} -
-
-
+ }} + > + {expand_files[index] ? 'colapse' : 'expand'} + + +
+ {#if expand_files[index]} +
+
{#each file.chunks as chunk, index} {#if index !== 0} -
+
+
+ ... +
+
{/if} {#each chunk.changes as change, i}
- {#if getFortmattedDiffHtml(change, (file.to || file.from) - ?.split('.') - .pop() || '')} - - {@html getFortmattedDiffHtml( - change, - (file.to || file.from)?.split('.').pop() || '' - )} - {:else} - {change.type == 'normal' - ? change.content - : change.content.substring(1)} - {/if} - {#if (change.type == 'normal' ? change.content : change.content.substring(1)).length === 0} - - - {/if} + {isAddChange(change) && + i !== 0 && + isDeleteChange(chunk.changes[i - 1]) + ? ' ' + : extractChangeLine(change)}
{/each} {/each}
+
+
+ {#each file.chunks as chunk, index} + {#if index !== 0} +
+ {/if} + {#each chunk.changes as change, i} +
+
+ {#if getFortmattedDiffHtml(change, (file.to || file.from) + ?.split('.') + .pop() || '')} + + {@html getFortmattedDiffHtml( + change, + (file.to || file.from)?.split('.').pop() || '' + )} + {:else} + {change.type == 'normal' + ? change.content + : change.content.substring(1)} + {/if} + {#if (change.type == 'normal' ? change.content : change.content.substring(1)).length === 0} + + + {/if} +
+
+ {/each} + {/each} +
+
+ {/if} +
+ + {#if expand_full_files[index]} +
+ + {#each file.chunks as chunk, index} + {#if index !== 0} + + {/if} + {#each chunk.changes as _, i} +   + + {/each} + {/each}
{/if} -
- - {#if expand_full_files[index]} -
- - {#each file.chunks as chunk, index} - {#if index !== 0} - - {/if} - {#each chunk.changes as _, i} -   - - {/each} - {/each} -
- {/if} - {/each} -
+ {/each} +
+{/if} diff --git a/src/lib/components/events/content/Repo.svelte b/src/lib/components/events/content/Repo.svelte new file mode 100644 index 0000000..f1877a7 --- /dev/null +++ b/src/lib/components/events/content/Repo.svelte @@ -0,0 +1,19 @@ + + +{#if repo} + + Git Repository: {repo.name} by + +{/if} diff --git a/src/lib/components/events/content/utils.ts b/src/lib/components/events/content/utils.ts index 8aae4fc..ac3650c 100644 --- a/src/lib/components/events/content/utils.ts +++ b/src/lib/components/events/content/utils.ts @@ -1,5 +1,6 @@ import type { NDKTag } from '@nostr-dev-kit/ndk' import { nip19 } from 'nostr-tools' +import type { AddressPointer, EventPointer } from 'nostr-tools/lib/types/nip19' import { last } from 'ramda' export const TOPIC = 'topic' @@ -67,31 +68,29 @@ export const NOSTR_NOTE = 'nostr:note' type PartTypeNote = 'nostr:note' export type ParsedNote = { type: PartTypeNote - id: string - relays: string[] + data: EventPointer } export const NOSTR_NEVENT = 'nostr:nevent' type PartTypeNevent = 'nostr:nevent' export type ParsedNevent = { type: PartTypeNevent - id: string - relays: string[] + data: EventPointer } - export const NOSTR_NADDR = 'nostr:naddr' type PartTypeNaddr = 'nostr:naddr' export type ParsedNaddr = { type: PartTypeNaddr - identifier: string - pubkey: string - kind: number - relays: string[] + data: AddressPointer } - -export type ParsedNostrLink = ParsedNpub | ParsedNprofile | ParsedNevent | ParsedNote | ParsedNaddr +export type ParsedNostrLink = + | ParsedNpub + | ParsedNprofile + | ParsedNevent + | ParsedNote + | ParsedNaddr export const TEXT = 'text' type PartTypeText = 'text' @@ -113,7 +112,11 @@ export const isParsedLink = (part: ParsedPart): part is ParsedLink => part.type == LINK export const isParsedNostrLink = (part: ParsedPart): part is ParsedNostrLink => - part.type == NOSTR_NPUB || part.type == NOSTR_NPROFILE || part.type == NOSTR_NEVENT || part.type == NOSTR_NOTE || part.type == NOSTR_NADDR + part.type == NOSTR_NPUB || + part.type == NOSTR_NPROFILE || + part.type == NOSTR_NEVENT || + part.type == NOSTR_NOTE || + part.type == NOSTR_NADDR export const isParsedNpub = (part: ParsedPart): part is ParsedNpub => part.type == NOSTR_NPUB @@ -219,13 +222,13 @@ export const parseContent = (content: string, tags: NDKTag[]): ParsedPart[] => { return [bech32, { type: NOSTR_NPUB, hex: decoded.data.pubkey }] } if (decoded.type === 'note') { - return [bech32, { type: NOSTR_NOTE, id: decoded.data, relays: [] }] + return [bech32, { type: NOSTR_NOTE, data: { id: decoded.data } }] } if (decoded.type === 'nevent') { - return [bech32, { type: NOSTR_NEVENT, id: decoded.data.id, relays: decoded.data.relays || [] }] + return [bech32, { type: NOSTR_NEVENT, data: decoded.data }] } if (decoded.type === 'naddr') { - return [bech32, { ...decoded.data, type: NOSTR_NADDR, relays: decoded.data.relays || [] }] + return [bech32, { type: NOSTR_NADDR, data: decoded.data }] } } catch {} } @@ -266,6 +269,14 @@ export const isCoverLetter = (s: string): boolean => { return s.indexOf('PATCH 0/') > 0 } +export function extractTagContent( + name: string, + tags: NDKTag[] +): string | undefined { + const tag = tags.find((tag) => tag[0] === name) + return tag ? tag[1] : undefined +} + /** this doesn't work for all patch formats and options */ export const extractPatchMessage = (s: string): string | undefined => { try { diff --git a/src/lib/wrappers/EventCard.svelte b/src/lib/wrappers/EventCard.svelte index 5f12ff4..245310d 100644 --- a/src/lib/wrappers/EventCard.svelte +++ b/src/lib/wrappers/EventCard.svelte @@ -5,7 +5,12 @@ import Patch from '$lib/components/events/content/Patch.svelte' import ParsedContent from '$lib/components/events/content/ParsedContent.svelte' import { defaults as user_defaults } from '$lib/components/users/type' - import { patch_kind, proposal_status_kinds } from '$lib/kinds' + import { + issue_kind, + patch_kind, + proposal_status_kinds, + repo_kind, + } from '$lib/kinds' import { ensureUser } from '$lib/stores/users' import type { NDKEvent } from '@nostr-dev-kit/ndk' import { onDestroy } from 'svelte' @@ -14,9 +19,12 @@ extractPatchMessage, isCoverLetter, } from '$lib/components/events/content/utils' + import Repo from '$lib/components/events/content/Repo.svelte' + import IssuePreview from '$lib/components/events/content/IssuePreview.svelte' export let event: NDKEvent export let type: 'proposal' | 'issue' = 'proposal' + export let preview = false let author = writable({ ...user_defaults }) let author_unsubsriber: Unsubscriber @@ -45,6 +53,18 @@ added to '{getDtag(event) || 'unknown'}' list by +{:else if event.kind && event.kind == repo_kind} + + + +{:else if preview && event.kind && event.kind === patch_kind} + + + +{:else if preview && event.kind && event.kind === issue_kind} + + + {:else} {#if event.kind == patch_kind} @@ -54,7 +74,7 @@ tags={event.tags} /> {:else} - + {/if} {:else if event.kind && proposal_status_kinds.includes(event.kind)} diff --git a/src/lib/wrappers/EventPreview.svelte b/src/lib/wrappers/EventPreview.svelte index 71e0c07..aee4a97 100644 --- a/src/lib/wrappers/EventPreview.svelte +++ b/src/lib/wrappers/EventPreview.svelte @@ -1,58 +1,72 @@ -
- {#if $event && $event.pubkey} - - {:else if cannot_find_event} - cannot find event - {:else} - loading... - {/if} -
- +
+ {#if repo && $repo} + + + + {:else if $event && $event.pubkey} +
+ +
+ {:else if cannot_find_event} +
cannot find event
+ {:else} +
loading...
+ {/if} +