feat: column manager
This commit is contained in:
83
apps/desktop2/src/components/col.tsx
Normal file
83
apps/desktop2/src/components/col.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
import { useCallback, useEffect, useMemo, useRef } from "react";
|
||||
import {
|
||||
LogicalPosition,
|
||||
LogicalSize,
|
||||
getCurrent,
|
||||
} from "@tauri-apps/api/window";
|
||||
import { Webview } from "@tauri-apps/api/webview";
|
||||
import { LumeColumn } from "@lume/types";
|
||||
import { useDebouncedCallback } from "use-debounce";
|
||||
import { type UnlistenFn } from "@tauri-apps/api/event";
|
||||
|
||||
export function Col({
|
||||
column,
|
||||
account,
|
||||
isScroll,
|
||||
}: {
|
||||
column: LumeColumn;
|
||||
account: string;
|
||||
isScroll: boolean;
|
||||
}) {
|
||||
const mainWindow = useMemo(() => getCurrent(), []);
|
||||
const childWindow = useRef<Webview>(null);
|
||||
const container = useRef<HTMLDivElement>(null);
|
||||
const initialRect = useRef<DOMRect>(null);
|
||||
const unlisten = useRef<UnlistenFn>(null);
|
||||
const handleResize = useDebouncedCallback(() => {
|
||||
if (!childWindow.current) return;
|
||||
const newRect = container.current.getBoundingClientRect();
|
||||
if (initialRect.current.height !== newRect.height) {
|
||||
childWindow.current.setSize(
|
||||
new LogicalSize(newRect.width, newRect.height),
|
||||
);
|
||||
}
|
||||
}, 500);
|
||||
|
||||
const trackResize = useCallback(async () => {
|
||||
unlisten.current = await mainWindow.onResized(() => {
|
||||
handleResize();
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!childWindow.current) return;
|
||||
if (isScroll) {
|
||||
const newRect = container.current.getBoundingClientRect();
|
||||
childWindow.current.setPosition(
|
||||
new LogicalPosition(newRect.x, newRect.y),
|
||||
);
|
||||
}
|
||||
}, [isScroll]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mainWindow) return;
|
||||
if (!container.current) return;
|
||||
if (childWindow.current) return;
|
||||
|
||||
const rect = container.current.getBoundingClientRect();
|
||||
const name = `column-${column.name.toLowerCase().replace(/\W/g, "")}`;
|
||||
const url = column.name + `?account=${account}&name=${column.name}`;
|
||||
|
||||
// create new webview
|
||||
initialRect.current = rect;
|
||||
childWindow.current = new Webview(mainWindow, name, {
|
||||
url,
|
||||
x: rect.x,
|
||||
y: rect.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
transparent: true,
|
||||
userAgent: "Lume/4.0",
|
||||
});
|
||||
|
||||
// track window resize event
|
||||
trackResize();
|
||||
|
||||
return () => {
|
||||
if (unlisten.current) unlisten.current();
|
||||
if (childWindow.current) childWindow.current.close();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return <div ref={container} className="h-full w-[440px] shrink-0 p-2" />;
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
import { RepostNote } from "@/components/repost";
|
||||
import { Suggest } from "@/components/suggest";
|
||||
import { TextNote } from "@/components/text";
|
||||
import { useArk } from "@lume/ark";
|
||||
import { LoaderIcon, ArrowRightCircleIcon, InfoIcon } from "@lume/icons";
|
||||
import { Event, Kind } from "@lume/types";
|
||||
import { Column } from "@lume/ui";
|
||||
import { FETCH_LIMIT } from "@lume/utils";
|
||||
import { useInfiniteQuery } from "@tanstack/react-query";
|
||||
import { Link, useParams } from "@tanstack/react-router";
|
||||
import { Virtualizer } from "virtua";
|
||||
|
||||
export function Newsfeed() {
|
||||
const ark = useArk();
|
||||
// @ts-ignore, just work!!!
|
||||
const { account } = useParams({ strict: false });
|
||||
const {
|
||||
data,
|
||||
hasNextPage,
|
||||
isLoading,
|
||||
isRefetching,
|
||||
isFetchingNextPage,
|
||||
fetchNextPage,
|
||||
} = useInfiniteQuery({
|
||||
queryKey: ["local_newsfeed", account],
|
||||
initialPageParam: 0,
|
||||
queryFn: async ({ pageParam }: { pageParam: number }) => {
|
||||
const events = await ark.get_events(
|
||||
"local",
|
||||
FETCH_LIMIT,
|
||||
pageParam,
|
||||
true,
|
||||
);
|
||||
return events;
|
||||
},
|
||||
getNextPageParam: (lastPage) => {
|
||||
const lastEvent = lastPage?.at(-1);
|
||||
if (!lastEvent) return;
|
||||
return lastEvent.created_at - 1;
|
||||
},
|
||||
select: (data) => data?.pages.flatMap((page) => page),
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
const renderItem = (event: Event) => {
|
||||
if (!event) return;
|
||||
switch (event.kind) {
|
||||
case Kind.Repost:
|
||||
return <RepostNote key={event.id} event={event} />;
|
||||
default:
|
||||
return <TextNote key={event.id} event={event} />;
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Column.Root>
|
||||
<Column.Header title="Newsfeed" />
|
||||
<Column.Content>
|
||||
{isLoading || isRefetching ? (
|
||||
<div className="flex h-20 w-full flex-col items-center justify-center gap-1">
|
||||
<LoaderIcon className="size-5 animate-spin" />
|
||||
</div>
|
||||
) : !data.length ? (
|
||||
<div className="flex flex-col gap-3">
|
||||
<div className="flex items-center gap-2 rounded-xl bg-neutral-50 p-5 dark:bg-neutral-950">
|
||||
<InfoIcon className="size-6" />
|
||||
<p>
|
||||
Empty newsfeed. Or you view the{" "}
|
||||
<Link
|
||||
to="/$account/home"
|
||||
className="text-blue-500 hover:text-blue-600"
|
||||
>
|
||||
Global Newsfeed
|
||||
</Link>
|
||||
</p>
|
||||
</div>
|
||||
<Suggest />
|
||||
</div>
|
||||
) : (
|
||||
<Virtualizer overscan={3}>
|
||||
{data.map((item) => renderItem(item))}
|
||||
</Virtualizer>
|
||||
)}
|
||||
<div className="flex h-20 items-center justify-center">
|
||||
{hasNextPage ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => fetchNextPage()}
|
||||
disabled={!hasNextPage || isFetchingNextPage}
|
||||
className="inline-flex h-12 w-36 items-center justify-center gap-2 rounded-full bg-neutral-100 px-3 font-medium hover:bg-neutral-200 focus:outline-none dark:bg-neutral-900 dark:hover:bg-neutral-800"
|
||||
>
|
||||
{isFetchingNextPage ? (
|
||||
<LoaderIcon className="size-5 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<ArrowRightCircleIcon className="size-5" />
|
||||
Load more
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
</Column.Content>
|
||||
</Column.Root>
|
||||
);
|
||||
}
|
||||
39
apps/desktop2/src/components/toolbar.tsx
Normal file
39
apps/desktop2/src/components/toolbar.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import { ArrowLeftIcon, ArrowRightIcon } from "@lume/icons";
|
||||
import { useEffect, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
|
||||
export function Toolbar({
|
||||
moveLeft,
|
||||
moveRight,
|
||||
}: {
|
||||
moveLeft: () => void;
|
||||
moveRight: () => void;
|
||||
}) {
|
||||
const [domReady, setDomReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setDomReady(true);
|
||||
}, []);
|
||||
|
||||
return domReady
|
||||
? createPortal(
|
||||
<div className="flex items-center gap-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => moveLeft()}
|
||||
className="inline-flex size-8 items-center justify-center rounded-full text-neutral-800 hover:bg-neutral-200 dark:text-neutral-200 dark:hover:bg-neutral-800"
|
||||
>
|
||||
<ArrowLeftIcon className="size-5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => moveRight()}
|
||||
className="inline-flex size-8 items-center justify-center rounded-full text-neutral-800 hover:bg-neutral-200 dark:text-neutral-200 dark:hover:bg-neutral-800"
|
||||
>
|
||||
<ArrowRightIcon className="size-5" />
|
||||
</button>
|
||||
</div>,
|
||||
document.getElementById("toolbar"),
|
||||
)
|
||||
: null;
|
||||
}
|
||||
Reference in New Issue
Block a user