diff options
| author | Wojciech Kwolek <wojciech@kwolek.io> | 2024-06-08 05:29:15 +0200 |
|---|---|---|
| committer | Wojciech Kwolek <wojciech@kwolek.io> | 2024-06-08 05:29:15 +0200 |
| commit | 9400a1b2eede87a2d953b0c754f9c2c96cfb34f8 (patch) | |
| tree | 206ac808fc86f0dded4a20e8af2e2afda7b225ec /src | |
initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.tsx | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/index.tsx b/src/index.tsx new file mode 100644 index 0000000..b300f20 --- /dev/null +++ b/src/index.tsx @@ -0,0 +1,62 @@ +import { ActionPanel, List, Action, closeMainWindow } from "@raycast/api"; +import { connect, Socket } from "net"; +import { showFailureToast } from "@raycast/utils"; +import { useState, useEffect, useRef } from "react"; + +export default function Command({ arguments: { host, port } }: { arguments: { host: string, port: string } }) { + const [elements, setElements] = useState<string[]>(); + const socket = useRef<Socket>(); + + useEffect(() => { + const showErr = (err: ErrorEvent) => { + showFailureToast(err.message, { title: "Couldn't connect to the script" }); + setElements([]); + }; + + const s = connect({ host, port: parseInt(port) }) + socket.current = s + s.on('error', showErr); + + let buf: string = ''; + + s.on('data', data => { + buf += data; + + const firstLineEnding = buf.indexOf('\n'); + if (firstLineEnding == -1) return; + + const amount = parseInt(buf.slice(0, firstLineEnding)); + + const lines = buf.slice(firstLineEnding + 1).trim().split("\n"); + if (lines.length != amount) return; + + setElements(lines.map(s => s.trim()).filter(s => s.length != 0)); + }) + + s.on('close', () => closeMainWindow()); + + return () => { + if (socket.current != null) socket.current.end(); + } + }, [host, port]) + + + return ( + <List isLoading={elements === undefined}> + {elements?.map((item, idx) => <List.Item title={item} key={idx} actions={ + <ActionPanel> + <Action title="Select" onAction={() => { + const s = socket.current; + if (s == null) { + showFailureToast('Socket disconnected') + closeMainWindow(); + return; + } + s.write(item + '\n') + s.end() + }} /> + </ActionPanel> + } />)} + </List> + ); +} |
