@wallarm-org/design-system 4.9.0 → 4.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/README.md +1 -1
  2. package/dist/components/Badge/generateBadgeVariants.js +9 -28
  3. package/dist/components/CodeSnippet/internal/FoldToggle.js +1 -1
  4. package/dist/components/SimpleCharts/Chart/classes.js +1 -1
  5. package/dist/components/SimpleCharts/HorizontalBarStack/classes.js +1 -1
  6. package/dist/components/SimpleCharts/Metric/classes.d.ts +2 -2
  7. package/dist/components/SimpleCharts/Metric/classes.js +2 -2
  8. package/dist/components/Table/TableBody/TableBodyCell.js +4 -1
  9. package/dist/guidelines/patterns/complex-drawer.md +1084 -0
  10. package/dist/guidelines/patterns/confirmation-dialogs.md +3 -11
  11. package/dist/guidelines/patterns/detail-drawer.md +31 -13
  12. package/dist/guidelines/patterns/empty-states.md +6 -5
  13. package/dist/guidelines/patterns/entity-table.md +25 -29
  14. package/dist/guidelines/patterns/feedback-toasts.md +3 -11
  15. package/dist/guidelines/patterns/resource-list-page.md +14 -16
  16. package/dist/guidelines/rules.md +6 -5
  17. package/dist/guidelines/templates/ai-assistant.md +1133 -0
  18. package/dist/guidelines/templates/api-inventory.md +905 -0
  19. package/dist/guidelines/templates/attack-events.md +2722 -0
  20. package/dist/guidelines/templates/incident-detail.md +2085 -0
  21. package/dist/guidelines/templates/integrations-settings.md +836 -0
  22. package/dist/guidelines/templates/page-states.md +798 -0
  23. package/dist/guidelines/templates/rule-editor.md +886 -0
  24. package/dist/guidelines/templates/security-dashboard.md +681 -0
  25. package/dist/metadata/components.json +10 -10
  26. package/dist/metadata/guidelines.json +1204 -37
  27. package/dist/theme/tokens.test.ts +52 -0
  28. package/package.json +1 -1
