websocket show real connected

This commit is contained in:
mbecker20
2024-04-08 01:13:56 -07:00
parent fd90a62af1
commit 7508ae21b8
2 changed files with 25 additions and 10 deletions

View File

@@ -239,12 +239,15 @@ export const AddTags = ({ target }: { target: TargetExcludingSystem }) => {
}} }}
/> />
<CommandList> <CommandList>
<CommandEmpty <CommandEmpty className="m-1">
className="justify-between cursor-pointer hover:bg-accent m-1" <Button
onClick={create_tag} variant="ghost"
> onClick={create_tag}
Create Tag className="w-full flex items-center justify-between hover:bg-accent"
<PlusCircle className="w-4" /> >
Create Tag
<PlusCircle className="w-4" />
</Button>
</CommandEmpty> </CommandEmpty>
<CommandGroup> <CommandGroup>
{all_tags {all_tags

View File

@@ -12,6 +12,9 @@ import { AUTH_TOKEN_STORAGE_KEY } from "@main";
const rws_atom = atom<Rws | null>(null); const rws_atom = atom<Rws | null>(null);
const useWebsocket = () => useAtom(rws_atom); const useWebsocket = () => useAtom(rws_atom);
const ws_connected = atom(false);
export const useWebsocketConnected = () => useAtom(ws_connected);
const on_message = ( const on_message = (
{ data }: MessageEvent, { data }: MessageEvent,
invalidate: ReturnType<typeof useInvalidate> invalidate: ReturnType<typeof useInvalidate>
@@ -108,12 +111,19 @@ export const WebsocketProvider = ({
}) => { }) => {
const invalidate = useInvalidate(); const invalidate = useInvalidate();
const [ws, set] = useWebsocket(); const [ws, set] = useWebsocket();
const setConnected = useWebsocketConnected()[1];
const on_open_fn = useCallback(() => on_open(ws), [ws]); const on_open_fn = useCallback(() => {
on_open(ws);
setConnected(true);
}, [ws, setConnected]);
const on_message_fn = useCallback( const on_message_fn = useCallback(
(e: MessageEvent) => on_message(e, invalidate), (e: MessageEvent) => on_message(e, invalidate),
[invalidate] [invalidate]
); );
const on_close_fn = useCallback(() => {
setConnected(false);
}, [setConnected]);
useEffect(() => { useEffect(() => {
if (!ws) set(new Rws(url)); if (!ws) set(new Rws(url));
@@ -125,18 +135,20 @@ export const WebsocketProvider = ({
useEffect(() => { useEffect(() => {
ws?.addEventListener("open", on_open_fn); ws?.addEventListener("open", on_open_fn);
ws?.addEventListener("message", on_message_fn); ws?.addEventListener("message", on_message_fn);
ws?.addEventListener("close", on_close_fn);
return () => { return () => {
ws?.close(); ws?.close();
ws?.removeEventListener("open", on_open_fn); ws?.removeEventListener("open", on_open_fn);
ws?.removeEventListener("message", on_message_fn); ws?.removeEventListener("message", on_message_fn);
ws?.removeEventListener("close", on_close_fn);
}; };
}, [on_message_fn, on_open_fn, ws]); }, [on_message_fn, on_open_fn, on_close_fn, ws]);
return <>{children}</>; return <>{children}</>;
}; };
export const WsStatusIndicator = () => { export const WsStatusIndicator = () => {
const [ws] = useWebsocket(); const [connected] = useWebsocketConnected();
const onclick = () => const onclick = () =>
toast({ title: "surprise", description: "motherfucker" }); toast({ title: "surprise", description: "motherfucker" });
@@ -150,7 +162,7 @@ export const WsStatusIndicator = () => {
<Circle <Circle
className={cn( className={cn(
"w-4 h-4 stroke-none", "w-4 h-4 stroke-none",
ws ? "fill-green-500" : "fill-red-500" connected ? "fill-green-500" : "fill-red-500"
)} )}
/> />
</Button> </Button>