-
Notifications
You must be signed in to change notification settings - Fork 8
Add sort options to main view #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StanFromIreland
wants to merge
7
commits into
main
Choose a base branch
from
sort-things
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ef70b0
Commit
StanFromIreland 9917d5a
Merge origin/main and resolve template conflicts
Copilot 10a3a66
Merge origin/main and resolve index template conflict
Copilot bfed9cc
Harden card column selection and reordering
Copilot 6172356
Improve pin controls accessibility and resilience
Copilot f370720
Fix field width wrapping
m-aciek fdc3d67
Add sorting by core completion, progress and audience
m-aciek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest dropping this all for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from collections.abc import Iterable | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def get_audience( | ||
| stats_dir: Path, | ||
| language_codes: Iterable[str], | ||
| snapshots: int = 30, | ||
| *, | ||
| required: bool = False, | ||
| ) -> dict[str, int]: | ||
| """Return a rolling audience score for each language. | ||
|
|
||
| Plausible's public exports contain one visitor count per hour, so the score | ||
| is the sum of those counts across the most recent available snapshots. | ||
| """ | ||
| if snapshots < 1: | ||
| raise ValueError('snapshots must be at least 1') | ||
|
|
||
| audience = dict.fromkeys(language_codes, 0) | ||
| if not stats_dir.is_dir(): | ||
| message = f'Plausible statistics directory not found: {stats_dir}' | ||
| if required: | ||
| raise FileNotFoundError(message) | ||
| logging.warning(message) | ||
| return audience | ||
|
|
||
| snapshot_dirs = sorted( | ||
| (path for path in stats_dir.glob('docs.python.org_*') if path.is_dir()), | ||
| key=lambda path: path.name, | ||
| reverse=True, | ||
| ) | ||
| if not snapshot_dirs: | ||
| message = f'No Plausible statistics snapshots found in {stats_dir}' | ||
| if required: | ||
| raise FileNotFoundError(message) | ||
| logging.warning(message) | ||
| return audience | ||
|
|
||
| snapshots_read = dict.fromkeys(audience, 0) | ||
| files_read = 0 | ||
| for snapshot_dir in snapshot_dirs: | ||
| for language_code in audience: | ||
| if snapshots_read[language_code] >= snapshots: | ||
| continue | ||
| visitors_file = snapshot_dir / ( | ||
| f'{snapshot_dir.name}.prefix-{language_code}.visitors.json' | ||
| ) | ||
| if not visitors_file.exists(): | ||
| continue | ||
|
|
||
| rows = json.loads(visitors_file.read_text()) | ||
| audience[language_code] += sum(int(row['visitors']) for row in rows) | ||
| snapshots_read[language_code] += 1 | ||
| files_read += 1 | ||
|
|
||
| if required and not files_read: | ||
| raise FileNotFoundError(f'No Plausible visitor statistics found in {stats_dir}') | ||
|
|
||
| return audience |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,32 @@ | ||
| {% extends "base.html.jinja" %} | ||
| {% block main %} | ||
| <div> | ||
| <div class="container-fluid mb-3"> | ||
| <div class="row justify-content-center"> | ||
| <div class="col-auto"> | ||
| <div class="d-flex align-items-center gap-2"> | ||
| <label for="sortDropdown" class="fw-medium text-nowrap flex-shrink-0">Sort by:</label> | ||
| <select id="sortDropdown" | ||
| class="form-select form-select-sm w-auto" | ||
| aria-label="Sort languages"> | ||
| <option value="completion">Completion</option> | ||
| <option value="core-completion">Core completion</option> | ||
| <option value="progress">Progress</option> | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear what "Progress" is, maybe something more like "Recent changes"? |
||
| <option value="audience">Audience</option> | ||
| <option value="name">Name</option> | ||
| </select> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <div id="languageContainer"> | ||
| <div class="row"> | ||
| {% for project in completion_progress | sort(attribute='core_completion,completion') | reverse %} | ||
| <div class="col-12 col-sm-6 col-md-4 col-xxl-3 d-flex"> | ||
| {% for project in completion_progress | sort(attribute='completion') | reverse %} | ||
| <div class="col-12 col-sm-6 col-md-4 col-xxl-3 d-flex" | ||
| data-completion="{{ project.completion }}" | ||
| data-core-completion="{{ project.core_completion }}" | ||
| data-progress="{{ project.change }}" | ||
| data-audience="{{ project.audience }}" | ||
| data-name="{{ project.language.name }}"> | ||
| <div id="{{ project.language.code }}" class="card shadow mb-3 w-100"> | ||
| <div class="card-body"> | ||
| <h2 class="card-title h3"> | ||
|
|
@@ -63,26 +86,69 @@ | |
| }); | ||
| } | ||
|
|
||
| function sortLanguages(sortBy) { | ||
| const languageContainer = document.getElementById('languageContainer'); | ||
| if (!languageContainer) { | ||
| return; | ||
| } | ||
| const row = languageContainer.querySelector('.row'); | ||
| if (!row) { | ||
| return; | ||
| } | ||
|
|
||
| const sortFields = { | ||
| 'completion': 'completion', | ||
| 'core-completion': 'coreCompletion', | ||
| 'progress': 'progress', | ||
| 'audience': 'audience', | ||
| }; | ||
| const cards = Array.from(row.children); | ||
| cards.sort((a, b) => { | ||
| const aPinPriority = a.dataset.pinPriority; | ||
| const bPinPriority = b.dataset.pinPriority; | ||
| if (aPinPriority !== undefined || bPinPriority !== undefined) { | ||
| if (aPinPriority === undefined) return 1; | ||
| if (bPinPriority === undefined) return -1; | ||
| return Number(aPinPriority) - Number(bPinPriority); | ||
| } | ||
|
|
||
| const nameComparison = a.dataset.name.localeCompare(b.dataset.name); | ||
| if (sortBy === 'name') { | ||
| return nameComparison; | ||
| } | ||
| const sortField = sortFields[sortBy] || sortFields.completion; | ||
| return Number(b.dataset[sortField]) - Number(a.dataset[sortField]) || nameComparison; | ||
| }); | ||
|
|
||
| row.innerHTML = ''; | ||
| cards.forEach(card => row.appendChild(card)); | ||
| } | ||
|
|
||
| updateProgressBarVisibility(); | ||
|
|
||
| const sortDropdown = document.getElementById('sortDropdown'); | ||
| if (sortDropdown) { | ||
| sortDropdown.addEventListener('change', function() { | ||
| sortLanguages(this.value); | ||
| }); | ||
| sortLanguages(sortDropdown.value); | ||
| } | ||
|
|
||
| window.addEventListener('resize', updateProgressBarVisibility); | ||
|
|
||
|
|
||
| (function () { | ||
| const userLangs = Array.from(navigator.languages || []).map(lang => lang.toLowerCase()); | ||
| const row = document.querySelector('.row'); | ||
| const row = document.querySelector('#languageContainer > .row'); | ||
| if (!row || !userLangs.length) return; | ||
|
|
||
| // Capture the original column order (completion-based sort from server) before any changes. | ||
| const originalCols = Array.from(row.children); | ||
|
|
||
| // Find the first matching card column for each user language preference. | ||
| const userLangToCol = new Map(); | ||
| for (const lang of userLangs) { | ||
| const langBase = lang.split('-')[0]; | ||
| const card = row.querySelector(`[id="${lang}"]`) || row.querySelector(`[id="${langBase}"]`); | ||
| if (card) { | ||
| const col = card.closest('.col-12'); | ||
| const col = card.parentElement; | ||
| if (col && col.parentElement === row) { | ||
| userLangToCol.set(lang, col); | ||
| } | ||
|
|
@@ -106,7 +172,10 @@ | |
| // localStorage helpers – store the set of explicitly unpinned card IDs. | ||
| const LS_KEY = 'dashboard-unpinned'; | ||
| function getUnpinned() { | ||
| try { return new Set(JSON.parse(localStorage.getItem(LS_KEY) || '[]')); } | ||
| try { | ||
| const value = JSON.parse(localStorage.getItem(LS_KEY) || '[]'); | ||
| return Array.isArray(value) ? new Set(value) : new Set(); | ||
| } | ||
| catch { return new Set(); } | ||
| } | ||
| function saveUnpinned(set) { | ||
|
|
@@ -128,8 +197,10 @@ | |
| for (const { cardId, col } of uniqueUserCols) { | ||
| const card = col.querySelector('.card'); | ||
| const btn = document.createElement('button'); | ||
| const languageName = card.querySelector('.card-title a')?.textContent?.trim() || cardId; | ||
| btn.className = 'pin-btn'; | ||
| btn.dataset.cardId = cardId; | ||
| btn.dataset.languageName = languageName; | ||
| btn.innerHTML = `<span class="pin-icon-pinned">${PIN_FILL}</span><span class="pin-icon-unpinned">${PIN_OUTLINE}</span>`; | ||
| card.appendChild(btn); | ||
|
|
||
|
|
@@ -141,34 +212,36 @@ | |
| unpinned.add(cardId); | ||
| } | ||
| saveUnpinned(unpinned); | ||
| reorder(); | ||
| reorder(unpinned); | ||
| }); | ||
| } | ||
|
|
||
| function reorder() { | ||
| const unpinned = getUnpinned(); | ||
| function reorder(unpinned = getUnpinned()) { | ||
|
|
||
| // Pinned user-language columns in navigator.languages priority order. | ||
| const pinnedCols = uniqueUserCols | ||
| .filter(({ cardId }) => !unpinned.has(cardId)) | ||
| .map(({ col }) => col); | ||
|
|
||
| // Remaining columns in original server-side sort order. | ||
| const pinnedSet = new Set(pinnedCols); | ||
| const restCols = originalCols.filter(col => !pinnedSet.has(col)); | ||
|
|
||
| [...pinnedCols, ...restCols].forEach(col => row.appendChild(col)); | ||
| for (const col of row.children) { | ||
| delete col.dataset.pinPriority; | ||
| } | ||
| pinnedCols.forEach((col, index) => { | ||
| col.dataset.pinPriority = index; | ||
| }); | ||
|
|
||
| // Sync button appearance and accessible label. | ||
| for (const { cardId, col } of uniqueUserCols) { | ||
| const btn = col.querySelector('.pin-btn'); | ||
| if (btn) { | ||
| const isPinned = !unpinned.has(cardId); | ||
| const languageName = btn.dataset.languageName || cardId; | ||
| btn.classList.toggle('unpinned', !isPinned); | ||
| btn.title = isPinned ? 'Unpin' : 'Pin'; | ||
| btn.setAttribute('aria-label', isPinned ? 'Unpin this language card' : 'Pin this language card'); | ||
| btn.title = isPinned ? `Unpin ${languageName}` : `Pin ${languageName}`; | ||
| btn.setAttribute('aria-label', isPinned ? `Unpin ${languageName} language card` : `Pin ${languageName} language card`); | ||
| } | ||
| } | ||
|
|
||
| sortLanguages(sortDropdown?.value || 'completion'); | ||
| } | ||
|
|
||
| reorder(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of this statistic, I'm not sure it adds much useful information. I suggest removing it so that we can land this, and we can debate it in a follow up.