-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomponents.py
210 lines (178 loc) · 6.4 KB
/
components.py
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
from uuid import uuid4
from channels.db import database_sync_to_async
from django_idom.components import static_css
from idom import component, hooks
from idom.html import _, a, button, div, h4, h5, li, ol, p
from conreq import config
from conreq._core.app_store.models import Category, Subcategory
from conreq._core.utils import tab_constructor
from conreq.app import register
class PlaceholderApp:
uuid = 0
name = "My App Name"
short_description = "This is a placeholder description. This will be removed in the future. Please pay no attention to this text. But what if the text gets too long? Who knows what will happen, but I can probably guess that it'll be great! Or maybe it won't be, only time will tell."
author = "Author"
version = "0.0.0"
category = "Category"
@register.component.app_store()
@component
def app_store(websocket, state, set_state):
# pylint: disable=unused-argument
tab, set_tab = hooks.use_state(None)
categories, set_categories = hooks.use_state({})
ready, set_ready = hooks.use_state(False)
@hooks.use_effect
async def load_from_db():
if categories:
return
new_categories = await get_categories()
if new_categories:
set_categories(new_categories)
# TODO: Update app store entries every first load
return _(
static_css("conreq/app_store.css"),
static_css("conreq/buttons.css"),
tab(key=tab.__name__)
if tab
else div(
{"className": "spotlight-region"},
recently_added(set_tab),
recently_updated(set_tab),
most_popular(set_tab),
top_downloaded(set_tab),
our_favorites(set_tab),
essentials(set_tab),
random_selection(set_tab),
key="spotlight-region",
),
div({"className": "nav-region"}, nav_constructor(categories, set_tab)),
)
@database_sync_to_async
def get_categories() -> dict[Category, list[Subcategory]]:
# FIXME: Django ORM currently does not conveniently support running within IDOM.
query = Subcategory.objects.select_related("category").order_by("name").all()
categories = {}
for subcategory in query:
categories.setdefault(subcategory.category, []).append(subcategory)
return categories
def nav_constructor(categories: dict[Category, list[Subcategory]], set_tab) -> list:
return [
div(
{"className": "nav-item"},
a(
{
"href": f"#{category.uuid}",
"onClick": lambda x: print("clicked"),
},
h5({"className": "nav-title"}, category.name),
),
ol(
{"className": "nav-sub"},
[
li(
{"className": "nav-sub-item"},
a(
{
"className": "nav-sub-link",
"href": f"#{subcategory.uuid}",
"onClick": lambda x: print("clicked"),
},
subcategory.name,
),
key=str(subcategory.uuid),
)
for subcategory in value
],
),
key=str(category.uuid),
)
for category, value in categories.items()
]
def recently_added(set_tab):
return spotlight("Recently Added", "The latest from our community.", set_tab)
def recently_updated(set_tab):
return spotlight(
"Recently Updated", "Actively maintained for all to enjoy.", set_tab
)
def most_popular(set_tab):
return spotlight(
"Most Popular", "These have gained a serious amount lot of love.", set_tab
)
def top_downloaded(set_tab):
return spotlight("Top Downloaded", "Tons of downloads!", set_tab)
def our_favorites(set_tab):
return spotlight("Our Favorites", "A curated list of our favorites.", set_tab)
def essentials(set_tab):
return spotlight(
"Essentials", "Something we think all users should consider.", set_tab
)
def random_selection(set_tab):
return spotlight("Random Selection", "Are you feeling lucky today?", set_tab)
def spotlight(title, description, set_tab, apps=None):
return div(
{"className": "spotlight"},
a(
{"href": "#", "onClick": lambda x: print("clicked")},
h4({"className": "title"}, title),
p({"className": "description"}, description),
),
div(
{"className": "card-stage"},
[card(set_tab) for _ in range(8)],
),
)
def card(set_tab, app: PlaceholderApp = PlaceholderApp()):
return div(
{"className": "card"},
div(
{"className": "top"},
div(
{"className": "text-region"},
h5(
{"className": "card-title"},
a(
{"href": f"#{app.uuid}", "onClick": lambda x: print("clicked")},
app.name,
),
),
div(
{"className": "card-author"},
a(
{"href": "#", "onClick": lambda x: print("clicked")},
app.author,
),
),
div(
{"className": "card-category"},
a(
{"href": "#", "onClick": lambda x: print("clicked")},
app.category,
),
),
),
div({"className": "image"}),
),
div(
{"className": "btn-container"},
button(
{
"className": "btn btn-sm btn-secondary",
"onClick": lambda x: print("clicked"),
},
"Details",
),
button(
{
"className": "btn btn-sm btn-primary",
"onClick": lambda x: print("clicked"),
},
"Install",
),
),
div({"className": "description"}, app.short_description),
key=str(uuid4()),
)
# pylint: disable=protected-access
config._homepage.admin_nav_tabs[1] = tab_constructor(
"App Store", app_store, html_class="app-store"
)