Skip to content

Commit 7fca2af

Browse files
authored
Fix open source templates filtering (#1725)
* Fix templates warning * fix filtering
1 parent 6afbbfd commit 7fca2af

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed

pcweb/components/docpage/navbar/typesense.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ def typesense_search() -> rx.Component:
717717
search_content(),
718718
on_interact_outside=SimpleSearch.reset_search,
719719
on_escape_key_down=SimpleSearch.reset_search,
720-
class_name="w-full max-w-[650px] mx-auto bg-secondary-1 border-none outline-none p-3 lg:!fixed lg:!top-24 lg:!left-1/2 lg:!transform lg:!-translate-x-1/2 lg:!translate-y-0 lg:!m-0 font-sans "
720+
class_name="!font-sans w-full max-w-[650px] mx-auto bg-secondary-1 border-none outline-none p-3 lg:!fixed lg:!top-24 lg:!left-1/2 lg:!transform lg:!-translate-x-1/2 lg:!translate-y-0 lg:!m-0 "
721721
+ rx.cond(SimpleSearch.query.length() < 3, "min-h[57vh]", "h-[57vh]"),
722722
),
723723
),

pcweb/pages/gallery/sidebar.py

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pcweb.components.new_button import button
44
from pcweb.components.user_input import input
5+
from pcweb.pages.gallery.apps import gallery_apps_data
56

67
TAGS = {
78
"Category": [
@@ -17,62 +18,52 @@
1718
}
1819

1920
ITEMS_PER_PAGE = 12
21+
TEMPLATES_FOLDER = "templates/"
2022

2123

22-
class TemplatesState(rx.State):
23-
query: rx.Field[str] = rx.field("")
24-
checked_tags: rx.Field[set[str]] = rx.field(default_factory=set)
25-
page: rx.Field[int] = rx.field(1)
26-
total_pages: rx.Field[int] = rx.field(1)
27-
28-
def _get_all_filtered_templates(self) -> list[str]:
29-
from pcweb.pages.gallery.apps import gallery_apps_data
30-
31-
filtered = []
32-
for (_path, folder), document in gallery_apps_data.items():
33-
if folder != "templates/":
34-
continue
35-
36-
app_metadata = document.metadata
37-
app_title = app_metadata.get("title", "")
38-
app_description = app_metadata.get("description", "")
39-
app_tags = app_metadata.get("tags", [])
40-
41-
# print(app_metadata, app_title, app_tags)
42-
43-
# Text search filtering
44-
if self.query.strip():
45-
query_lower = self.query.lower()
46-
if not (
47-
query_lower in app_title.lower()
48-
or query_lower in app_description.lower()
49-
):
50-
continue
24+
TEMPLATE_SUMMARIES = [
25+
{
26+
"title": (m := doc.metadata or {}).get("title", ""),
27+
"description": m.get("description", ""),
28+
"tags": m.get("tags", []),
29+
}
30+
for (_, folder), doc in gallery_apps_data.items()
31+
if folder == TEMPLATES_FOLDER
32+
]
5133

52-
# Tag filtering
53-
if self.checked_tags and not (set(app_tags) & self.checked_tags):
54-
continue
5534

56-
filtered.append(app_title)
57-
58-
return filtered
35+
class TemplatesState(rx.State):
36+
query: rx.Field[str] = rx.field(default="")
37+
checked_tags: rx.Field[set[str]] = rx.field(default_factory=set)
38+
page: rx.Field[int] = rx.field(default=1)
5939

6040
@rx.event
6141
def clear_filters(self):
6242
self.checked_tags = set()
6343
self.page = 1
6444

45+
@rx.var
46+
def all_filtered_templates(self) -> list[str]:
47+
query = self.query.strip().lower()
48+
return [
49+
t["title"]
50+
for t in TEMPLATE_SUMMARIES
51+
if (
52+
not query
53+
or query in t["title"].lower()
54+
or query in t["description"].lower()
55+
)
56+
and (not self.checked_tags or set(t["tags"]) & self.checked_tags)
57+
]
58+
59+
@rx.var
60+
def total_pages(self) -> int:
61+
return max(1, -(-len(self.all_filtered_templates) // ITEMS_PER_PAGE))
62+
6563
@rx.var
6664
def filtered_templates(self) -> list[str]:
67-
all_filtered = self._get_all_filtered_templates()
68-
self.total_pages = (
69-
(len(all_filtered) + ITEMS_PER_PAGE - 1) // ITEMS_PER_PAGE
70-
if all_filtered
71-
else 1
72-
)
7365
start = (self.page - 1) * ITEMS_PER_PAGE
74-
end = start + ITEMS_PER_PAGE
75-
return all_filtered[start:end]
66+
return self.all_filtered_templates[start : start + ITEMS_PER_PAGE]
7667

7768
@rx.event
7869
def set_query(self, value: str):
@@ -129,9 +120,9 @@ def checkbox_item(text: str, value: str):
129120
return rx.box(
130121
rx.checkbox(
131122
checked=TemplatesState.checked_tags.contains(value),
132-
on_change=TemplatesState.toggle_template(value),
133123
color_scheme="violet",
134124
key=value,
125+
class_name="cursor-pointer",
135126
),
136127
rx.text(
137128
text,

0 commit comments

Comments
 (0)