@@ -0,0 +1,886 @@
1
+ ---
2
+ title: Rule editor
3
+ description: Use when building a create or edit flow for a custom security rule, with scope, a condition written as code, an action and a review step.
4
+ components: [Page, PageHeader, PageToolbar, PageContent, Table, SearchInput, Drawer, Stepper, Field, Input, Select, Checkbox, CheckboxGroup, HttpMethod, CodeEditorRoot, CodeEditorContent, CodeSnippetRoot, InlineCodeSnippet, RadioGroup, Radio, Attribute, Badge, Alert, Button, Toaster]
5
+ keywords: [rule, custom rule, security rule, policy, wizard, stepper, multi-step, condition, code editor, yaml, scope, host, path, methods, block, monitor, review, create rule]
6
+ order: 50
7
+ ---
8
+
9
+ # Rule editor
10
+
11
+ A custom rules page whose "Create rule" button opens a create drawer: four steps take the user from where the rule applies to what it does, with a review before anything is saved.
12
+
13
+ ## What it shows
14
+
15
+ - A [Resource list page](../patterns/resource-list-page.md) behind the drawer, with `HttpMethod`, `InlineCodeSnippet` and `Badge` cells.
16
+ - A four-step `Stepper` drawer from [Create and edit forms](../patterns/create-edit-forms.md).
17
+ - Scope: `Input`, a `Select` for the host and a `CheckboxGroup` of methods.
18
+ - Condition: a `CodeEditor` with YAML highlighting, shown again as a read-only `CodeSnippet` on review.
19
+ - Action: `RadioGroup variant='card'` for block or monitor.
20
+ - Review: `Attribute` rows and a section as in the [Detail drawer](../patterns/detail-drawer.md).
21
+ - Errors after a submit attempt and a toast after create, from [Feedback toasts](../patterns/feedback-toasts.md).
22
+
23
+ ## Examples
24
+
25
+ The rules list. Press "Create rule" to open the drawer and walk Scope, Condition, Action and Review; press "Create rule" on Review with gaps to see the drawer jump back to the first invalid step.
26
+
27
+ The custom rules list. "Create rule" opens a four-step `Stepper` drawer: Scope (a host
28
+ `Select`, a path and a `CheckboxGroup` of methods), Condition (a YAML `CodeEditor`), Action
29
+ (`RadioGroup variant='card'`) and Review (`Attribute` rows and a read-only `CodeSnippet`).
30
+ Steps never block; "Create rule" on Review jumps back to the first invalid step, or closes the
31
+ drawer, adds the row and shows a toast.
32
+
33
+ ```tsx story=Default
34
+ import { type ChangeEvent, type FormEvent, useState } from 'react';
35
+ import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from '@wallarm-org/design-system/Alert';
36
+ import { Attribute, AttributeLabel, AttributeValue } from '@wallarm-org/design-system/Attribute';
37
+ import { Badge } from '@wallarm-org/design-system/Badge';
38
+ import { Button } from '@wallarm-org/design-system/Button';
39
+ import { Checkbox, CheckboxGroup, CheckboxIndicator, CheckboxLabel } from '@wallarm-org/design-system/Checkbox';
40
+ import { CodeEditorContent, CodeEditorRoot } from '@wallarm-org/design-system/CodeEditor';
41
+ import { CodeSnippetActions, CodeSnippetAdapterProvider, CodeSnippetCode, CodeSnippetContent, CodeSnippetCopyButton, CodeSnippetHeader, CodeSnippetLineNumbers, CodeSnippetRoot, CodeSnippetTitle, CodeSnippetWrapButton, InlineCodeSnippet, loadPrismAdapter } from '@wallarm-org/design-system/CodeSnippet';
42
+ import { Drawer, DrawerBody, DrawerClose, DrawerContent, DrawerFooter, DrawerFooterControls, DrawerHeader, DrawerTitle } from '@wallarm-org/design-system/Drawer';
43
+ import { Field, FieldDescription, FieldError, FieldIndicator, FieldLabel, FieldLegend } from '@wallarm-org/design-system/Field';
44
+ import { Heading } from '@wallarm-org/design-system/Heading';
45
+ import { HttpMethod } from '@wallarm-org/design-system/HttpMethod';
46
+ import { Plus } from '@wallarm-org/design-system/icons';
47
+ import { Input } from '@wallarm-org/design-system/Input';
48
+ import { Page, PageActions, PageContent, PageDescription, PageHeader, PageHeaderGroup, PageTitle, PageToolbar } from '@wallarm-org/design-system/Page';
49
+ import { Radio, RadioDescription, RadioGroup, RadioIndicator, RadioLabel } from '@wallarm-org/design-system/Radio';
50
+ import { SearchInput } from '@wallarm-org/design-system/SearchInput';
51
+ import { Select, SelectContent, SelectInput, SelectOption, SelectOptionIndicator, SelectOptionText, SelectPositioner } from '@wallarm-org/design-system/Select';
52
+ import { Separator } from '@wallarm-org/design-system/Separator';
53
+ import { HStack, VStack } from '@wallarm-org/design-system/Stack';
54
+ import { Stepper, StepperContent, StepperIndicator, StepperItem, type StepperItemStatus, StepperList, StepperNextTrigger, StepperPrevTrigger, StepperSeparator, StepperTitle, StepperTrigger } from '@wallarm-org/design-system/Stepper';
55
+ import { Table } from '@wallarm-org/design-system/Table';
56
+ import { useToast } from '@wallarm-org/design-system/Toast';
57
+
58
+ export const Default = ({ isLoading }) => {
59
+ const toast = useToast();
60
+ const [rules, setRules] = useState(RULES);
61
+ const [query, setQuery] = useState('');
62
+ const [open, setOpen] = useState(false);
63
+ const [step, setStep] = useState(0);
64
+ const [visited, setVisited] = useState<number[]>([]);
65
+ const [name, setName] = useState(EMPTY_DRAFT.name);
66
+ const [host, setHost] = useState(EMPTY_DRAFT.host);
67
+ const [path, setPath] = useState(EMPTY_DRAFT.path);
68
+ const [methods, setMethods] = useState(EMPTY_DRAFT.methods);
69
+ const [condition, setCondition] = useState(EMPTY_DRAFT.condition);
70
+ const [action, setAction] = useState<RuleAction>(EMPTY_DRAFT.action);
71
+ const [pending, setPending] = useState(false);
72
+
73
+ const errors = {
74
+ name: name.trim() === '' ? 'Enter a rule name.' : null,
75
+ host: host.length === 0 ? 'Select a host.' : null,
76
+ path: path.startsWith('/') ? null : 'Start the path with /.',
77
+ methods: methods.length === 0 ? 'Select at least one method.' : null,
78
+ condition: condition.trim() === '' ? 'Write a condition.' : null,
79
+ };
80
+ const valid = [
81
+ !errors.name && !errors.host && !errors.path && !errors.methods,
82
+ !errors.condition,
83
+ true,
84
+ true,
85
+ ];
86
+ const statuses = valid.map<StepperItemStatus>((ok, i) =>
87
+ !visited.includes(i) ? 'upcoming' : ok ? 'completed' : 'danger',
88
+ );
89
+ const showScopeErrors = visited.includes(0);
90
+ const showConditionErrors = visited.includes(1);
91
+ const last = step === RULE_STEPS.length - 1;
92
+
93
+ const search = query.trim().toLowerCase();
94
+ const rows = rules.filter(
95
+ rule => rule.name.toLowerCase().includes(search) || rule.path.includes(search),
96
+ );
97
+
98
+ const handleCreate = () => {
99
+ setName(EMPTY_DRAFT.name);
100
+ setHost(EMPTY_DRAFT.host);
101
+ setPath(EMPTY_DRAFT.path);
102
+ setMethods(EMPTY_DRAFT.methods);
103
+ setCondition(EMPTY_DRAFT.condition);
104
+ setAction(EMPTY_DRAFT.action);
105
+ setVisited([]);
106
+ setStep(0);
107
+ setOpen(true);
108
+ };
109
+ const handleStepChange = ({ step: next }: { step: number }) => {
110
+ setVisited(prev => (prev.includes(step) ? prev : [...prev, step]));
111
+ setStep(next);
112
+ };
113
+ const handleNameChange = (event: ChangeEvent<HTMLInputElement>) => setName(event.target.value);
114
+ const handleHostChange = ({ value }: { value: string[] }) => setHost(value);
115
+ const handlePathChange = (event: ChangeEvent<HTMLInputElement>) => setPath(event.target.value);
116
+ const handleActionChange = ({ value }: { value: string | null }) =>
117
+ setAction(value === 'monitor' ? 'monitor' : 'block');
118
+
119
+ const handleSubmit = async (event: FormEvent) => {
120
+ event.preventDefault();
121
+ if (!valid.every(Boolean)) {
122
+ setVisited([0, 1, 2]);
123
+ setStep(valid.indexOf(false));
124
+ return;
125
+ }
126
+ setPending(true);
127
+ await saveRequest();
128
+ setPending(false);
129
+ setRules(prev => [
130
+ {
131
+ id: `r-${2042 + prev.length}`,
132
+ name,
133
+ host: host[0] ?? '',
134
+ path,
135
+ methods,
136
+ action,
137
+ updatedAt: new Date().toISOString(),
138
+ },
139
+ ...prev,
140
+ ]);
141
+ setOpen(false);
142
+ toast.create({ id: 'rule-created', title: 'Rule created', type: 'default' });
143
+ };
144
+
145
+ return (
146
+ <Page name='custom-rules' fullSize fixedHeight data-testid='rules-page'>
147
+ <PageHeader>
148
+ <PageHeaderGroup className='flex-1'>
149
+ <PageTitle>Custom rules</PageTitle>
150
+ <PageDescription>
151
+ Rules that block or monitor requests matching your own conditions, on top of the
152
+ built-in attack detection.
153
+ </PageDescription>
154
+ </PageHeaderGroup>
155
+ <PageActions>
156
+ <Button size='medium' onClick={handleCreate} data-testid='rules-create'>
157
+ <Plus />
158
+ Create rule
159
+ </Button>
160
+ </PageActions>
161
+ </PageHeader>
162
+
163
+ <PageToolbar>
164
+ <SearchInput
165
+ value={query}
166
+ onChange={setQuery}
167
+ placeholder='Search name or path'
168
+ className='w-[320px]'
169
+ data-testid='rules-search'
170
+ />
171
+ </PageToolbar>
172
+
173
+ <PageContent>
174
+ <Table
175
+ aria-label='Custom rules'
176
+ data={isLoading ? [] : rows}
177
+ columns={RULE_COLUMNS}
178
+ isLoading={isLoading}
179
+ getRowId={getRuleId}
180
+ data-testid='rules-table'
181
+ />
182
+ </PageContent>
183
+
184
+ <Drawer open={open} onOpenChange={setOpen} data-testid='rule-drawer'>
185
+ <DrawerContent>
186
+ <DrawerHeader>
187
+ <DrawerTitle>Create rule</DrawerTitle>
188
+ </DrawerHeader>
189
+ {/* noValidate: the page validates itself and shows its own errors. */}
190
+ <form noValidate onSubmit={handleSubmit} className='flex min-h-0 flex-1 flex-col'>
191
+ <Stepper
192
+ count={RULE_STEPS.length}
193
+ step={step}
194
+ onStepChange={handleStepChange}
195
+ className='flex min-h-0 flex-1 flex-col'
196
+ data-testid='rule-stepper'
197
+ >
198
+ <StepperList aria-label='Create rule steps'>
199
+ {RULE_STEPS.map((title, i) => (
200
+ <StepperItem key={title} index={i} status={statuses[i]}>
201
+ <StepperTrigger>
202
+ <StepperIndicator />
203
+ <StepperTitle>{title}</StepperTitle>
204
+ </StepperTrigger>
205
+ <StepperSeparator />
206
+ </StepperItem>
207
+ ))}
208
+ </StepperList>
209
+ <CodeSnippetAdapterProvider adapter={loadPrismAdapter}>
210
+ <DrawerBody>
211
+ <StepperContent index={0} className='py-16'>
212
+ <VStack gap={12}>
213
+ <Field required>
214
+ <FieldLabel>
215
+ Name
216
+ <FieldIndicator />
217
+ </FieldLabel>
218
+ <Input
219
+ value={name}
220
+ error={showScopeErrors && Boolean(errors.name)}
221
+ onChange={handleNameChange}
222
+ placeholder='Login credential stuffing'
223
+ data-testid='rule-name'
224
+ />
225
+ {showScopeErrors && errors.name && <FieldError>{errors.name}</FieldError>}
226
+ </Field>
227
+ <div className='grid grid-cols-2 gap-12'>
228
+ <Field required>
229
+ <FieldLabel>
230
+ Host
231
+ <FieldIndicator />
232
+ </FieldLabel>
233
+ <Select
234
+ collection={hostCollection}
235
+ value={host}
236
+ onValueChange={handleHostChange}
237
+ invalid={showScopeErrors && Boolean(errors.host)}
238
+ data-testid='rule-host'
239
+ >
240
+ <SelectInput placeholder='Select a host' />
241
+ <SelectPositioner>
242
+ <SelectContent>
243
+ {hostCollection.items.map(item => (
244
+ <SelectOption key={item.value} item={item}>
245
+ <SelectOptionText>{item.label}</SelectOptionText>
246
+ <SelectOptionIndicator />
247
+ </SelectOption>
248
+ ))}
249
+ </SelectContent>
250
+ </SelectPositioner>
251
+ </Select>
252
+ {showScopeErrors && errors.host && <FieldError>{errors.host}</FieldError>}
253
+ </Field>
254
+ <Field required>
255
+ <FieldLabel>
256
+ Path
257
+ <FieldIndicator />
258
+ </FieldLabel>
259
+ <Input
260
+ value={path}
261
+ error={showScopeErrors && Boolean(errors.path)}
262
+ onChange={handlePathChange}
263
+ placeholder='/v2/login'
264
+ data-testid='rule-path'
265
+ />
266
+ {showScopeErrors && errors.path && <FieldError>{errors.path}</FieldError>}
267
+ </Field>
268
+ </div>
269
+ {/* A direct CheckboxGroup child makes Field a fieldset, so each checkbox keeps its
270
+ own id. Inside a plain field every checkbox label points at the field id,
271
+ and clicking any method toggles the first. */}
272
+ <Field data-testid='rule-methods'>
273
+ <FieldLegend variant='label'>Methods</FieldLegend>
274
+ <FieldDescription>
275
+ Requests with other methods skip this rule.
276
+ </FieldDescription>
277
+ <CheckboxGroup name='methods' value={methods} onValueChange={setMethods}>
278
+ {HTTP_METHODS.map(method => (
279
+ <Checkbox key={method} value={method}>
280
+ <CheckboxIndicator />
281
+ <CheckboxLabel>
282
+ <HttpMethod method={method} />
283
+ </CheckboxLabel>
284
+ </Checkbox>
285
+ ))}
286
+ </CheckboxGroup>
287
+ {showScopeErrors && errors.methods && (
288
+ <FieldError>{errors.methods}</FieldError>
289
+ )}
290
+ </Field>
291
+ </VStack>
292
+ </StepperContent>
293
+
294
+ <StepperContent index={1} className='py-16'>
295
+ <Field required>
296
+ <FieldLabel>
297
+ Condition
298
+ <FieldIndicator />
299
+ </FieldLabel>
300
+ <FieldDescription>
301
+ YAML. A request matches when every item under all is true.
302
+ </FieldDescription>
303
+ <CodeEditorRoot
304
+ value={condition}
305
+ onChange={setCondition}
306
+ language='yaml'
307
+ data-testid='rule-condition'
308
+ >
309
+ <CodeSnippetHeader>
310
+ <CodeSnippetTitle>condition.yaml</CodeSnippetTitle>
311
+ <CodeSnippetActions>
312
+ <CodeSnippetCopyButton />
313
+ <CodeSnippetWrapButton />
314
+ </CodeSnippetActions>
315
+ </CodeSnippetHeader>
316
+ <CodeEditorContent lineNumbers aria-label='Rule condition' />
317
+ </CodeEditorRoot>
318
+ {showConditionErrors && errors.condition && (
319
+ <FieldError>{errors.condition}</FieldError>
320
+ )}
321
+ </Field>
322
+ </StepperContent>
323
+
324
+ <StepperContent index={2} className='py-16'>
325
+ <Field required>
326
+ <FieldLabel>
327
+ Action
328
+ <FieldIndicator />
329
+ </FieldLabel>
330
+ <FieldDescription>What happens to a request that matches.</FieldDescription>
331
+ <RadioGroup
332
+ variant='card'
333
+ name='action'
334
+ value={action}
335
+ onValueChange={handleActionChange}
336
+ >
337
+ <Radio value='block'>
338
+ <RadioIndicator />
339
+ <RadioLabel>Block</RadioLabel>
340
+ <RadioDescription>
341
+ Return 403 and record the request as an attack.
342
+ </RadioDescription>
343
+ </Radio>
344
+ <Radio value='monitor'>
345
+ <RadioIndicator />
346
+ <RadioLabel>Monitor</RadioLabel>
347
+ <RadioDescription>
348
+ Let the request through and record it as an attack. Use it to tune a new
349
+ rule first.
350
+ </RadioDescription>
351
+ </Radio>
352
+ </RadioGroup>
353
+ </Field>
354
+ </StepperContent>
355
+
356
+ <StepperContent index={3} className='py-16'>
357
+ <VStack gap={16}>
358
+ <Alert color='info'>
359
+ <AlertIcon />
360
+ <AlertContent>
361
+ <AlertTitle>Applies within a minute</AlertTitle>
362
+ <AlertDescription>
363
+ Every node picks up the rule on its next sync. Requests already in
364
+ flight are not rechecked.
365
+ </AlertDescription>
366
+ </AlertContent>
367
+ </Alert>
368
+ <div>
369
+ <VStack gap={6} className='pb-8' data-testid='rule-review'>
370
+ <Attribute orientation='horizontal'>
371
+ <AttributeLabel width={140}>Name</AttributeLabel>
372
+ <AttributeValue>{name}</AttributeValue>
373
+ </Attribute>
374
+ <Attribute orientation='horizontal'>
375
+ <AttributeLabel width={140}>Host</AttributeLabel>
376
+ <AttributeValue>{host[0]}</AttributeValue>
377
+ </Attribute>
378
+ <Attribute orientation='horizontal'>
379
+ <AttributeLabel width={140}>Path</AttributeLabel>
380
+ <AttributeValue>
381
+ <InlineCodeSnippet code={path} size='md' />
382
+ </AttributeValue>
383
+ </Attribute>
384
+ <Attribute orientation='horizontal'>
385
+ <AttributeLabel width={140}>Methods</AttributeLabel>
386
+ <AttributeValue>
387
+ <HStack gap={4}>
388
+ {methods.map(method => (
389
+ <HttpMethod key={method} method={method} />
390
+ ))}
391
+ </HStack>
392
+ </AttributeValue>
393
+ </Attribute>
394
+ <Attribute orientation='horizontal'>
395
+ <AttributeLabel width={140}>Action</AttributeLabel>
396
+ <AttributeValue>
397
+ <Badge variant='dotted' type='secondary' color={ACTION[action].color}>
398
+ {ACTION[action].label}
399
+ </Badge>
400
+ </AttributeValue>
401
+ </Attribute>
402
+ </VStack>
403
+ <Separator spacing={4} />
404
+ <HStack justify='between' align='center' className='py-8'>
405
+ <Heading as='h3' size='md' weight='medium'>
406
+ Condition
407
+ </Heading>
408
+ </HStack>
409
+ <CodeSnippetRoot code={condition} language='yaml'>
410
+ <CodeSnippetContent>
411
+ <CodeSnippetLineNumbers />
412
+ <CodeSnippetCode />
413
+ </CodeSnippetContent>
414
+ </CodeSnippetRoot>
415
+ </div>
416
+ </VStack>
417
+ </StepperContent>
418
+ </DrawerBody>
419
+ </CodeSnippetAdapterProvider>
420
+ <DrawerFooter>
421
+ <DrawerFooterControls placement='left'>
422
+ <DrawerClose asChild>
423
+ <Button variant='ghost' color='neutral'>
424
+ Cancel
425
+ </Button>
426
+ </DrawerClose>
427
+ </DrawerFooterControls>
428
+ <DrawerFooterControls placement='right'>
429
+ <StepperPrevTrigger asChild>
430
+ <Button variant='outline' color='neutral' data-testid='rule-back'>
431
+ Back
432
+ </Button>
433
+ </StepperPrevTrigger>
434
+ {last ? (
435
+ <Button type='submit' loading={pending} data-testid='rule-submit'>
436
+ Create rule
437
+ </Button>
438
+ ) : (
439
+ <StepperNextTrigger asChild>
440
+ <Button data-testid='rule-next'>Next</Button>
441
+ </StepperNextTrigger>
442
+ )}
443
+ </DrawerFooterControls>
444
+ </DrawerFooter>
445
+ </Stepper>
446
+ </form>
447
+ </DrawerContent>
448
+ </Drawer>
449
+ </Page>
450
+ );
451
+ };
452
+ ```
453
+
454
+ The rules request still in flight: skeleton rows, with "Create rule" still available.
455
+
456
+ The rules request is still in flight: the table shows skeleton rows while "Create rule" stays
457
+ available.
458
+
459
+ ```tsx story=Loading
460
+ import { type ChangeEvent, type FormEvent, useState } from 'react';
461
+ import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from '@wallarm-org/design-system/Alert';
462
+ import { Attribute, AttributeLabel, AttributeValue } from '@wallarm-org/design-system/Attribute';
463
+ import { Badge } from '@wallarm-org/design-system/Badge';
464
+ import { Button } from '@wallarm-org/design-system/Button';
465
+ import { Checkbox, CheckboxGroup, CheckboxIndicator, CheckboxLabel } from '@wallarm-org/design-system/Checkbox';
466
+ import { CodeEditorContent, CodeEditorRoot } from '@wallarm-org/design-system/CodeEditor';
467
+ import { CodeSnippetActions, CodeSnippetAdapterProvider, CodeSnippetCode, CodeSnippetContent, CodeSnippetCopyButton, CodeSnippetHeader, CodeSnippetLineNumbers, CodeSnippetRoot, CodeSnippetTitle, CodeSnippetWrapButton, InlineCodeSnippet, loadPrismAdapter } from '@wallarm-org/design-system/CodeSnippet';
468
+ import { Drawer, DrawerBody, DrawerClose, DrawerContent, DrawerFooter, DrawerFooterControls, DrawerHeader, DrawerTitle } from '@wallarm-org/design-system/Drawer';
469
+ import { Field, FieldDescription, FieldError, FieldIndicator, FieldLabel, FieldLegend } from '@wallarm-org/design-system/Field';
470
+ import { Heading } from '@wallarm-org/design-system/Heading';
471
+ import { HttpMethod } from '@wallarm-org/design-system/HttpMethod';
472
+ import { Plus } from '@wallarm-org/design-system/icons';
473
+ import { Input } from '@wallarm-org/design-system/Input';
474
+ import { Page, PageActions, PageContent, PageDescription, PageHeader, PageHeaderGroup, PageTitle, PageToolbar } from '@wallarm-org/design-system/Page';
475
+ import { Radio, RadioDescription, RadioGroup, RadioIndicator, RadioLabel } from '@wallarm-org/design-system/Radio';
476
+ import { SearchInput } from '@wallarm-org/design-system/SearchInput';
477
+ import { Select, SelectContent, SelectInput, SelectOption, SelectOptionIndicator, SelectOptionText, SelectPositioner } from '@wallarm-org/design-system/Select';
478
+ import { Separator } from '@wallarm-org/design-system/Separator';
479
+ import { HStack, VStack } from '@wallarm-org/design-system/Stack';
480
+ import { Stepper, StepperContent, StepperIndicator, StepperItem, type StepperItemStatus, StepperList, StepperNextTrigger, StepperPrevTrigger, StepperSeparator, StepperTitle, StepperTrigger } from '@wallarm-org/design-system/Stepper';
481
+ import { Table } from '@wallarm-org/design-system/Table';
482
+ import { useToast } from '@wallarm-org/design-system/Toast';
483
+
484
+ export const Loading = ({ isLoading }) => {
485
+ const toast = useToast();
486
+ const [rules, setRules] = useState(RULES);
487
+ const [query, setQuery] = useState('');
488
+ const [open, setOpen] = useState(false);
489
+ const [step, setStep] = useState(0);
490
+ const [visited, setVisited] = useState<number[]>([]);
491
+ const [name, setName] = useState(EMPTY_DRAFT.name);
492
+ const [host, setHost] = useState(EMPTY_DRAFT.host);
493
+ const [path, setPath] = useState(EMPTY_DRAFT.path);
494
+ const [methods, setMethods] = useState(EMPTY_DRAFT.methods);
495
+ const [condition, setCondition] = useState(EMPTY_DRAFT.condition);
496
+ const [action, setAction] = useState<RuleAction>(EMPTY_DRAFT.action);
497
+ const [pending, setPending] = useState(false);
498
+
499
+ const errors = {
500
+ name: name.trim() === '' ? 'Enter a rule name.' : null,
501
+ host: host.length === 0 ? 'Select a host.' : null,
502
+ path: path.startsWith('/') ? null : 'Start the path with /.',
503
+ methods: methods.length === 0 ? 'Select at least one method.' : null,
504
+ condition: condition.trim() === '' ? 'Write a condition.' : null,
505
+ };
506
+ const valid = [
507
+ !errors.name && !errors.host && !errors.path && !errors.methods,
508
+ !errors.condition,
509
+ true,
510
+ true,
511
+ ];
512
+ const statuses = valid.map<StepperItemStatus>((ok, i) =>
513
+ !visited.includes(i) ? 'upcoming' : ok ? 'completed' : 'danger',
514
+ );
515
+ const showScopeErrors = visited.includes(0);
516
+ const showConditionErrors = visited.includes(1);
517
+ const last = step === RULE_STEPS.length - 1;
518
+
519
+ const search = query.trim().toLowerCase();
520
+ const rows = rules.filter(
521
+ rule => rule.name.toLowerCase().includes(search) || rule.path.includes(search),
522
+ );
523
+
524
+ const handleCreate = () => {
525
+ setName(EMPTY_DRAFT.name);
526
+ setHost(EMPTY_DRAFT.host);
527
+ setPath(EMPTY_DRAFT.path);
528
+ setMethods(EMPTY_DRAFT.methods);
529
+ setCondition(EMPTY_DRAFT.condition);
530
+ setAction(EMPTY_DRAFT.action);
531
+ setVisited([]);
532
+ setStep(0);
533
+ setOpen(true);
534
+ };
535
+ const handleStepChange = ({ step: next }: { step: number }) => {
536
+ setVisited(prev => (prev.includes(step) ? prev : [...prev, step]));
537
+ setStep(next);
538
+ };
539
+ const handleNameChange = (event: ChangeEvent<HTMLInputElement>) => setName(event.target.value);
540
+ const handleHostChange = ({ value }: { value: string[] }) => setHost(value);
541
+ const handlePathChange = (event: ChangeEvent<HTMLInputElement>) => setPath(event.target.value);
542
+ const handleActionChange = ({ value }: { value: string | null }) =>
543
+ setAction(value === 'monitor' ? 'monitor' : 'block');
544
+
545
+ const handleSubmit = async (event: FormEvent) => {
546
+ event.preventDefault();
547
+ if (!valid.every(Boolean)) {
548
+ setVisited([0, 1, 2]);
549
+ setStep(valid.indexOf(false));
550
+ return;
551
+ }
552
+ setPending(true);
553
+ await saveRequest();
554
+ setPending(false);
555
+ setRules(prev => [
556
+ {
557
+ id: `r-${2042 + prev.length}`,
558
+ name,
559
+ host: host[0] ?? '',
560
+ path,
561
+ methods,
562
+ action,
563
+ updatedAt: new Date().toISOString(),
564
+ },
565
+ ...prev,
566
+ ]);
567
+ setOpen(false);
568
+ toast.create({ id: 'rule-created', title: 'Rule created', type: 'default' });
569
+ };
570
+
571
+ return (
572
+ <Page name='custom-rules' fullSize fixedHeight data-testid='rules-page'>
573
+ <PageHeader>
574
+ <PageHeaderGroup className='flex-1'>
575
+ <PageTitle>Custom rules</PageTitle>
576
+ <PageDescription>
577
+ Rules that block or monitor requests matching your own conditions, on top of the
578
+ built-in attack detection.
579
+ </PageDescription>
580
+ </PageHeaderGroup>
581
+ <PageActions>
582
+ <Button size='medium' onClick={handleCreate} data-testid='rules-create'>
583
+ <Plus />
584
+ Create rule
585
+ </Button>
586
+ </PageActions>
587
+ </PageHeader>
588
+
589
+ <PageToolbar>
590
+ <SearchInput
591
+ value={query}
592
+ onChange={setQuery}
593
+ placeholder='Search name or path'
594
+ className='w-[320px]'
595
+ data-testid='rules-search'
596
+ />
597
+ </PageToolbar>
598
+
599
+ <PageContent>
600
+ <Table
601
+ aria-label='Custom rules'
602
+ data={isLoading ? [] : rows}
603
+ columns={RULE_COLUMNS}
604
+ isLoading={isLoading}
605
+ getRowId={getRuleId}
606
+ data-testid='rules-table'
607
+ />
608
+ </PageContent>
609
+
610
+ <Drawer open={open} onOpenChange={setOpen} data-testid='rule-drawer'>
611
+ <DrawerContent>
612
+ <DrawerHeader>
613
+ <DrawerTitle>Create rule</DrawerTitle>
614
+ </DrawerHeader>
615
+ {/* noValidate: the page validates itself and shows its own errors. */}
616
+ <form noValidate onSubmit={handleSubmit} className='flex min-h-0 flex-1 flex-col'>
617
+ <Stepper
618
+ count={RULE_STEPS.length}
619
+ step={step}
620
+ onStepChange={handleStepChange}
621
+ className='flex min-h-0 flex-1 flex-col'
622
+ data-testid='rule-stepper'
623
+ >
624
+ <StepperList aria-label='Create rule steps'>
625
+ {RULE_STEPS.map((title, i) => (
626
+ <StepperItem key={title} index={i} status={statuses[i]}>
627
+ <StepperTrigger>
628
+ <StepperIndicator />
629
+ <StepperTitle>{title}</StepperTitle>
630
+ </StepperTrigger>
631
+ <StepperSeparator />
632
+ </StepperItem>
633
+ ))}
634
+ </StepperList>
635
+ <CodeSnippetAdapterProvider adapter={loadPrismAdapter}>
636
+ <DrawerBody>
637
+ <StepperContent index={0} className='py-16'>
638
+ <VStack gap={12}>
639
+ <Field required>
640
+ <FieldLabel>
641
+ Name
642
+ <FieldIndicator />
643
+ </FieldLabel>
644
+ <Input
645
+ value={name}
646
+ error={showScopeErrors && Boolean(errors.name)}
647
+ onChange={handleNameChange}
648
+ placeholder='Login credential stuffing'
649
+ data-testid='rule-name'
650
+ />
651
+ {showScopeErrors && errors.name && <FieldError>{errors.name}</FieldError>}
652
+ </Field>
653
+ <div className='grid grid-cols-2 gap-12'>
654
+ <Field required>
655
+ <FieldLabel>
656
+ Host
657
+ <FieldIndicator />
658
+ </FieldLabel>
659
+ <Select
660
+ collection={hostCollection}
661
+ value={host}
662
+ onValueChange={handleHostChange}
663
+ invalid={showScopeErrors && Boolean(errors.host)}
664
+ data-testid='rule-host'
665
+ >
666
+ <SelectInput placeholder='Select a host' />
667
+ <SelectPositioner>
668
+ <SelectContent>
669
+ {hostCollection.items.map(item => (
670
+ <SelectOption key={item.value} item={item}>
671
+ <SelectOptionText>{item.label}</SelectOptionText>
672
+ <SelectOptionIndicator />
673
+ </SelectOption>
674
+ ))}
675
+ </SelectContent>
676
+ </SelectPositioner>
677
+ </Select>
678
+ {showScopeErrors && errors.host && <FieldError>{errors.host}</FieldError>}
679
+ </Field>
680
+ <Field required>
681
+ <FieldLabel>
682
+ Path
683
+ <FieldIndicator />
684
+ </FieldLabel>
685
+ <Input
686
+ value={path}
687
+ error={showScopeErrors && Boolean(errors.path)}
688
+ onChange={handlePathChange}
689
+ placeholder='/v2/login'
690
+ data-testid='rule-path'
691
+ />
692
+ {showScopeErrors && errors.path && <FieldError>{errors.path}</FieldError>}
693
+ </Field>
694
+ </div>
695
+ {/* A direct CheckboxGroup child makes Field a fieldset, so each checkbox keeps its
696
+ own id. Inside a plain field every checkbox label points at the field id,
697
+ and clicking any method toggles the first. */}
698
+ <Field data-testid='rule-methods'>
699
+ <FieldLegend variant='label'>Methods</FieldLegend>
700
+ <FieldDescription>
701
+ Requests with other methods skip this rule.
702
+ </FieldDescription>
703
+ <CheckboxGroup name='methods' value={methods} onValueChange={setMethods}>
704
+ {HTTP_METHODS.map(method => (
705
+ <Checkbox key={method} value={method}>
706
+ <CheckboxIndicator />
707
+ <CheckboxLabel>
708
+ <HttpMethod method={method} />
709
+ </CheckboxLabel>
710
+ </Checkbox>
711
+ ))}
712
+ </CheckboxGroup>
713
+ {showScopeErrors && errors.methods && (
714
+ <FieldError>{errors.methods}</FieldError>
715
+ )}
716
+ </Field>
717
+ </VStack>
718
+ </StepperContent>
719
+
720
+ <StepperContent index={1} className='py-16'>
721
+ <Field required>
722
+ <FieldLabel>
723
+ Condition
724
+ <FieldIndicator />
725
+ </FieldLabel>
726
+ <FieldDescription>
727
+ YAML. A request matches when every item under all is true.
728
+ </FieldDescription>
729
+ <CodeEditorRoot
730
+ value={condition}
731
+ onChange={setCondition}
732
+ language='yaml'
733
+ data-testid='rule-condition'
734
+ >
735
+ <CodeSnippetHeader>
736
+ <CodeSnippetTitle>condition.yaml</CodeSnippetTitle>
737
+ <CodeSnippetActions>
738
+ <CodeSnippetCopyButton />
739
+ <CodeSnippetWrapButton />
740
+ </CodeSnippetActions>
741
+ </CodeSnippetHeader>
742
+ <CodeEditorContent lineNumbers aria-label='Rule condition' />
743
+ </CodeEditorRoot>
744
+ {showConditionErrors && errors.condition && (
745
+ <FieldError>{errors.condition}</FieldError>
746
+ )}
747
+ </Field>
748
+ </StepperContent>
749
+
750
+ <StepperContent index={2} className='py-16'>
751
+ <Field required>
752
+ <FieldLabel>
753
+ Action
754
+ <FieldIndicator />
755
+ </FieldLabel>
756
+ <FieldDescription>What happens to a request that matches.</FieldDescription>
757
+ <RadioGroup
758
+ variant='card'
759
+ name='action'
760
+ value={action}
761
+ onValueChange={handleActionChange}
762
+ >
763
+ <Radio value='block'>
764
+ <RadioIndicator />
765
+ <RadioLabel>Block</RadioLabel>
766
+ <RadioDescription>
767
+ Return 403 and record the request as an attack.
768
+ </RadioDescription>
769
+ </Radio>
770
+ <Radio value='monitor'>
771
+ <RadioIndicator />
772
+ <RadioLabel>Monitor</RadioLabel>
773
+ <RadioDescription>
774
+ Let the request through and record it as an attack. Use it to tune a new
775
+ rule first.
776
+ </RadioDescription>
777
+ </Radio>
778
+ </RadioGroup>
779
+ </Field>
780
+ </StepperContent>
781
+
782
+ <StepperContent index={3} className='py-16'>
783
+ <VStack gap={16}>
784
+ <Alert color='info'>
785
+ <AlertIcon />
786
+ <AlertContent>
787
+ <AlertTitle>Applies within a minute</AlertTitle>
788
+ <AlertDescription>
789
+ Every node picks up the rule on its next sync. Requests already in
790
+ flight are not rechecked.
791
+ </AlertDescription>
792
+ </AlertContent>
793
+ </Alert>
794
+ <div>
795
+ <VStack gap={6} className='pb-8' data-testid='rule-review'>
796
+ <Attribute orientation='horizontal'>
797
+ <AttributeLabel width={140}>Name</AttributeLabel>
798
+ <AttributeValue>{name}</AttributeValue>
799
+ </Attribute>
800
+ <Attribute orientation='horizontal'>
801
+ <AttributeLabel width={140}>Host</AttributeLabel>
802
+ <AttributeValue>{host[0]}</AttributeValue>
803
+ </Attribute>
804
+ <Attribute orientation='horizontal'>
805
+ <AttributeLabel width={140}>Path</AttributeLabel>
806
+ <AttributeValue>
807
+ <InlineCodeSnippet code={path} size='md' />
808
+ </AttributeValue>
809
+ </Attribute>
810
+ <Attribute orientation='horizontal'>
811
+ <AttributeLabel width={140}>Methods</AttributeLabel>
812
+ <AttributeValue>
813
+ <HStack gap={4}>
814
+ {methods.map(method => (
815
+ <HttpMethod key={method} method={method} />
816
+ ))}
817
+ </HStack>
818
+ </AttributeValue>
819
+ </Attribute>
820
+ <Attribute orientation='horizontal'>
821
+ <AttributeLabel width={140}>Action</AttributeLabel>
822
+ <AttributeValue>
823
+ <Badge variant='dotted' type='secondary' color={ACTION[action].color}>
824
+ {ACTION[action].label}
825
+ </Badge>
826
+ </AttributeValue>
827
+ </Attribute>
828
+ </VStack>
829
+ <Separator spacing={4} />
830
+ <HStack justify='between' align='center' className='py-8'>
831
+ <Heading as='h3' size='md' weight='medium'>
832
+ Condition
833
+ </Heading>
834
+ </HStack>
835
+ <CodeSnippetRoot code={condition} language='yaml'>
836
+ <CodeSnippetContent>
837
+ <CodeSnippetLineNumbers />
838
+ <CodeSnippetCode />
839
+ </CodeSnippetContent>
840
+ </CodeSnippetRoot>
841
+ </div>
842
+ </VStack>
843
+ </StepperContent>
844
+ </DrawerBody>
845
+ </CodeSnippetAdapterProvider>
846
+ <DrawerFooter>
847
+ <DrawerFooterControls placement='left'>
848
+ <DrawerClose asChild>
849
+ <Button variant='ghost' color='neutral'>
850
+ Cancel
851
+ </Button>
852
+ </DrawerClose>
853
+ </DrawerFooterControls>
854
+ <DrawerFooterControls placement='right'>
855
+ <StepperPrevTrigger asChild>
856
+ <Button variant='outline' color='neutral' data-testid='rule-back'>
857
+ Back
858
+ </Button>
859
+ </StepperPrevTrigger>
860
+ {last ? (
861
+ <Button type='submit' loading={pending} data-testid='rule-submit'>
862
+ Create rule
863
+ </Button>
864
+ ) : (
865
+ <StepperNextTrigger asChild>
866
+ <Button data-testid='rule-next'>Next</Button>
867
+ </StepperNextTrigger>
868
+ )}
869
+ </DrawerFooterControls>
870
+ </DrawerFooter>
871
+ </Stepper>
872
+ </form>
873
+ </DrawerContent>
874
+ </Drawer>
875
+ </Page>
876
+ );
877
+ };
878
+ ```
879
+
880
+ ## Related
881
+
882
+ - [Create and edit forms](../patterns/create-edit-forms.md)
883
+ - [Resource list page](../patterns/resource-list-page.md)
884
+ - [Detail drawer](../patterns/detail-drawer.md)
885
+ - [Confirmation dialogs](../patterns/confirmation-dialogs.md): guard a dirty drawer on close.
886
+ - [Feedback toasts](../patterns/feedback-toasts.md)