-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAppPanels.tsx
More file actions
319 lines (295 loc) · 10.4 KB
/
AppPanels.tsx
File metadata and controls
319 lines (295 loc) · 10.4 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { useLayoutEffect, useRef, useState } from 'preact/hooks';
import { Bookmarklet } from './Bookmarklet';
import { DominantField } from './DominantField';
export interface Strategy {
id: string;
name: string;
display_name: string;
}
export interface FeedFormData {
url: string;
strategy: string;
}
export interface FeedFieldErrors {
url: string;
form: string;
}
interface CreateFeedPanelProps {
focusComposerKey: number;
feedFormData: FeedFormData;
feedFieldErrors: FeedFieldErrors;
conversionError: string | null;
isConverting: boolean;
submitDisabled: boolean;
strategies: Strategy[];
strategiesLoading: boolean;
strategiesError: string | null;
feedCreationEnabled: boolean;
featuredFeeds: Array<{ path: string; title: string; description: string }>;
accessTokenRequired: boolean;
hasAccessToken: boolean;
tokenDraft: string;
tokenError: string;
showTokenPrompt: boolean;
onFeedSubmit: (event: Event) => void;
onFeedFieldChange: (key: 'url' | 'strategy', value: string) => void;
onTokenDraftChange: (value: string) => void;
onSaveToken: () => void;
onCancelTokenPrompt: () => void;
strategyHint: (strategy: Strategy) => string;
}
export function CreateFeedPanel({
focusComposerKey,
feedFormData,
feedFieldErrors,
conversionError,
isConverting,
submitDisabled,
strategies,
strategiesLoading,
strategiesError,
feedCreationEnabled,
featuredFeeds,
accessTokenRequired,
hasAccessToken,
tokenDraft,
tokenError,
showTokenPrompt,
onFeedSubmit,
onFeedFieldChange,
onTokenDraftChange,
onSaveToken,
onCancelTokenPrompt,
strategyHint,
}: CreateFeedPanelProps) {
const selectedStrategy = strategies.find((strategy) => strategy.id === feedFormData.strategy);
const urlInputRef = useRef<HTMLInputElement | null>(null);
const tokenInputRef = useRef<HTMLInputElement | null>(null);
const strategyOptionLabel = (strategy: Strategy) => {
if (strategy.id === 'faraday') return 'Default';
if (strategy.id === 'browserless') return 'JavaScript pages (recommended)';
return strategy.display_name;
};
useLayoutEffect(() => {
if (!urlInputRef.current || typeof window === 'undefined') return;
const focusHandle = window.requestAnimationFrame(() => {
const input = urlInputRef.current;
if (!input) return;
input.focus();
input.select();
});
return () => window.cancelAnimationFrame(focusHandle);
}, [focusComposerKey]);
useLayoutEffect(() => {
if (!showTokenPrompt || !tokenInputRef.current || typeof window === 'undefined') return;
const focusHandle = window.requestAnimationFrame(() => {
tokenInputRef.current?.focus();
});
return () => window.cancelAnimationFrame(focusHandle);
}, [showTokenPrompt]);
return (
<form class="form-shell form-shell--minimal" onSubmit={onFeedSubmit}>
<div class={`field-stack field-stack--dense${showTokenPrompt ? ' field-stack--inactive' : ''}`}>
<DominantField
id="url"
label="Page URL"
type="url"
value={feedFormData.url}
placeholder="https://example.com/article"
autoFocus
inputRef={urlInputRef}
actionLabel={isConverting ? 'Generating feed URL' : 'Generate feed URL'}
actionText={isConverting ? '...' : '>'}
disabled={submitDisabled}
error={feedFieldErrors.url}
onInput={(event) => onFeedFieldChange('url', (event.target as HTMLInputElement).value)}
/>
<label class="field-block field-block--centered field-block--compact" htmlFor="strategy">
<select
id="strategy"
name="strategy"
class="input input--minimal"
value={feedFormData.strategy}
disabled={strategiesLoading || showTokenPrompt}
onChange={(event) => onFeedFieldChange('strategy', (event.target as HTMLSelectElement).value)}
>
{strategiesLoading ? (
<option value="">Loading…</option>
) : (
strategies.map((strategy) => (
<option key={strategy.id} value={strategy.id}>
{strategyOptionLabel(strategy)}
</option>
))
)}
</select>
</label>
{strategiesError && <p class="field-help">{strategiesError}</p>}
{!strategiesError && selectedStrategy?.id === 'browserless' && (
<p class="field-help">{strategyHint(selectedStrategy)}</p>
)}
{!feedCreationEnabled && (
<>
<p class="field-help field-help--alert">Custom feed generation is disabled for this instance.</p>
{featuredFeeds.length > 0 && (
<div
class="ui-card ui-card--notice ui-card--padded notice"
role="status"
aria-label="Included feeds"
>
<p class="ui-eyebrow">Included feeds</p>
<div class="notice__title">Try a working included feed</div>
<p class="notice__intro">Start with a ready-made feed from this instance.</p>
<div class="featured-feed-list" role="list" aria-label="Featured feeds">
{featuredFeeds.map((feed) => (
<div key={feed.path} class="featured-feed-item" role="listitem">
<a href={feed.path} class="featured-feed-item__link" aria-label={feed.title}>
<span class="featured-feed-item__title">{feed.title}</span>
<span class="featured-feed-item__description">{feed.description}</span>
</a>
</div>
))}
</div>
<p class="notice__meta">
<a
href="https://html2rss.github.io/web-application/how-to/use-included-configs/"
target="_blank"
rel="noopener noreferrer"
>
Learn how included configs work.
</a>
</p>
</div>
)}
</>
)}
</div>
{showTokenPrompt && (
<div class="token-gate" role="group" aria-label="Access token">
<div class="token-gate__copy">
<h2>Add access token</h2>
<p class="token-gate__hint">This instance needs an access token.</p>
</div>
<label class="field-block field-block--stretch field-block--compact" htmlFor="access-token">
<span class="field-label field-label--ghost">Access token</span>
<input
id="access-token"
name="access-token"
type="password"
class="input input--mono input--minimal"
aria-label="Access token"
placeholder="Paste access token"
autocomplete="off"
ref={tokenInputRef}
value={tokenDraft}
onKeyDown={(event) => {
if (event.key !== 'Enter') return;
event.preventDefault();
void onSaveToken();
}}
onInput={(event) => onTokenDraftChange((event.target as HTMLInputElement).value)}
/>
<span class="field-error">{tokenError}</span>
</label>
<a
href="https://html2rss.github.io/web-application/getting-started/"
target="_blank"
rel="noopener noreferrer"
class="token-gate__nudge token-gate__nudge-link"
>
Set up your own instance with Docker.
</a>
<div class="token-gate__actions">
<button type="button" class="btn btn--ghost" onClick={onSaveToken}>
Save and continue
</button>
</div>
<div class="token-gate__back">
<button type="button" class="btn btn--quiet btn--linkish" onClick={onCancelTokenPrompt}>
Back
</button>
</div>
</div>
)}
{conversionError && (
<div class="ui-card ui-card--notice ui-card--padded notice" data-tone="error" role="alert">
<div class="notice__title">Feed generation failed</div>
<p>{conversionError}</p>
</div>
)}
{feedFieldErrors.form && (
<div class="ui-card ui-card--notice ui-card--padded notice" data-tone="error" role="alert">
<p>{feedFieldErrors.form}</p>
</div>
)}
</form>
);
}
interface UtilityStripProps {
hidden?: boolean;
hasAccessToken: boolean;
openapiUrl: string | null;
onClearToken: () => void;
}
export function UtilityStrip({
hidden = false,
hasAccessToken,
openapiUrl,
onClearToken,
}: UtilityStripProps) {
const [isOpen, setIsOpen] = useState(false);
const includedFeedsHref = (() => {
const directoryUrl = new URL('https://html2rss.github.io/feed-directory/');
if (typeof window === 'undefined') return directoryUrl.toString();
const instanceUrl = new URL('/', window.location.origin);
directoryUrl.hash = `!url=${encodeURIComponent(instanceUrl.toString())}`;
return directoryUrl.toString();
})();
if (hidden) return null;
return (
<section class="utility-strip" aria-label="Utilities">
<button
type="button"
class="utility-button utility-button--toggle"
aria-expanded={isOpen ? 'true' : 'false'}
onClick={() => setIsOpen((current) => !current)}
>
More
</button>
{isOpen && (
<div class="utility-strip__items">
<a href={includedFeedsHref} target="_blank" rel="noopener noreferrer" class="utility-link">
Try included feeds
</a>
<Bookmarklet />
{openapiUrl && (
<a href={openapiUrl} target="_blank" rel="noopener noreferrer" class="utility-link">
OpenAPI spec
</a>
)}
<a
href="https://github.com/html2rss/html2rss-web"
target="_blank"
rel="noopener noreferrer"
class="utility-link"
>
Source code
</a>
<a
href="https://hub.docker.com/r/html2rss/web"
target="_blank"
rel="noopener noreferrer"
class="utility-link"
>
Install from Docker Hub
</a>
{hasAccessToken && (
<button type="button" class="utility-button" onClick={onClearToken}>
Clear saved token
</button>
)}
</div>
)}
</section>
);
}