aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/components/MastodonShareButton.tsx
blob: 71395b345d70ebf65ebd7d97ca5ca3759a0da83d (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// @jsxImportSource xeact
// @jsxRuntime automatic

import { u } from "xeact";

export interface MastodonShareButtonProps {
  title: string;
  url: string;
  series?: string;
  tags: string;
}

export default function MastodonShareButton(
  { title, url = u(), series, tags }: MastodonShareButtonProps,
) {
  let defaultURL = localStorage["mastodon_instance"];

  if (defaultURL == undefined) {
    defaultURL = "";
  }

  const tootTemplate = `${title}

${url}

${series ? "#" + series + " " : ""}${
    tags ? tags.map((x) => "#" + x).join(" ") : ""
  } @cadey@pony.social`;

  const instanceBox = (
    <input
      type="text"
      placeholder="https://pony.social"
      value={defaultURL}
    />
  );
  const tootBox = (
    <textarea rows={6} cols={40}>
      {tootTemplate}
    </textarea>
  );

  return (
    <div>
      <details>
        <summary>Share on Mastodon</summary>
        <span>{"Instance URL (https://mastodon.example)"}</span>
        <br />
        {instanceBox}
        <br />
        {tootBox}
        <br />
        <button
          onClick={(() => {
            let instanceURL = instanceBox.value;

            if (!instanceURL.startsWith("https://")) {
              instanceURL = `https://${instanceURL}`;
            }

            localStorage["mastodon_instance"] = instanceURL;
            const text = tootBox.value;
            const mastodonURL = u(instanceURL + "/share", {
              text,
              visibility: "public",
            });
            console.log({ text, mastodonURL });
            window.open(mastodonURL, "_blank");
          })()}
        >
          Share
        </button>
      </details>
    </div>
  );
}