fix lints for linkify and notes

This commit is contained in:
Paul Miller
2023-04-11 17:52:47 -05:00
parent b9eef23fbf
commit 309c642e51
2 changed files with 18 additions and 15 deletions

View File

@@ -1,20 +1,22 @@
import { JSX } from 'solid-js';
interface LinkifyProps {
text: string;
initialText: string;
}
export default function Linkify(props: LinkifyProps): JSX.Element {
// By naming this "initialText" we can prove to eslint that the props won't change
const text = props.initialText;
const links: (string | JSX.Element)[] = [];
const pattern = /((https?:\/\/|www\.)\S+)/gi;
let lastIndex = 0;
let match;
while ((match = pattern.exec(props.text)) !== null) {
while ((match = pattern.exec(text)) !== null) {
const link = match[1];
const href = link.startsWith('http') ? link : `https://${link}`;
const beforeLink = props.text.slice(lastIndex, match.index);
const beforeLink = text.slice(lastIndex, match.index);
lastIndex = pattern.lastIndex;
if (beforeLink) {
@@ -24,7 +26,7 @@ export default function Linkify(props: LinkifyProps): JSX.Element {
links.push(<a href={href} target="_blank" rel="noopener noreferrer">{link}</a>);
}
const remainingText = props.text.slice(lastIndex);
const remainingText = text.slice(lastIndex);
if (remainingText) {
links.push(remainingText);
}

View File

@@ -1,4 +1,4 @@
import { Component, For } from "solid-js";
import { Component, For, createEffect, createSignal } from "solid-js";
import { Event, nip19 } from "nostr-tools"
import { Linkify } from "~/components/layout";
@@ -8,26 +8,27 @@ type NostrEvent = {
}
const Note: Component<{ e: NostrEvent }> = (props) => {
const e = props.e;
const date = new Date(e.created_at * 1000);
const linkRoot = "https://snort.social/e/";
let noteId;
const [noteId, setNoteId] = createSignal("");
createEffect(() => {
if (props.e.id) {
setNoteId(nip19.noteEncode(props.e.id))
}
})
if (e.id) {
noteId = nip19.noteEncode(e.id)
}
return (
<div class="flex gap-4 border-b border-faint-white py-6 items-start w-full">
<img class="bg-black rounded-xl flex-0" src="../180.png" width={45} height={45} />
<div class="flex flex-col gap-2 flex-1">
<p class="break-words">
<Linkify text={e.content} />
{/* {props.e.content} */}
<Linkify initialText={props.e.content} />
</p>
<a class="no-underline hover:underline hover:decoration-light-text" href={`${linkRoot}${noteId}`}>
<small class="text-light-text">{date.toLocaleString()}</small>
<a class="no-underline hover:underline hover:decoration-light-text" href={`${linkRoot}${noteId()}`}>
<small class="text-light-text">{(new Date(props.e.created_at * 1000)).toLocaleString()}</small>
</a>
</div>
</div>