Skip to content

Commit afb6496

Browse files
author
jdv
committed
test interactiveCheckboxes
1 parent 20bb7f5 commit afb6496

File tree

5 files changed

+178
-17
lines changed

5 files changed

+178
-17
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import React, { useState, useEffect, createContext, useContext } from 'react';
2+
3+
// Global context to manage checkbox states across all instances
4+
const CheckboxContext = createContext();
5+
6+
// Provider component to wrap your MDX content
7+
export const CheckboxProvider = ({ children }) => {
8+
const [checkboxStates, setCheckboxStates] = useState({});
9+
const [manualChecks, setManualChecks] = useState({});
10+
11+
const updateCheckbox = (id, isManuallyChecked) => {
12+
setManualChecks(prev => ({
13+
...prev,
14+
[id]: isManuallyChecked
15+
}));
16+
};
17+
18+
const isCheckboxChecked = (id, references = []) => {
19+
// Manual check has priority
20+
if (manualChecks[id]) {
21+
return { checked: true, type: 'manual' };
22+
}
23+
24+
// Check if all references are checked (either manually or through their references)
25+
if (references.length > 0) {
26+
const allReferencesChecked = references.every(refId => {
27+
if (manualChecks[refId]) return true;
28+
// Recursively check if reference has its own references
29+
return checkboxStates[refId]?.checked;
30+
});
31+
32+
if (allReferencesChecked) {
33+
return { checked: true, type: 'reference' };
34+
}
35+
}
36+
37+
return { checked: false, type: 'none' };
38+
};
39+
40+
useEffect(() => {
41+
// Update all checkbox states when manual checks change
42+
const newStates = {};
43+
Object.keys(checkboxStates).forEach(id => {
44+
const checkbox = checkboxStates[id];
45+
newStates[id] = {
46+
...checkbox,
47+
...isCheckboxChecked(id, checkbox.references)
48+
};
49+
});
50+
setCheckboxStates(newStates);
51+
}, [manualChecks]);
52+
53+
const registerCheckbox = (id, references = []) => {
54+
setCheckboxStates(prev => ({
55+
...prev,
56+
[id]: {
57+
references,
58+
...isCheckboxChecked(id, references)
59+
}
60+
}));
61+
};
62+
63+
return (
64+
<CheckboxContext.Provider value={{
65+
checkboxStates,
66+
manualChecks,
67+
updateCheckbox,
68+
registerCheckbox,
69+
isCheckboxChecked
70+
}}>
71+
{children}
72+
</CheckboxContext.Provider>
73+
);
74+
};
75+
76+
// The main checkbox component
77+
export const InteractiveCheckbox = ({
78+
id,
79+
references = [],
80+
label = '',
81+
className = ''
82+
}) => {
83+
const {
84+
checkboxStates,
85+
manualChecks,
86+
updateCheckbox,
87+
registerCheckbox,
88+
isCheckboxChecked
89+
} = useContext(CheckboxContext);
90+
91+
useEffect(() => {
92+
registerCheckbox(id, references);
93+
}, [id, references]);
94+
95+
const handleClick = () => {
96+
updateCheckbox(id, !manualChecks[id]);
97+
};
98+
99+
const checkboxState = isCheckboxChecked(id, references);
100+
const isChecked = checkboxState.checked;
101+
const checkType = checkboxState.type;
102+
103+
// Determine the color based on check type
104+
const getCheckmarkColor = () => {
105+
if (checkType === 'manual') return '#22c55e'; // green
106+
if (checkType === 'reference') return '#3b82f6'; // blue
107+
return '#d1d5db'; // gray (unchecked)
108+
};
109+
110+
const getBackgroundColor = () => {
111+
if (isChecked) return getCheckmarkColor();
112+
return 'transparent';
113+
};
114+
115+
return (
116+
<div className={`inline-flex items-center gap-2 ${className}`}>
117+
<div
118+
onClick={handleClick}
119+
style={{
120+
width: '20px',
121+
height: '20px',
122+
border: `2px solid ${isChecked ? getCheckmarkColor() : '#d1d5db'}`,
123+
borderRadius: '3px',
124+
backgroundColor: getBackgroundColor(),
125+
cursor: 'pointer',
126+
display: 'flex',
127+
alignItems: 'center',
128+
justifyContent: 'center',
129+
transition: 'all 0.2s ease'
130+
}}
131+
>
132+
{isChecked && (
133+
<svg
134+
width="12"
135+
height="12"
136+
viewBox="0 0 12 12"
137+
fill="none"
138+
stroke="white"
139+
strokeWidth="2"
140+
strokeLinecap="round"
141+
strokeLinejoin="round"
142+
>
143+
<path d="M2 6l2.5 2.5L10 3" />
144+
</svg>
145+
)}
146+
</div>
147+
{label && (
148+
<label
149+
onClick={handleClick}
150+
style={{ cursor: 'pointer', userSelect: 'none' }}
151+
>
152+
{label}
153+
</label>
154+
)}
155+
</div>
156+
);
157+
};
158+
159+
// Export both components as default for easier importing
160+
export default InteractiveCheckbox;

