Skip to content

Commit

Permalink
feat: add trending notes column
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Jan 30, 2024
1 parent c389a23 commit 1fa1872
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 9 deletions.
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@columns/hashtag": "workspace:^",
"@columns/thread": "workspace:^",
"@columns/timeline": "workspace:^",
"@columns/trending-notes": "workspace:^",
"@columns/user": "workspace:^",
"@getalby/sdk": "^3.2.3",
"@lume/ark": "workspace:^",
Expand Down
Binary file removed apps/desktop/public/columns/topic.jpg
Binary file not shown.
Binary file added apps/desktop/public/columns/trending-notes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/desktop/src/routes/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Group } from "@columns/group";
import { Hashtag } from "@columns/hashtag";
import { Thread } from "@columns/thread";
import { Timeline } from "@columns/timeline";
import { TrendingNotes } from "@columns/trending-notes";
import { User } from "@columns/user";
import { useColumnContext } from "@lume/ark";
import {
Expand Down Expand Up @@ -45,6 +46,8 @@ export function HomeScreen() {
return <Group key={column.id} column={column} />;
case COL_TYPES.antenas:
return <Antenas key={column.id} column={column} />;
case COL_TYPES.trendingNotes:
return <TrendingNotes key={column.id} column={column} />;
default:
return <Default key={column.id} column={column} />;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/ark/src/ark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class Ark {
return sub;
}

public getNDKEvent(event: NostrEvent) {
return new NDKEvent(this.ndk, event);
}

public async createEvent({
kind,
tags,
Expand Down
2 changes: 1 addition & 1 deletion packages/ark/src/components/column/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function ColumnHeader({
className="inline-flex items-center gap-3 px-3 text-sm font-medium text-red-500 rounded-lg h-9 hover:bg-red-500 hover:text-red-50 focus:outline-none"
>
<TrashIcon className="size-4" />
{t("global.Delete")}
{t("global.delete")}
</button>
</DropdownMenu.Item>
</DropdownMenu.Content>
Expand Down
40 changes: 34 additions & 6 deletions packages/lume-column-default/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Column, useColumnContext } from "@lume/ark";
import { ColumnIcon } from "@lume/icons";
import { IColumn } from "@lume/types";
import { COL_TYPES } from "@lume/utils";

Expand All @@ -8,11 +7,7 @@ export function Default({ column }: { column: IColumn }) {

return (
<Column.Root>
<Column.Header
id={column.id}
title="Add columns"
icon={<ColumnIcon className="size-4" />}
/>
<Column.Header id={column.id} title="Add columns" />
<div className="h-full px-3 mt-3 flex flex-col gap-3 overflow-y-auto scrollbar-none">
<div className="h-11 flex items-center gap-5">
<button
Expand Down Expand Up @@ -87,6 +82,39 @@ export function Default({ column }: { column: IColumn }) {
</button>
</div>
</div>
<div className="flex flex-col rounded-xl overflow-hidden bg-neutral-50 dark:bg-neutral-950 ring-1 ring-neutral-100 dark:ring-neutral-900">
<div className="h-[100px] w-full px-3 pt-3">
<img
src="/columns/trending-notes.jpg"
srcSet="/columns/[email protected] 2x"
alt="trendingNotes"
loading="lazy"
decoding="async"
className="w-full h-auto object-cover rounded-lg"
/>
</div>
<div className="h-16 shrink-0 px-3 flex items-center justify-between">
<div>
<h1 className="font-semibold">Trending Notes</h1>
<p className="max-w-[18rem] truncate text-sm text-neutral-600 dark:text-neutral-500">
What is trending on Nostr?
</p>
</div>
<button
type="button"
onClick={() => {
addColumn({
kind: COL_TYPES.trendingNotes,
title: "",
content: "",
});
}}
className="shrink-0 w-16 h-8 rounded-lg text-sm font-semibold bg-neutral-100 dark:bg-neutral-900 text-blue-500 hover:bg-neutral-200 dark:hover:bg-neutral-800 inline-flex items-center justify-center"
>
Add
</button>
</div>
</div>
</div>
</Column.Root>
);
Expand Down
26 changes: 26 additions & 0 deletions packages/lume-column-trending-notes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@columns/trending-notes",
"version": "0.0.0",
"private": true,
"main": "./src/index.tsx",
"dependencies": {
"@lume/ark": "workspace:^",
"@lume/icons": "workspace:^",
"@lume/ui": "workspace:^",
"@lume/utils": "workspace:^",
"@nostr-dev-kit/ndk": "^2.3.3",
"@tanstack/react-query": "^5.17.19",
"react": "^18.2.0",
"react-router-dom": "^6.21.3",
"sonner": "^1.3.1",
"virtua": "^0.21.1"
},
"devDependencies": {
"@lume/tailwindcss": "workspace:^",
"@lume/tsconfig": "workspace:^",
"@lume/types": "workspace:^",
"@types/react": "^18.2.48",
"tailwind": "^4.0.0",
"typescript": "^5.3.3"
}
}
71 changes: 71 additions & 0 deletions packages/lume-column-trending-notes/src/home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { TextNote, useArk } from "@lume/ark";
import { LoaderIcon } from "@lume/icons";
import { type NDKEvent, type NostrEvent } from "@nostr-dev-kit/ndk";
import { useQuery } from "@tanstack/react-query";
import { fetch } from "@tauri-apps/plugin-http";
import { useEffect, useMemo, useRef } from "react";
import { CacheSnapshot, VList, VListHandle } from "virtua";

export function HomeRoute({ colKey }: { colKey: string }) {
const ark = useArk();
const ref = useRef<VListHandle>();
const cacheKey = `${colKey}-vlist`;

const [offset, cache] = useMemo(() => {
const serialized = sessionStorage.getItem(cacheKey);
if (!serialized) return [];
return JSON.parse(serialized) as [number, CacheSnapshot];
}, []);

const { data, isLoading } = useQuery({
queryKey: [colKey],
queryFn: async ({ signal }: { signal: AbortSignal }) => {
const res = await fetch("https://api.nostr.band/v0/trending/notes", {
signal,
});

if (!res) throw new Error("Failed to fetch trending notes");

const data = await res.json();
const events = data.notes.map((item: { event: NostrEvent }) =>
ark.getNDKEvent(item.event),
);

return events as NDKEvent[];
},
refetchOnMount: false,
refetchOnWindowFocus: false,
});

useEffect(() => {
if (!ref.current) return;
const handle = ref.current;

if (offset) {
handle.scrollTo(offset);
}

return () => {
sessionStorage.setItem(
cacheKey,
JSON.stringify([handle.scrollOffset, handle.cache]),
);
};
}, []);

return (
<div className="w-full h-full">
<VList ref={ref} cache={cache} overscan={2} className="flex-1 px-3">
{isLoading ? (
<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>
) : (
data.map((item) => (
<TextNote key={item.id} event={item} className="mt-3" />
))
)}
</VList>
</div>
);
}
23 changes: 23 additions & 0 deletions packages/lume-column-trending-notes/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Column } from "@lume/ark";
import { IColumn } from "@lume/types";
import { EventRoute, UserRoute } from "@lume/ui";
import { HomeRoute } from "./home";

export function TrendingNotes({ column }: { column: IColumn }) {
const colKey = `trending-notes-${column.id}`;

return (
<Column.Root>
<Column.Header
id={column.id}
queryKey={[colKey]}
title="Trending Notes"
/>
<Column.Content>
<Column.Route path="/" element={<HomeRoute colKey={colKey} />} />
<Column.Route path="/events/:id" element={<EventRoute />} />
<Column.Route path="/users/:id" element={<UserRoute />} />
</Column.Content>
</Column.Root>
);
}
8 changes: 8 additions & 0 deletions packages/lume-column-trending-notes/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sharedConfig from "@lume/tailwindcss";

const config = {
content: ["./src/**/*.{js,ts,jsx,tsx}"],
presets: [sharedConfig],
};

export default config;
8 changes: 8 additions & 0 deletions packages/lume-column-trending-notes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@lume/tsconfig/base.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
2 changes: 0 additions & 2 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export const COL_TYPES = {
hashtag: 3,
group: 4,
antenas: 5,
topic: 6,
trendingNotes: 9000,
trendingAccounts: 9001,
foryou: 9998,
newsfeed: 9999,
};
Expand Down
107 changes: 107 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fa1872

Please sign in to comment.