summaryrefslogtreecommitdiff
path: root/src/index.tsx
blob: b300f20a33dd7dedd8e469ca9f63b1defdfbff28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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>
  );
}