crowdsec-docs/unversioned/user_guides/interactive_se_install/01_install.mdx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ pagination_next: user_guides/interactive_se_install/se_install_02
77
import Tabs from '@theme/Tabs';
88
import TabItem from '@theme/TabItem';
99
import CodeBlock from '@theme/CodeBlock';
10+
import { InteractiveCheckbox } from '@site/src/components/InteractiveCheckbox.js';
1011

11-
# Interractive Security Engine Installation Guide
12+
# Interactive Security Engine Installation Guide
1213

1314
Welcome! This interactive guide will help you set up your CrowdSec Security Engine and validate each step to ensure proper operation.
1415
We'll guide you through detecting and remediating malicious behavior in your services' logs, regardless of your chosen implementation (on host, Docker, Kubernetes)
@@ -69,9 +70,20 @@ But if you're comfortable with Docker it also is a great way to get started and
6970

7071
Let's check that CrowdSec is running and able to retrieve the community blocklist !
7172

73+
#### Enroll your Security Engine into CrowdSec Console
74+
For advanced monitoring and trouble shooting the CrowdSec Console is a great tool to visualize your Security Engine's activity and alerts.
75+
76+
<InteractiveCheckbox id="ConsoleEnroll" label=" Enroll into the console " />
77+
78+
- [link to doc]
79+
- You'll see a confirmation pop up in the console for enrollment if not you might have conectivity issue to the central API [link to troubleshooting section]
80+
- You'll see the last heartbeat and the status of your Security Engine in the console
81+
- You'll be able to check various configurations for the upcoming steps of the installation
82+
- You'll be warned when a new version of CrowdSec is available
83+
7284
#### CrowdSec installation health
7385

74-
> [ ] Check that the CrowdSec service is running
86+
<InteractiveCheckbox id="CrowdsecRunning" label="Check that the CrowdSec service is running" references={["ConsoleEnroll"]} />
7587

7688
```bash
7789
systemctl status crowdsec
@@ -113,18 +125,7 @@ sudo cscli capi status
113125
- Pulling community blocklist is enabled //+link to doc where to turn this on/off ?
114126
- Pulling blocklists from the console is enabled //+link to doc where to turn this on/off ?
115127

116-
#### Enroll your Security Engine into CrowdSec Console
117-
For advanced monitoring and trouble shooting the CrowdSec Console is a great tool to visualize your Security Engine's activity and alerts.
118-
119-
> [ ] Enroll into the console
120-
121-
- [link to doc]
122-
- You'll see a confirmation pop up in the console for enrollment if not you might have conectivity issue to the central API [link to troubleshooting section]
123-
- You'll see the last heartbeat and the status of your Security Engine in the console
124-
- You'll be able to check various configurations for the upcoming steps of the installation
125-
- You'll be warned when a new version of CrowdSec is available
126-
127-
### Troubleshooting
128+
### 🚨 Troubleshooting
128129
<details>
129130
<summary>There could be ports conflicts with other services</summary>
130131

crowdsec-docs/unversioned/user_guides/interactive_se_install/02_parsers_scenarios copy.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ cscli scenarios list
6767
When clicking on the "Scenarios" part of your [Security engine tile in the CrowdSec console](https://app.crowdsec.net/security-engines), you should see the scenarios you installed listed there.
6868
The tile itself should show you the count of scenarios that are installed on your Security Engine.
6969

70-
### Troubleshooting
70+
### 🚨 Troubleshooting
7171

7272
<details>
7373
<summary>Missing Scenarios</summary>

crowdsec-docs/unversioned/user_guides/interactive_se_install/03_acquisition copy.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ sudo cscli alerts list
5454
```
5555
- The alerts will also show up in the console within a few minutes if you enrolled your Security Engine.
5656

57-
### Troubleshooting
57+
### 🚨 Troubleshooting
5858
If you never see any alerts: However unlikely it's possible that you're not being hit by attacks but first lets check what could be wrong.
5959

6060
<details>

crowdsec-docs/unversioned/user_guides/interactive_se_install/04_remediation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cscli bouncers list
4444
- ??? How do we do this if we don't have a canary? without asking them to ban themselves
4545
// find an alternative while we don't have a canary system @thib @seb @laurence
4646

47-
### Troubleshooting
47+
### 🚨 Troubleshooting
4848

4949
<details>
5050
<summary>Docker communication with bouncer</summary>

0 commit comments

Comments
 (0)