Skip to content

Commit 0f4400b

Browse files
simonwclaude
andcommitted
Redesign /-/queries to match the new homepage
The global and per-database query lists now use the homepage's search box, chips and compact query rows: - Live search with ?_fragment=results, sharing homepage.js: up/down and Enter pick a query, Escape clears. On these pages the arrow keys work even with an empty search box. - Database chips with matching counts that keep the current search. - Mode and visibility filters as chips, shown only when they offer a choice. - Rows show database, description or SQL, inputs (read from the SQL when not declared) and owner, tagged yours, writes, private, or trusted for non-config queries. - Homepage-style pagination footer. Also de-duplicates inputs derived from SQL on the homepage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9c49a52 commit 0f4400b

11 files changed

Lines changed: 371 additions & 358 deletions

File tree

‎datasette/homepage.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ async def build_catalog(
350350
except ValueError:
351351
parameters = []
352352
if not parameters:
353-
parameters = named_parameters(row["sql"])
353+
# A parameter used twice in the SQL is still one input
354+
parameters = list(dict.fromkeys(named_parameters(row["sql"])))
354355
queries.append(
355356
{
356357
"database": row["database_name"],

‎datasette/static/app.css‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6573,7 +6573,8 @@ button.column-menu-trigger[aria-expanded="true"] {
65736573
background: #fff3b0;
65746574
color: var(--ink);
65756575
}
6576-
.homepage a.homepage-database.empty {
6576+
.homepage a.homepage-database.empty,
6577+
.homepage span.homepage-database.empty {
65776578
opacity: 0.5;
65786579
}
65796580

@@ -6811,3 +6812,45 @@ button.column-menu-trigger[aria-expanded="true"] {
68116812
.homepage-recent[hidden] {
68126813
display: none;
68136814
}
6815+
6816+
/* Query list (/-/queries): the homepage's search, chips and query rows */
6817+
.query-list-filters {
6818+
display: flex;
6819+
flex-direction: column;
6820+
align-items: flex-start;
6821+
gap: 0.5rem;
6822+
margin: 0 0 1rem;
6823+
}
6824+
.query-list-filters .homepage-databases {
6825+
flex-wrap: wrap;
6826+
margin: 0;
6827+
}
6828+
.query-list-facets .homepage-databases-label + .homepage-database {
6829+
margin-left: 0;
6830+
}
6831+
.query-list-facets .homepage-database + .homepage-databases-label {
6832+
margin-left: 0.75rem;
6833+
}
6834+
.homepage-searching.query-list-page .homepage-databases {
6835+
display: inline-flex;
6836+
}
6837+
.query-list-heading-db {
6838+
vertical-align: middle;
6839+
font-size: 0.875rem;
6840+
}
6841+
.type-tag-private {
6842+
color: #8a3b52;
6843+
background: #f7edf0;
6844+
}
6845+
.type-tag-trusted {
6846+
color: #1f7a4d;
6847+
background: #e7f5ec;
6848+
}
6849+
.query-row-owner {
6850+
font-family: ui-monospace, monospace;
6851+
font-size: 0.8125rem;
6852+
}
6853+
.query-list-page .homepage-queries,
6854+
.homepage-searching.query-list-page .homepage-queries {
6855+
margin-top: 0;
6856+
}

‎datasette/static/homepage.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Homepage: live search over the catalog, and plugin-provided sections.
2+
// The /-/queries pages use the same live search and keyboard navigation.
23
(function () {
34
const form = document.querySelector("[data-homepage-search]");
45
const results = document.querySelector("[data-homepage-results]");
@@ -23,6 +24,11 @@
2324
// a highlight through the result links in page order (tables, then
2425
// matching queries) and Enter opens the highlighted one.
2526
const RESULT_LINKS = ".catalog-item-name, .query-row-name";
27+
// On /-/queries the list is the whole page, so the arrow keys move
28+
// through it even before anything has been typed
29+
const browseWhenEmpty = !!(
30+
root && root.hasAttribute("data-browse-when-empty")
31+
);
2632
let active = -1;
2733
const resultLinks = () => [...results.querySelectorAll(RESULT_LINKS)];
2834
const setActive = (index) => {
@@ -47,7 +53,9 @@
4753
} else {
4854
params.set("q", q);
4955
}
56+
// A new search starts again from the first page
5057
params.delete("page");
58+
params.delete("_next");
5159
const qs = params.toString();
5260
return form.action + (qs ? "?" + qs : "");
5361
};
@@ -127,14 +135,17 @@
127135
update("");
128136
} else if (
129137
(event.key === "ArrowDown" || event.key === "ArrowUp") &&
130-
input.value.trim()
138+
(input.value.trim() || browseWhenEmpty)
131139
) {
132140
event.preventDefault();
133141
setActive(active + (event.key === "ArrowDown" ? 1 : -1));
134142
if (active < 0) {
135143
setActive(0);
136144
}
137-
} else if (event.key === "Enter" && input.value.trim()) {
145+
} else if (
146+
event.key === "Enter" &&
147+
(input.value.trim() || (browseWhenEmpty && active >= 0))
148+
) {
138149
event.preventDefault();
139150
const openActive = () => {
140151
const link = resultLinks()[active];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% include "_query_list_summary.html" %}
2+
<div data-homepage-results>
3+
{% include "_query_list_results.html" %}
4+
</div>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{% set multiple_databases = database_chips|length > 1 %}
2+
{# A filter group is only useful with a choice in it, or one already applied #}
3+
{% set useful_facets = [] %}
4+
{% for facet in facets %}{% if facet["items"]|length > 1 or facet["items"]|selectattr("active")|list %}{% set _ = useful_facets.append(facet) %}{% endif %}{% endfor %}
5+
{% if database_chips or useful_facets %}
6+
<div class="query-list-filters">
7+
{% if database_chips %}
8+
<nav class="homepage-databases" aria-labelledby="query-list-databases-label">
9+
<span class="homepage-databases-label" id="query-list-databases-label">Databases</span>
10+
<a href="{{ all_queries_href }}" class="homepage-database{% if not database %} selected{% endif %}"{% if not database %} aria-current="true"{% endif %}>All</a>
11+
{% for chip in database_chips %}
12+
<a href="{{ chip.href }}" class="homepage-database{% if chip.selected %} selected{% endif %}{% if not chip.count %} empty{% endif %}" style="--db-color: #{{ chip.color }}"{% if chip.selected %} aria-current="true"{% endif %}>
13+
<span class="db-swatch" aria-hidden="true"></span>{{ chip.name }}
14+
<span class="homepage-database-count">{{ "{:,}".format(chip.count) }}</span>
15+
</a>
16+
{% endfor %}
17+
</nav>
18+
{% endif %}
19+
{% if useful_facets %}
20+
<nav class="homepage-databases query-list-facets" aria-label="Query filters">
21+
{% for facet in useful_facets %}
22+
<span class="homepage-databases-label">{{ facet.title }}</span>
23+
{% for item in facet["items"] %}
24+
{% if item.href %}<a class="homepage-database{% if item.active %} selected{% endif %}" href="{{ item.href }}"{% if item.active %} aria-current="true"{% endif %}>{% else %}<span class="homepage-database empty">{% endif %}{{ item.label }} <span class="homepage-database-count">{{ "{:,}".format(item.count) }}</span>{% if item.href %}</a>{% else %}</span>{% endif %}
25+
{% endfor %}
26+
{% endfor %}
27+
</nav>
28+
{% endif %}
29+
</div>
30+
{% endif %}
31+
32+
<section class="homepage-queries" aria-label="Queries">
33+
{% if queries %}
34+
<ul class="query-list">
35+
{% for query in queries %}
36+
<li class="query-row">
37+
<a class="query-row-name" href="{{ urls.query(query.database, query.name) }}{% if query.fragment %}#{{ query.fragment }}{% endif %}">{{ query.title or query.name }}</a>
38+
{%- if query.private %} 🔒{% endif %}
39+
{% if actor_id and query.owner_id == actor_id %}<span class="type-tag type-tag-mine">yours</span>{% endif %}
40+
{% if query.is_write %}<span class="type-tag type-tag-write">writes</span>{% endif %}
41+
{% if query.is_private %}<span class="type-tag type-tag-private" title="Only the owner can view this query">private</span>{% endif %}
42+
{% if query.is_trusted and query.source != "config" %}<span class="type-tag type-tag-trusted" title="Runs without the usual SQL and write permission checks">trusted</span>{% endif %}
43+
{% if multiple_databases and not database %}<span class="db-tag" style="--db-color: #{{ (database_chips|selectattr('name', 'equalto', query.database)|first or {}).color }}">{{ query.database }}</span>{% endif %}
44+
<span class="query-row-detail">
45+
{%- if query.description %}{{ query.description }}{% elif not query.hide_sql %}<code>{{ query.sql|truncate(120) }}</code>{% endif -%}
46+
{%- set parameters = query_parameters[(query.database, query.name)] %}
47+
{%- if parameters %} · inputs: {% for p in parameters %}<code>{{ p }}</code>{% if not loop.last %}, {% endif %}{% endfor %}{% endif -%}
48+
{%- if query.owner_id and query.owner_id != actor_id %} · by <span class="query-row-owner">{{ query.owner_id }}</span>{% endif -%}
49+
</span>
50+
</li>
51+
{% endfor %}
52+
</ul>
53+
{% if next_url %}
54+
<nav class="page-position page-position-footer" aria-label="Query pagination">
55+
<span class="page-position-text">Showing <strong>{{ queries|length }}</strong> of <strong>{{ "{:,}".format(total) }}</strong></span>
56+
<span class="page-position-links"><a class="page-position-next" href="{{ next_url }}" rel="next">
57+
Next
58+
<svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 18 6-6-6-6"/></svg>
59+
</a></span>
60+
</nav>
61+
{% endif %}
62+
{% elif filters.q %}
63+
<p class="homepage-empty">No queries match “{{ filters.q }}”. <a href="{{ query_list_path }}">Clear search</a></p>
64+
{% else %}
65+
<p class="homepage-empty">No queries found.</p>
66+
{% endif %}
67+
</section>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p class="homepage-summary">
2+
<span>{{ "{:,}".format(total) }} quer{% if total != 1 %}ies{% else %}y{% endif %}</span>
3+
{%- if filters.q %}<span>matching “{{ filters.q }}”</span>{% endif -%}
4+
</p>

0 commit comments

Comments
 (0)