feat(columns): update timeline column

This commit is contained in:
reya 2023-12-27 15:01:40 +07:00
parent b4dac2d477
commit ed538c91c6
14 changed files with 282 additions and 130 deletions

View File

@ -6,11 +6,13 @@
"dependencies": {
"@lume/ark": "workspace:^",
"@lume/icons": "workspace:^",
"@lume/ui": "workspace:^",
"@lume/utils": "workspace:^",
"@nostr-dev-kit/ndk": "^2.3.1",
"@tanstack/react-query": "^5.14.2",
"react": "^18.2.0",
"react-router-dom": "^6.21.0",
"sonner": "^1.2.4",
"virtua": "^0.18.0"
},
"devDependencies": {

View File

@ -87,9 +87,8 @@ export function HomeRoute({ colKey }: { colKey: string }) {
<div className="w-full h-full">
<VList ref={ref} cache={cache} overscan={2} className="flex-1 px-3">
{isLoading ? (
<div className="inline-flex h-16 items-center justify-center gap-2 px-3 py-1.5">
<LoaderIcon className="size-5" />
Loading
<div className="w-full flex h-16 items-center justify-center gap-2 px-3 py-1.5">
<LoaderIcon className="size-5 animate-spin" />
</div>
) : (
allEvents.map((item) => renderItem(item))

View File

@ -1,7 +1,213 @@
import { useParams } from "react-router-dom";
import {
RepostNote,
TextNote,
useArk,
useProfile,
useStorage,
} from "@lume/ark";
import { ArrowLeftIcon, ArrowRightCircleIcon, LoaderIcon } from "@lume/icons";
import { NIP05 } from "@lume/ui";
import { FETCH_LIMIT, displayNpub } from "@lume/utils";
import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk";
import { useInfiniteQuery } from "@tanstack/react-query";
import { useEffect, useMemo, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { toast } from "sonner";
import { WVList } from "virtua";
export function UserRoute() {
const { id } = useParams();
const ark = useArk();
const storage = useStorage();
const navigate = useNavigate();
return <div>{id}</div>;
const { id } = useParams();
const { user } = useProfile(id);
const { data, hasNextPage, isLoading, isFetchingNextPage, fetchNextPage } =
useInfiniteQuery({
queryKey: ["user-posts", id],
initialPageParam: 0,
queryFn: async ({
signal,
pageParam,
}: {
signal: AbortSignal;
pageParam: number;
}) => {
const events = await ark.getInfiniteEvents({
filter: {
kinds: [NDKKind.Text, NDKKind.Repost],
authors: [id],
},
limit: FETCH_LIMIT,
pageParam,
signal,
});
return events;
},
getNextPageParam: (lastPage) => {
const lastEvent = lastPage.at(-1);
if (!lastEvent) return;
return lastEvent.created_at - 1;
},
refetchOnWindowFocus: false,
});
const [followed, setFollowed] = useState(false);
const allEvents = useMemo(
() => (data ? data.pages.flatMap((page) => page) : []),
[data],
);
const follow = async (pubkey: string) => {
try {
const add = await ark.createContact({ pubkey });
if (add) {
setFollowed(true);
} else {
toast.success("You already follow this user");
}
} catch (error) {
console.log(error);
}
};
const unfollow = async (pubkey: string) => {
try {
const remove = await ark.deleteContact({ pubkey });
if (remove) {
setFollowed(false);
}
} catch (error) {
console.log(error);
}
};
const renderItem = (event: NDKEvent) => {
switch (event.kind) {
case NDKKind.Text:
return <TextNote key={event.id} event={event} className="mt-3" />;
case NDKKind.Repost:
return <RepostNote key={event.id} event={event} className="mt-3" />;
default:
return <TextNote key={event.id} event={event} className="mt-3" />;
}
};
useEffect(() => {
if (storage.account.contacts.includes(id)) {
setFollowed(true);
}
}, []);
return (
<WVList className="pb-5 overflow-y-auto">
<div className="h-11 bg-neutral-50 dark:bg-neutral-950 border-b flex items-center px-3 border-neutral-100 dark:border-neutral-900 mb-3">
<button
type="button"
className="inline-flex items-center gap-2.5 text-sm font-medium"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon className="size-4" />
Back
</button>
</div>
<div className="px-3">
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<img
src={user?.picture || user?.image}
alt={id}
className="h-12 w-12 shrink-0 rounded-lg object-cover"
loading="lazy"
decoding="async"
/>
<div className="inline-flex items-center gap-2">
{followed ? (
<button
type="button"
onClick={() => unfollow(id)}
className="inline-flex h-9 w-28 items-center justify-center rounded-lg bg-neutral-200 text-sm font-medium hover:bg-blue-500 hover:text-white dark:bg-neutral-800"
>
Unfollow
</button>
) : (
<button
type="button"
onClick={() => follow(id)}
className="inline-flex h-9 w-28 items-center justify-center rounded-lg bg-neutral-200 text-sm font-medium hover:bg-blue-500 hover:text-white dark:bg-neutral-800"
>
Follow
</button>
)}
<Link
to={`/chats/${id}`}
className="inline-flex h-9 w-28 items-center justify-center rounded-lg bg-neutral-200 text-sm font-medium hover:bg-blue-500 hover:text-white dark:bg-neutral-800"
>
Message
</Link>
</div>
</div>
<div className="flex flex-1 flex-col gap-1.5">
<div className="flex flex-col">
<h5 className="text-lg font-semibold">
{user?.name ||
user?.display_name ||
user?.displayName ||
"Anon"}
</h5>
{user?.nip05 ? (
<NIP05
pubkey={id}
nip05={user?.nip05}
className="max-w-[15rem] truncate text-sm text-neutral-600 dark:text-neutral-400"
/>
) : (
<span className="max-w-[15rem] truncate text-sm text-neutral-600 dark:text-neutral-400">
{displayNpub(id, 16)}
</span>
)}
</div>
<div className="max-w-[500px] select-text break-words text-neutral-900 dark:text-neutral-100">
{user?.about}
</div>
</div>
</div>
<div className="pt-2 mt-2 border-t border-neutral-100 dark:border-neutral-900">
<h3 className="text-lg font-semibold text-neutral-900 dark:text-neutral-100">
Latest posts
</h3>
<div className="flex h-full w-full flex-col justify-between gap-1.5 pb-10">
{isLoading ? (
<div className="flex items-center justify-center">
<LoaderIcon className="h-4 w-4 animate-spin" />
</div>
) : (
allEvents.map((item) => renderItem(item))
)}
<div className="flex h-16 items-center justify-center px-3 pb-3">
{hasNextPage ? (
<button
type="button"
onClick={() => fetchNextPage()}
disabled={!hasNextPage || isFetchingNextPage}
className="inline-flex h-10 w-max items-center justify-center gap-2 rounded-full bg-blue-500 px-6 font-medium text-white hover:bg-blue-600 focus:outline-none"
>
{isFetchingNextPage ? (
<LoaderIcon className="h-4 w-4 animate-spin" />
) : (
<>
<ArrowRightCircleIcon className="h-5 w-5" />
Load more
</>
)}
</button>
) : null}
</div>
</div>
</div>
</div>
</WVList>
);
}

View File

@ -41,7 +41,7 @@ export function ColumnLiveWidget({
className="inline-flex h-9 w-max items-center justify-center gap-1 rounded-full bg-blue-500 px-2.5 text-sm font-semibold text-white hover:bg-blue-600"
>
<ChevronUpIcon className="h-4 w-4" />
{events.length} {events.length === 1 ? "event" : "events"}
{events.length} {events.length === 1 ? "new event" : "new events"}
</button>
</div>
);

View File

@ -7,10 +7,10 @@ export function ChildReply({
}: { event: NDKEvent; rootEventId?: string }) {
return (
<Note.Provider event={event}>
<Note.Root>
<Note.Root className="pl-4 gap-2 mb-5">
<Note.User />
<Note.TextContent content={event.content} className="min-w-0" />
<div className="-ml-1 flex h-14 items-center gap-10">
<div className="-ml-1 flex items-center gap-10">
<Note.Reply rootEventId={rootEventId} />
<Note.Reaction />
<Note.Repost />

View File

@ -39,13 +39,10 @@ export const MentionNote = memo(function MentionNote({
}
return (
<Note.Provider event={data}>
<Note.Root className="my-2 flex w-full cursor-default flex-col gap-1 rounded-lg bg-neutral-100 dark:bg-neutral-900">
<div className="mt-3 px-3">
<Note.User
pubkey={data.pubkey}
time={data.created_at}
variant="mention"
/>
<Note.User variant="mention" />
</div>
<div className="mt-1 px-3 pb-3">
{renderKind(data)}
@ -57,5 +54,6 @@ export const MentionNote = memo(function MentionNote({
</Link>
</div>
</Note.Root>
</Note.Provider>
);
});

View File

@ -1,5 +1,7 @@
import { WIDGET_KIND } from "@lume/utils";
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
import { memo } from "react";
import { Link } from "react-router-dom";
import { useProfile } from "../../../hooks/useProfile";
import { useWidget } from "../../../hooks/useWidget";
@ -10,18 +12,35 @@ export const MentionUser = memo(function MentionUser({
const { addWidget } = useWidget();
return (
<DropdownMenu.Root>
<DropdownMenu.Trigger className="break-words text-blue-500 hover:text-blue-600">
{`@${user?.name || user?.displayName || user?.username || "anon"}`}
</DropdownMenu.Trigger>
<DropdownMenu.Content className="left-[50px] z-50 relative flex w-[200px] flex-col overflow-hidden rounded-xl border border-neutral-200 bg-neutral-950 focus:outline-none dark:border-neutral-900">
<DropdownMenu.Item asChild>
<Link
to={`/users/${pubkey}`}
className="inline-flex h-10 items-center px-4 text-sm text-white hover:bg-neutral-900 focus:outline-none"
>
View profile
</Link>
</DropdownMenu.Item>
<DropdownMenu.Item asChild>
<button
type="button"
onClick={() =>
addWidget.mutate({
kind: WIDGET_KIND.user,
title: user?.name || user?.display_name || user?.displayName,
title: user?.name || user?.displayName || "",
content: pubkey,
})
}
className="break-words text-blue-500 hover:text-blue-600"
className="inline-flex h-10 items-center px-4 text-sm text-white hover:bg-neutral-900 focus:outline-none"
>
{`@${user?.name || user?.displayName || user?.username || "unknown"}`}
Pin
</button>
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
);
});

View File

@ -63,7 +63,7 @@ export function NoteUser({
<Avatar.Image
src={user?.picture || user?.image}
alt={event.pubkey}
loading="lazy"
loading="eager"
decoding="async"
className="h-6 w-6 rounded-md"
/>
@ -94,12 +94,12 @@ export function NoteUser({
if (variant === "repost") {
if (isLoading) {
return (
<div className={twMerge("flex gap-3", className)}>
<div className="inline-flex w-10 items-center justify-center">
<div className={twMerge("flex gap-2 px-3", className)}>
<div className="inline-flex shrink-0 w-10 items-center justify-center">
<RepostIcon className="h-5 w-5 text-blue-500" />
</div>
<div className="inline-flex items-center gap-2">
<div className="h-6 w-6 animate-pulse rounded bg-neutral-300 dark:bg-neutral-700" />
<div className="h-6 w-6 shrink-0 animate-pulse rounded bg-neutral-300 dark:bg-neutral-700" />
<div className="h-4 w-24 animate-pulse rounded bg-neutral-300 dark:bg-neutral-700" />
</div>
</div>
@ -107,8 +107,8 @@ export function NoteUser({
}
return (
<div className={twMerge("flex gap-2", className)}>
<div className="inline-flex w-10 items-center justify-center">
<div className={twMerge("flex gap-2 px-3", className)}>
<div className="inline-flex shrink-0 w-10 items-center justify-center">
<RepostIcon className="h-5 w-5 text-blue-500" />
</div>
<div className="inline-flex items-center gap-2">
@ -116,7 +116,7 @@ export function NoteUser({
<Avatar.Image
src={user?.picture || user?.image}
alt={event.pubkey}
loading="lazy"
loading="eager"
decoding="async"
className="h-6 w-6 rounded object-cover"
/>
@ -161,7 +161,7 @@ export function NoteUser({
<Avatar.Image
src={user?.picture || user?.image}
alt={event.pubkey}
loading="lazy"
loading="eager"
decoding="async"
className="h-10 w-10 rounded-lg object-cover ring-1 ring-neutral-200/50 dark:ring-neutral-800/50"
/>
@ -212,7 +212,7 @@ export function NoteUser({
<Avatar.Image
src={user?.picture || user?.image}
alt={event.pubkey}
loading="lazy"
loading="eager"
decoding="async"
className="h-9 w-9 rounded-lg bg-white object-cover ring-1 ring-neutral-200/50 dark:ring-neutral-800/50"
/>

View File

@ -1,3 +1,4 @@
import { nanoid } from "nanoid";
import { nip19 } from "nostr-tools";
import { ReactNode } from "react";
import { Link } from "react-router-dom";
@ -172,7 +173,6 @@ export function useRichContent(content: string, textmode = false) {
/(https?:\/\/\S+)/g,
(match, i) => {
const url = new URL(match);
url.search = "";
if (!linkPreview && !textmode) {
linkPreview = match;
@ -194,7 +194,7 @@ export function useRichContent(content: string, textmode = false) {
);
parsedContent = reactStringReplace(parsedContent, "\n", () => {
return null;
return <div key={nanoid()} className="h-3" />;
});
if (typeof parsedContent[0] === "string") {

View File

@ -1,78 +0,0 @@
import { MentionOptions } from "@tiptap/extension-mention";
import { ReactRenderer } from "@tiptap/react";
import tippy from "tippy.js";
import { MentionList } from "../components/mentions";
import { useStorage } from "../provider";
export function useSuggestion() {
const storage = useStorage();
const suggestion: MentionOptions["suggestion"] = {
items: async ({ query }) => {
const users = await storage.getAllCacheUsers();
return users
.filter((item) => {
if (item.name)
return item.name.toLowerCase().startsWith(query.toLowerCase());
return item.displayName.toLowerCase().startsWith(query.toLowerCase());
})
.slice(0, 5);
},
render: () => {
let component;
let popup;
return {
onStart: (props) => {
component = new ReactRenderer(MentionList, {
props,
editor: props.editor,
});
if (!props.clientRect) {
return;
}
popup = tippy("body", {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
content: component.element,
showOnCreate: true,
interactive: true,
trigger: "manual",
placement: "bottom-start",
});
},
onUpdate(props) {
component.updateProps(props);
if (!props.clientRect) {
return;
}
popup[0].setProps({
getReferenceClientRect: props.clientRect,
});
},
onKeyDown(props) {
if (props.event.key === "Escape") {
popup[0].hide();
return true;
}
return component.ref?.onKeyDown(props);
},
onExit() {
popup[0].destroy();
component.destroy();
},
};
},
};
return { suggestion };
}

View File

@ -6,5 +6,4 @@ export * from "./hooks/useWidget";
export * from "./hooks/useRichContent";
export * from "./hooks/useEvent";
export * from "./hooks/useProfile";
export * from "./hooks/useSuggestion";
export * from "./hooks/useRelay";

View File

@ -10,3 +10,4 @@ export * from "./layouts/auth";
export * from "./layouts/composer";
export * from "./layouts/home";
export * from "./layouts/settings";
export * from "./mentions";

View File

@ -371,6 +371,9 @@ importers:
'@lume/icons':
specifier: workspace:^
version: link:../../icons
'@lume/ui':
specifier: workspace:^
version: link:../../ui
'@lume/utils':
specifier: workspace:^
version: link:../../utils
@ -386,6 +389,9 @@ importers:
react-router-dom:
specifier: ^6.21.0
version: 6.21.0(react-dom@18.2.0)(react@18.2.0)
sonner:
specifier: ^1.2.4
version: 1.2.4(react-dom@18.2.0)(react@18.2.0)
virtua:
specifier: ^0.18.0
version: 0.18.0(react-dom@18.2.0)(react@18.2.0)