Skip to content

Commit

Permalink
fix: filters and performance (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk4b1r authored Sep 15, 2024
1 parent 16f2ac7 commit f1fe372
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/components/CaptureTheFlag/CTF.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const CTF = () => {
const [setFilteredCTFs] = useState(ctfs);

useEffect(() => {
const filteredCTFs = Array.isArray(ctfs)
? ctfs.filter((challenge) => {
const filteredCTFs = Array?.isArray(ctfs)
? ctfs?.filter((challenge) => {
const nameMatches = challenge?.challengeName?.toLowerCase().includes(searchTerm?.toLowerCase());
const difficultyMatches =
selectedDifficulty === "all" || challenge?.difficulty === selectedDifficulty;
Expand Down
2 changes: 1 addition & 1 deletion src/components/WebSecurity/CodeReviews/RoomCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function CodeReviewCard(props) {
background: "#090909",
}}
>
{props.tags.map((data) => {
{props.tags?.map((data) => {
return <RoomTags key={data}>{data}</RoomTags>;
})}
</div>
Expand Down
25 changes: 13 additions & 12 deletions src/components/WebSecurity/Common/Labs/Labs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Labs = ({ LabData, heading }) => {
};

const renderLevelButtons = (levels) => {
return levels.map((level, index) => (
return levels?.map((level, index) => (
<LevelButton
key={index}
style={{
Expand All @@ -37,16 +37,17 @@ const Labs = ({ LabData, heading }) => {
const levels = ["All", "Beginner", "Intermediate", "Advance"];

// Filtered LabData based on search input
const filteredLabData =
LabData?.filter(
(data) =>
(data?.level === levelActive || levelActive === "All") &&
(data?.category === categoryActive || categoryActive === "All") &&
(data?.title.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.description.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.level.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.tags.some((tag) => tag.toLowerCase().includes(searchInput.toLowerCase()))),
) || [];
const filteredLabData = LabData
? LabData?.filter(
(data) =>
(data?.level === levelActive || levelActive === "All") &&
(data?.category === categoryActive || categoryActive === "All") &&
(data?.title.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.description.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.level.toLowerCase().includes(searchInput.toLowerCase()) ||
data?.tags.some((tag) => tag.toLowerCase().includes(searchInput.toLowerCase()))),
) || []
: [];

return (
<>
Expand All @@ -66,7 +67,7 @@ const Labs = ({ LabData, heading }) => {
<Input placeholder="Search" value={searchInput} onChange={handleSearchInputChange} />
</div>
<div className="room-cards-container">
{filteredLabData.map((data, index) => (
{filteredLabData?.map((data, index) => (
<RoomCard
key={index}
title={data?.title}
Expand Down
6 changes: 3 additions & 3 deletions src/components/WebSecurity/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Sidebar = ({ heading, topic, topics, onSelectSubtopic, setCategoryActive,

// get unique and sorted categories

const categories = [...new Set(topics.map((topic) => topic.category))].sort();
const categories = [...new Set(topics?.map((topic) => topic.category))].sort();

const sidebarHeader = heading || topic?.category;

Expand Down Expand Up @@ -151,7 +151,7 @@ const Sidebar = ({ heading, topic, topics, onSelectSubtopic, setCategoryActive,
}}
>
{onlyCat &&
categories.map((category, index) => (
categories?.map((category, index) => (
<MainTitle
key={index}
onClick={() => setCategoryActive(category)}
Expand All @@ -177,7 +177,7 @@ const Sidebar = ({ heading, topic, topics, onSelectSubtopic, setCategoryActive,
))}
</div>

{topics.map((topic, index) =>
{topics?.map((topic, index) =>
!onlyCat ? (
<div key={index} style={{ width: "100%" }}>
<MainTitle
Expand Down
21 changes: 11 additions & 10 deletions src/components/WebSecurity/WebSecurityTopics/Topics.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Topics = () => {
};

const renderLevelButtons = (levels) => {
return levels.map((level, index) => (
return levels?.map((level, index) => (
<LevelButton
key={index}
style={{
Expand All @@ -70,14 +70,15 @@ const Topics = () => {

const levels = ["All", "Beginner", "Intermediate", "Advance"];

const filteredTopics =
topics.filter(
(topic) =>
(topic.level === levelActive || levelActive === "All") &&
(topic.category === categoryActive || categoryActive === "All") &&
(topic.title.toLowerCase().includes(searchInput.toLowerCase()) ||
topic.tags.some((tag) => tag.toLowerCase().includes(searchInput.toLowerCase()))),
) || [];
const filteredTopics = topics
? topics?.filter(
(topic) =>
(topic.level === levelActive || levelActive === "All") &&
(topic.category === categoryActive || categoryActive === "All") &&
(topic.title.toLowerCase().includes(searchInput.toLowerCase()) ||
topic.tags.some((tag) => tag.toLowerCase().includes(searchInput.toLowerCase()))),
) || []
: [];

if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error}</p>;
Expand All @@ -98,7 +99,7 @@ const Topics = () => {
<Input placeholder="Search" value={searchInput} onChange={handleSearchInputChange} />
</div>
<TopicCards className={"room-cards-container"}>
{filteredTopics.map((topic) => (
{filteredTopics?.map((topic) => (
<RouterLink
to={`/websecurity/topic/${topic._id}`}
style={{ textDecoration: "none", width: "100%" }}
Expand Down

0 comments on commit f1fe372

Please sign in to comment.