feat: improve popup visibility
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
import { commands } from '@/commands'
|
||||
import { ago, cn } from '@/commons'
|
||||
import { Spinner } from '@/components/spinner'
|
||||
import { User } from '@/components/user'
|
||||
import { commands } from "@/commands";
|
||||
import { ago, cn } from "@/commons";
|
||||
import { Spinner } from "@/components/spinner";
|
||||
import { User } from "@/components/user";
|
||||
import {
|
||||
ArrowRight,
|
||||
CaretDown,
|
||||
CirclesFour,
|
||||
Plus,
|
||||
X,
|
||||
} from '@phosphor-icons/react'
|
||||
import * as Dialog from '@radix-ui/react-dialog'
|
||||
import * as Progress from '@radix-ui/react-progress'
|
||||
import * as ScrollArea from '@radix-ui/react-scroll-area'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Link, Outlet, createLazyFileRoute } from '@tanstack/react-router'
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
import { Menu, MenuItem, PredefinedMenuItem } from '@tauri-apps/api/menu'
|
||||
import { readText, writeText } from '@tauri-apps/plugin-clipboard-manager'
|
||||
import { message } from '@tauri-apps/plugin-dialog'
|
||||
import { open } from '@tauri-apps/plugin-shell'
|
||||
import { type NostrEvent, nip19 } from 'nostr-tools'
|
||||
import { useCallback, useEffect, useRef, useState, useTransition } from 'react'
|
||||
import { Virtualizer } from 'virtua'
|
||||
} from "@phosphor-icons/react";
|
||||
import * as Dialog from "@radix-ui/react-dialog";
|
||||
import * as Progress from "@radix-ui/react-progress";
|
||||
import * as ScrollArea from "@radix-ui/react-scroll-area";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Link, Outlet, createLazyFileRoute } from "@tanstack/react-router";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { Menu, MenuItem, PredefinedMenuItem } from "@tauri-apps/api/menu";
|
||||
import { readText, writeText } from "@tauri-apps/plugin-clipboard-manager";
|
||||
import { message } from "@tauri-apps/plugin-dialog";
|
||||
import { open } from "@tauri-apps/plugin-shell";
|
||||
import { type NostrEvent, nip19 } from "nostr-tools";
|
||||
import { useCallback, useEffect, useRef, useState, useTransition } from "react";
|
||||
import { Virtualizer } from "virtua";
|
||||
|
||||
type EventPayload = {
|
||||
event: string
|
||||
sender: string
|
||||
}
|
||||
event: string;
|
||||
sender: string;
|
||||
};
|
||||
|
||||
export const Route = createLazyFileRoute('/$account/_layout/chats')({
|
||||
export const Route = createLazyFileRoute("/$account/_layout/chats")({
|
||||
component: Screen,
|
||||
})
|
||||
});
|
||||
|
||||
function Screen() {
|
||||
return (
|
||||
@@ -46,19 +46,19 @@ function Screen() {
|
||||
<Outlet />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function Header() {
|
||||
const { platform } = Route.useRouteContext()
|
||||
const { account } = Route.useParams()
|
||||
const { platform } = Route.useRouteContext();
|
||||
const { account } = Route.useParams();
|
||||
|
||||
return (
|
||||
<div
|
||||
data-tauri-drag-region
|
||||
className={cn(
|
||||
'z-[200] shrink-0 h-12 flex items-center justify-between',
|
||||
platform === 'macos' ? 'pl-[78px] pr-3.5' : 'px-3.5',
|
||||
"z-[200] shrink-0 h-12 flex items-center justify-between",
|
||||
platform === "macos" ? "pl-[78px] pr-3.5" : "px-3.5",
|
||||
)}
|
||||
>
|
||||
<CurrentUser />
|
||||
@@ -73,90 +73,90 @@ function Header() {
|
||||
<Compose />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function ChatList() {
|
||||
const { account } = Route.useParams()
|
||||
const { queryClient } = Route.useRouteContext()
|
||||
const { account } = Route.useParams();
|
||||
const { queryClient } = Route.useRouteContext();
|
||||
const { isLoading, data } = useQuery({
|
||||
queryKey: ['chats'],
|
||||
queryKey: ["chats"],
|
||||
queryFn: async () => {
|
||||
const res = await commands.getChats()
|
||||
const res = await commands.getChats();
|
||||
|
||||
if (res.status === 'ok') {
|
||||
const raw = res.data
|
||||
const events = raw.map((item) => JSON.parse(item) as NostrEvent)
|
||||
if (res.status === "ok") {
|
||||
const raw = res.data;
|
||||
const events = raw.map((item) => JSON.parse(item) as NostrEvent);
|
||||
|
||||
return events
|
||||
return events;
|
||||
} else {
|
||||
throw new Error(res.error)
|
||||
throw new Error(res.error);
|
||||
}
|
||||
},
|
||||
select: (data) => data.sort((a, b) => b.created_at - a.created_at),
|
||||
refetchOnMount: false,
|
||||
refetchOnWindowFocus: false,
|
||||
})
|
||||
});
|
||||
|
||||
const [isSync, setIsSync] = useState(false)
|
||||
const [progress, setProgress] = useState(0)
|
||||
const [isSync, setIsSync] = useState(false);
|
||||
const [progress, setProgress] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setInterval(
|
||||
() => setProgress((prev) => (prev <= 100 ? prev + 4 : 100)),
|
||||
1200,
|
||||
)
|
||||
return () => clearInterval(timer)
|
||||
}, [])
|
||||
);
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const unlisten = listen('synchronized', async () => {
|
||||
await queryClient.refetchQueries({ queryKey: ['chats'] })
|
||||
setIsSync(true)
|
||||
})
|
||||
const unlisten = listen("synchronized", async () => {
|
||||
await queryClient.refetchQueries({ queryKey: ["chats"] });
|
||||
setIsSync(true);
|
||||
});
|
||||
|
||||
return () => {
|
||||
unlisten.then((f) => f())
|
||||
}
|
||||
}, [])
|
||||
unlisten.then((f) => f());
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const unlisten = listen<EventPayload>('event', async (data) => {
|
||||
const event: NostrEvent = JSON.parse(data.payload.event)
|
||||
const chats: NostrEvent[] = await queryClient.getQueryData(['chats'])
|
||||
const unlisten = listen<EventPayload>("event", async (data) => {
|
||||
const event: NostrEvent = JSON.parse(data.payload.event);
|
||||
const chats: NostrEvent[] = await queryClient.getQueryData(["chats"]);
|
||||
|
||||
if (chats) {
|
||||
const index = chats.findIndex((item) => item.pubkey === event.pubkey)
|
||||
const index = chats.findIndex((item) => item.pubkey === event.pubkey);
|
||||
|
||||
if (index === -1) {
|
||||
await queryClient.setQueryData(
|
||||
['chats'],
|
||||
["chats"],
|
||||
(prevEvents: NostrEvent[]) => {
|
||||
if (!prevEvents) return prevEvents
|
||||
if (event.pubkey === account) return
|
||||
if (!prevEvents) return prevEvents;
|
||||
if (event.pubkey === account) return;
|
||||
|
||||
return [event, ...prevEvents]
|
||||
return [event, ...prevEvents];
|
||||
},
|
||||
)
|
||||
);
|
||||
} else {
|
||||
const newEvents = [...chats]
|
||||
const newEvents = [...chats];
|
||||
newEvents[index] = {
|
||||
...event,
|
||||
}
|
||||
};
|
||||
|
||||
await queryClient.setQueryData(['chats'], newEvents)
|
||||
await queryClient.setQueryData(["chats"], newEvents);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return () => {
|
||||
unlisten.then((f) => f())
|
||||
}
|
||||
}, [])
|
||||
unlisten.then((f) => f());
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ScrollArea.Root
|
||||
type={'scroll'}
|
||||
type={"scroll"}
|
||||
scrollHideDelay={300}
|
||||
className="relative overflow-hidden flex-1 w-full"
|
||||
>
|
||||
@@ -190,8 +190,8 @@ function ChatList() {
|
||||
<User.Provider pubkey={item.pubkey}>
|
||||
<User.Root
|
||||
className={cn(
|
||||
'flex items-center rounded-lg p-2 mb-1 gap-2 hover:bg-black/5 dark:hover:bg-white/5',
|
||||
isActive ? 'bg-black/5 dark:bg-white/5' : '',
|
||||
"flex items-center rounded-lg p-2 mb-1 gap-2 hover:bg-black/5 dark:hover:bg-white/5",
|
||||
isActive ? "bg-black/5 dark:bg-white/5" : "",
|
||||
)}
|
||||
>
|
||||
<User.Avatar className="size-8 rounded-full" />
|
||||
@@ -199,7 +199,7 @@ function ChatList() {
|
||||
<div className="inline-flex leading-tight">
|
||||
<User.Name className="max-w-[8rem] truncate font-semibold" />
|
||||
<span className="ml-1.5 text-neutral-500">
|
||||
{account === item.pubkey ? '(you)' : ''}
|
||||
{account === item.pubkey ? "(you)" : ""}
|
||||
</span>
|
||||
</div>
|
||||
{isTransitioning ? (
|
||||
@@ -226,17 +226,18 @@ function ChatList() {
|
||||
</ScrollArea.Scrollbar>
|
||||
<ScrollArea.Corner className="bg-transparent" />
|
||||
</ScrollArea.Root>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function SyncPopup({ progress }: { progress: number }) {
|
||||
return (
|
||||
<div className="absolute bottom-0 w-full p-4">
|
||||
<div className="relative flex flex-col items-center gap-1.5">
|
||||
<div className="absolute bottom-0 w-full h-36 flex flex-col justify-end">
|
||||
<div className="absolute left-0 bottom-0 w-full h-32 gradient-mask-t-10 bg-white dark:bg-black" />
|
||||
<div className="relative flex flex-col items-center gap-1.5 p-4">
|
||||
<Progress.Root
|
||||
className="relative overflow-hidden bg-black/20 dark:bg-white/20 rounded-full w-full h-1"
|
||||
style={{
|
||||
transform: 'translateZ(0)',
|
||||
transform: "translateZ(0)",
|
||||
}}
|
||||
value={progress}
|
||||
>
|
||||
@@ -248,93 +249,93 @@ function SyncPopup({ progress }: { progress: number }) {
|
||||
<span className="text-center text-xs">Syncing message...</span>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function Compose() {
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
const [target, setTarget] = useState('')
|
||||
const [newMessage, setNewMessage] = useState('')
|
||||
const [isPending, startTransition] = useTransition()
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [target, setTarget] = useState("");
|
||||
const [newMessage, setNewMessage] = useState("");
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
const { account } = Route.useParams()
|
||||
const { account } = Route.useParams();
|
||||
const { isLoading, data: contacts } = useQuery({
|
||||
queryKey: ['contacts', account],
|
||||
queryKey: ["contacts", account],
|
||||
queryFn: async () => {
|
||||
const res = await commands.getContactList()
|
||||
const res = await commands.getContactList();
|
||||
|
||||
if (res.status === 'ok') {
|
||||
return res.data
|
||||
if (res.status === "ok") {
|
||||
return res.data;
|
||||
} else {
|
||||
return []
|
||||
return [];
|
||||
}
|
||||
},
|
||||
refetchOnWindowFocus: false,
|
||||
enabled: isOpen,
|
||||
})
|
||||
});
|
||||
|
||||
const navigate = Route.useNavigate()
|
||||
const scrollRef = useRef<HTMLDivElement>(null)
|
||||
const navigate = Route.useNavigate();
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const pasteFromClipboard = async () => {
|
||||
const val = await readText()
|
||||
setTarget(val)
|
||||
}
|
||||
const val = await readText();
|
||||
setTarget(val);
|
||||
};
|
||||
|
||||
const sendMessage = () => {
|
||||
startTransition(async () => {
|
||||
if (!newMessage.length) return
|
||||
if (!target.length) return
|
||||
if (!target.startsWith('npub1')) {
|
||||
await message('You must enter the public key as npub', {
|
||||
title: 'Send Message',
|
||||
kind: 'error',
|
||||
})
|
||||
return
|
||||
if (!newMessage.length) return;
|
||||
if (!target.length) return;
|
||||
if (!target.startsWith("npub1")) {
|
||||
await message("You must enter the public key as npub", {
|
||||
title: "Send Message",
|
||||
kind: "error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const decoded = nip19.decode(target)
|
||||
let id: string
|
||||
const decoded = nip19.decode(target);
|
||||
let id: string;
|
||||
|
||||
if (decoded.type !== 'npub') {
|
||||
await message('You must enter the public key as npub', {
|
||||
title: 'Send Message',
|
||||
kind: 'error',
|
||||
})
|
||||
return
|
||||
if (decoded.type !== "npub") {
|
||||
await message("You must enter the public key as npub", {
|
||||
title: "Send Message",
|
||||
kind: "error",
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
id = decoded.data
|
||||
id = decoded.data;
|
||||
}
|
||||
|
||||
// Connect to user's inbox relays
|
||||
const connect = await commands.connectInboxRelays(target, false)
|
||||
const connect = await commands.connectInboxRelays(target, false);
|
||||
|
||||
// Send message
|
||||
if (connect.status === 'ok') {
|
||||
const res = await commands.sendMessage(id, newMessage)
|
||||
if (connect.status === "ok") {
|
||||
const res = await commands.sendMessage(id, newMessage);
|
||||
|
||||
if (res.status === 'ok') {
|
||||
setTarget('')
|
||||
setNewMessage('')
|
||||
setIsOpen(false)
|
||||
if (res.status === "ok") {
|
||||
setTarget("");
|
||||
setNewMessage("");
|
||||
setIsOpen(false);
|
||||
|
||||
navigate({
|
||||
to: '/$account/chats/$id',
|
||||
to: "/$account/chats/$id",
|
||||
params: { account, id },
|
||||
})
|
||||
});
|
||||
} else {
|
||||
await message(res.error, { title: 'Send Message', kind: 'error' })
|
||||
return
|
||||
await message(res.error, { title: "Send Message", kind: "error" });
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
await message(connect.error, {
|
||||
title: 'Connect Inbox Relays',
|
||||
kind: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
})
|
||||
title: "Connect Inbox Relays",
|
||||
kind: "error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog.Root open={isOpen} onOpenChange={setIsOpen}>
|
||||
@@ -401,7 +402,7 @@ function Compose() {
|
||||
</div>
|
||||
</div>
|
||||
<ScrollArea.Root
|
||||
type={'scroll'}
|
||||
type={"scroll"}
|
||||
scrollHideDelay={300}
|
||||
className="overflow-hidden flex-1 size-full"
|
||||
>
|
||||
@@ -448,45 +449,45 @@ function Compose() {
|
||||
</Dialog.Content>
|
||||
</Dialog.Portal>
|
||||
</Dialog.Root>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function CurrentUser() {
|
||||
const params = Route.useParams()
|
||||
const navigate = Route.useNavigate()
|
||||
const params = Route.useParams();
|
||||
const navigate = Route.useNavigate();
|
||||
|
||||
const showContextMenu = useCallback(async (e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
e.preventDefault();
|
||||
|
||||
const menuItems = await Promise.all([
|
||||
MenuItem.new({
|
||||
text: 'Copy Public Key',
|
||||
text: "Copy Public Key",
|
||||
action: async () => {
|
||||
const npub = nip19.npubEncode(params.account)
|
||||
await writeText(npub)
|
||||
const npub = nip19.npubEncode(params.account);
|
||||
await writeText(npub);
|
||||
},
|
||||
}),
|
||||
MenuItem.new({
|
||||
text: 'Settings',
|
||||
action: () => navigate({ to: '/' }),
|
||||
text: "Settings",
|
||||
action: () => navigate({ to: "/" }),
|
||||
}),
|
||||
MenuItem.new({
|
||||
text: 'Feedback',
|
||||
action: async () => await open('https://github.com/lumehq/coop/issues'),
|
||||
text: "Feedback",
|
||||
action: async () => await open("https://github.com/lumehq/coop/issues"),
|
||||
}),
|
||||
PredefinedMenuItem.new({ item: 'Separator' }),
|
||||
PredefinedMenuItem.new({ item: "Separator" }),
|
||||
MenuItem.new({
|
||||
text: 'Switch account',
|
||||
action: () => navigate({ to: '/' }),
|
||||
text: "Switch account",
|
||||
action: () => navigate({ to: "/" }),
|
||||
}),
|
||||
])
|
||||
]);
|
||||
|
||||
const menu = await Menu.new({
|
||||
items: menuItems,
|
||||
})
|
||||
});
|
||||
|
||||
await menu.popup().catch((e) => console.error(e))
|
||||
}, [])
|
||||
await menu.popup().catch((e) => console.error(e));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<button
|
||||
@@ -501,5 +502,5 @@ function CurrentUser() {
|
||||
</User.Provider>
|
||||
<CaretDown className="size-3 text-neutral-600 dark:text-neutral-400" />
|
||||
</button>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user