-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAppPanels.tsx
More file actions
317 lines (290 loc) · 10.4 KB
/
AppPanels.tsx
File metadata and controls
317 lines (290 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
import { useLayoutEffect, useRef } from 'preact/hooks';
import { Bookmarklet } from './Bookmarklet';
import { DominantField } from './DominantField';
import type { FeedCreationError } from '../api/contracts';
export interface FeedFormData {
url: string;
}
export interface FeedFieldErrors {
url: string;
form: string;
}
export type WorkflowState = 'create' | 'submitting' | 'token_prompt' | 'result' | 'error';
export type WorkflowErrorKind = FeedCreationError['kind'];
interface CreateFeedPanelProperties {
focusComposerKey: number;
workflowState: WorkflowState;
feedFormData: FeedFormData;
feedFieldErrors: FeedFieldErrors;
conversionError?: FeedCreationError;
errorKind?: WorkflowErrorKind;
isConverting: boolean;
submitDisabled: boolean;
feedCreationEnabled: boolean;
featuredFeeds: Array<{ path: string; title: string; description: string }>;
tokenDraft: string;
tokenError: string;
showTokenPrompt: boolean;
onFeedSubmit: (event: Event) => void;
onFeedFieldChange: (key: 'url', value: string) => void;
onTokenDraftChange: (value: string) => void;
onSaveToken: () => void;
onCancelTokenPrompt: () => void;
onRetryCreate: () => void;
}
export function CreateFeedPanel({
focusComposerKey,
workflowState,
feedFormData,
feedFieldErrors,
conversionError,
errorKind,
isConverting,
submitDisabled,
feedCreationEnabled,
featuredFeeds,
tokenDraft,
tokenError,
showTokenPrompt,
onFeedSubmit,
onFeedFieldChange,
onTokenDraftChange,
onSaveToken,
onCancelTokenPrompt,
onRetryCreate,
}: CreateFeedPanelProperties) {
const urlInputReference = useRef<HTMLInputElement>(undefined as never);
const tokenInputReference = useRef<HTMLInputElement>(undefined as never);
const failureMessage = conversionError?.message || feedFieldErrors.form;
const showRetryButton = Boolean(
conversionError && conversionError.nextAction === 'retry' && conversionError.retryAction !== 'none'
);
useLayoutEffect(() => {
if (!urlInputReference.current || globalThis.window === undefined) return;
const focusHandle = globalThis.requestAnimationFrame(() => {
const input = urlInputReference.current;
if (!input) return;
input.focus();
input.select();
});
return () => globalThis.cancelAnimationFrame(focusHandle);
}, [focusComposerKey]);
useLayoutEffect(() => {
if (!showTokenPrompt || !tokenInputReference.current || globalThis.window === undefined) return;
const focusHandle = globalThis.requestAnimationFrame(() => {
tokenInputReference.current?.focus();
});
return () => globalThis.cancelAnimationFrame(focusHandle);
}, [showTokenPrompt]);
return (
<form
class="form-shell form-shell--minimal"
onSubmit={onFeedSubmit}
data-state={workflowState}
data-error-kind={errorKind}
>
<div class={`field-stack field-stack--dense${showTokenPrompt ? ' field-stack--inactive' : ''}`}>
<DominantField
id="url"
label="Page URL"
type="text"
value={feedFormData.url}
placeholder="example.com/articles"
inputMode="url"
autoCapitalize="off"
spellcheck={false}
autoFocus
inputRef={urlInputReference}
actionLabel={isConverting ? 'Creating feed link' : 'Generate feed URL'}
actionText={isConverting ? '...' : '>'}
disabled={submitDisabled}
error={feedFieldErrors.url}
onInput={(event) => onFeedFieldChange('url', (event.target as HTMLInputElement).value)}
/>
{!feedCreationEnabled && (
<>
<p class="field-help field-help--alert">Feed creation is disabled on 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>Enter access token</h2>
<p class="token-gate__hint">
Use the `HTML2RSS_ACCESS_TOKEN` from your instance `.env` or setup.
</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"
autoCapitalize="off"
autoCorrect="off"
spellcheck={false}
data-1p-ignore="true"
data-lpignore="true"
ref={tokenInputReference}
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"
>
Copy `docker-compose.yml`, copy `.env`, start the stack, then paste the token.
</a>
<div class="token-gate__actions">
<button type="button" class="btn btn--primary" 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>
)}
{failureMessage && (
<div
class="ui-card ui-card--notice ui-card--padded notice"
data-tone="error"
data-error-kind={errorKind}
role="alert"
>
<div class="notice__title">Couldn't create feed yet</div>
<p>{failureMessage}</p>
{showRetryButton && (
<div class="notice__actions">
<button type="button" class="btn btn--primary" onClick={onRetryCreate}>
Try again
</button>
</div>
)}
</div>
)}
{isConverting && (
<div class="ui-card ui-card--notice ui-card--padded notice" data-state="loading" role="status">
<div class="notice__title">Creating feed link</div>
<p>Preparing preview.</p>
</div>
)}
</form>
);
}
interface UtilityStripProperties {
hasAccessToken: boolean;
openapiUrl?: string;
onClearToken: () => void;
}
export function UtilityStrip({ hasAccessToken, openapiUrl, onClearToken }: UtilityStripProperties) {
const normalizedOpenapiUrl = normalizeLocalOriginUrl(openapiUrl);
const includedFeedsHref = (() => {
const directoryUrl = new URL('https://html2rss.github.io/feed-directory/');
if (globalThis.window === undefined) return directoryUrl.toString();
const instanceUrl = new URL('/', globalThis.location.origin);
directoryUrl.hash = `!url=${encodeURIComponent(instanceUrl.toString())}`;
return directoryUrl.toString();
})();
return (
<section class="utility-strip" aria-label="Utilities">
<div class="utility-strip__items">
<a href={includedFeedsHref} target="_blank" rel="noopener noreferrer" class="utility-link">
Browse included feeds
</a>
<Bookmarklet />
{hasAccessToken && (
<button type="button" class="utility-button" onClick={onClearToken}>
Logout
</button>
)}
<a
href="https://hub.docker.com/r/html2rss/web"
target="_blank"
rel="noopener noreferrer"
class="utility-link"
>
Install from Docker Hub
</a>
{openapiUrl && (
<a
href={normalizedOpenapiUrl ?? 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>
</div>
</section>
);
}
function normalizeLocalOriginUrl(value?: string): string | undefined {
if (!value || globalThis.window === undefined) return value;
try {
const target = new URL(value, globalThis.location.origin);
const current = new URL(globalThis.location.origin);
const isLocalHost = (host: string) => host === 'localhost' || host === '127.0.0.1';
if (isLocalHost(current.hostname) && isLocalHost(target.hostname) && target.port !== current.port) {
return `${current.origin}${target.pathname}${target.search}${target.hash}`;
}
return target.toString();
} catch {
return value;
}
}