-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNewDomain.tsx
More file actions
203 lines (175 loc) · 5.92 KB
/
NewDomain.tsx
File metadata and controls
203 lines (175 loc) · 5.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { useState, useCallback, useId } from "react";
import { useQuery } from "@tanstack/react-query";
import { useFuzzySearchList, Highlight } from "@nozbe/microfuzz/react";
import { addDomain } from "./actions";
import { useAppDispatch } from "./store";
import { FormattedMessage } from "react-intl";
import { useTextParam } from "./utils";
interface ServerResponseJSON {
domain: string;
}
const isValidDomain = (str: string): boolean => {
try {
const url = new URL(`https://${str}`);
return url.hostname === str;
} catch {
return false;
}
};
export const NewDomain: React.FC<{ onDismiss: () => void }> = () => {
const dispatch = useAppDispatch();
const text = useTextParam();
const [value, setValue] = useState("");
const [focused, setFocused] = useState(false);
const [showResults, setShowResults] = useState(false);
const [submitting, setSubmitting] = useState(false);
const accessibilityId = useId();
const domain = value.trim();
const serversQuery = useQuery<ServerResponseJSON[]>({
queryKey: ["servers"],
queryFn: async () => {
const response = await fetch(
"https://api.joinmastodon.org/servers?registrations=all",
);
if (!response.ok) {
throw new Error("Unexpected response");
}
return response.json();
},
});
const results = useFuzzySearchList({
list: serversQuery.data ?? [],
queryText: domain ?? "",
key: "domain",
mapResultItem: ({ item, matches: [highlightRanges] }) => ({
item,
highlightRanges,
}),
});
const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
if (e.target.value.length > 0) {
setShowResults(true);
} else {
setShowResults(false);
}
}, []);
const handleFocus = useCallback(() => {
setFocused(true);
}, []);
const handleBlur = useCallback(() => {
setFocused(false);
}, []);
const handleClick = useCallback(
(e: React.MouseEvent<HTMLButtonElement>) => {
const domain = e.currentTarget.getAttribute("data-domain") ?? "";
setValue(domain);
setShowResults(false);
const redirectUrl = `https://${domain}/share?text=${encodeURIComponent(text)}`;
setSubmitting(true);
dispatch(addDomain(domain));
setTimeout(() => {
window.location.href = redirectUrl;
}, 100);
},
[dispatch, text],
);
const handleSubmit = useCallback(
(e: React.FormEvent) => {
e.preventDefault();
if (!domain || !isValidDomain(domain)) {
return;
}
const redirectUrl = `https://${domain}/share?text=${encodeURIComponent(text)}`;
setSubmitting(true);
dispatch(addDomain(domain));
setTimeout(() => {
window.location.href = redirectUrl;
}, 100);
},
[dispatch, text, domain],
);
return (
<>
<form onSubmit={handleSubmit} autoComplete="off">
<div className="relative">
<label htmlFor={accessibilityId} className="block font-bold mb-4">
<p>
<FormattedMessage id="" defaultMessage="Your Mastodon domain" />
</p>
<p className="text-sm font-normal text-slate-500 mt-2">
<FormattedMessage
defaultMessage="If your handle is <samp>@alice@mastodon.social</samp>, the domain is <samp>mastodon.social</samp>. You will be redirected to a page on your Mastodon server."
values={{
samp: (str) => (
<samp className="font-mono text-xs border border-slate-200 bg-white rounded-xs px-1 py-[0.17rem]">
{str}
</samp>
),
}}
/>
</p>
</label>
<div
className={`w-full flex items-center border bg-white rounded-md ${focused ? "border-blurple-500" : "border-slate-200"} ${showResults ? "rounded-b-none" : ""}`}
>
<input
className="text-black flex-grow p-3 border-0 focus:outline-0"
type="text"
value={value}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
id={accessibilityId}
/>
</div>
{showResults && results.length > 0 && (
<div
className={`absolute top-full mt-[-1px] w-full flex flex-col bg-white border border-t-0 rounded-b-md p-1 ${focused ? "border-blurple-500" : "border-slate-200"}`}
>
{results.slice(0, 5).map(({ item, highlightRanges }) => (
<button
type="button"
key={item.domain}
className="block rounded-md p-2 text-start cursor-pointer hover:bg-blurple-500 hover:text-white"
data-domain={item.domain}
onClick={handleClick}
>
<Highlight
text={item.domain}
ranges={highlightRanges}
style={{}}
className="font-bold"
/>
</button>
))}
</div>
)}
</div>
<button
className="mt-4 flex-none w-full bg-blurple-500 text-white text-base text-center font-bold px-4 py-3 rounded-md cursor-pointer hover:bg-blurple-600"
type="submit"
>
<FormattedMessage id="" defaultMessage="Continue to Mastodon" />
{submitting && "..."}
</button>
</form>
<p className="text-center text-sm mt-4">
<FormattedMessage
id=""
defaultMessage="Not on Mastodon yet? <a>Create an account</a>"
values={{
a: (f) => (
<a
href="https://joinmastodon.org/servers"
className="text-blurple-500 hover:underline"
>
{f}
</a>
),
}}
/>
</p>
</>
);
};