mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-20 20:07:59 +00:00
feat(dashboard): display owned repositories with pagination
This commit is contained in:
parent
9d0e0b481a
commit
9f8c7fdfd0
2 changed files with 123 additions and 4 deletions
|
|
@ -4,7 +4,7 @@ from contextlib import asynccontextmanager
|
|||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, FastAPI, Request
|
||||
from fastapi import Depends, FastAPI, HTTPException, Query, Request
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy import func as sql_func
|
||||
|
|
@ -15,12 +15,14 @@ from starlette.responses import Response
|
|||
|
||||
from app import models as _models # noqa: F401 - register ORM tables on metadata
|
||||
from app.auth.session import get_current_session, get_optional_session
|
||||
from app.clients import forgejo
|
||||
from app.database import Base
|
||||
from app.deps import get_db
|
||||
from app.middleware import SetupRequiredMiddleware
|
||||
from app.models.app_settings import AppSettings
|
||||
from app.models.session import Session as UserBrowserSession
|
||||
from app.routers import auth, links, repositories, settings, setup
|
||||
from app.settings import get_settings
|
||||
from app.settings import get_app_settings, get_settings
|
||||
from app.templating import templates
|
||||
|
||||
|
||||
|
|
@ -137,15 +139,51 @@ async def root(
|
|||
)
|
||||
|
||||
|
||||
REPOS_PER_PAGE = 30
|
||||
|
||||
|
||||
@app.get("/dashboard", response_class=HTMLResponse)
|
||||
async def dashboard(
|
||||
request: Request, sess: Annotated[UserBrowserSession, Depends(get_current_session)]
|
||||
request: Request,
|
||||
sess: Annotated[UserBrowserSession, Depends(get_current_session)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
app_settings: Annotated[AppSettings, Depends(get_app_settings)],
|
||||
page: Annotated[int, Query(ge=1)] = 1,
|
||||
) -> Response:
|
||||
repos = []
|
||||
total_count = 0
|
||||
has_next_page = False
|
||||
repo_error = None
|
||||
if sess.user.has_pat:
|
||||
try:
|
||||
access_token = await repositories.get_forgejo_token(sess)
|
||||
owner = await repositories.get_forgejo_owner(sess, db)
|
||||
repo_page = await forgejo.get_repos(
|
||||
app_settings,
|
||||
access_token,
|
||||
limit=REPOS_PER_PAGE,
|
||||
page=page,
|
||||
owner=owner,
|
||||
)
|
||||
except HTTPException as exc:
|
||||
if exc.status_code not in {403, 502, 503}:
|
||||
raise
|
||||
repo_error = exc.detail
|
||||
else:
|
||||
repos = repo_page.repos
|
||||
total_count = repo_page.total_count or 0
|
||||
has_next_page = repo_page.has_next_page
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"dashboard.html",
|
||||
{
|
||||
"user": sess.user,
|
||||
"pat_registered": sess.user.has_pat,
|
||||
"repos": repos,
|
||||
"repo_error": repo_error,
|
||||
"repo_page": page,
|
||||
"repo_total_count": total_count,
|
||||
"repo_has_previous_page": page > 1,
|
||||
"repo_has_next_page": has_next_page,
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@
|
|||
<th class="px-4 py-3">Expires</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-border-subtle">
|
||||
<tbody class="divide-y divide-border">
|
||||
<tr class="text-muted-foreground text-center">
|
||||
<td class="px-4 py-5" colspan="5">You have not created any share links yet.</td>
|
||||
</tr>
|
||||
|
|
@ -66,5 +66,86 @@
|
|||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<div class="space-y-3">
|
||||
<div class="flex flex-col justify-between gap-2 sm:flex-row sm:items-end">
|
||||
<div>
|
||||
<h2 class="text-base font-semibold">Repositories</h2>
|
||||
<p class="mt-2 text-sm text-muted-foreground">Repositories owned by your Forgejo account.</p>
|
||||
</div>
|
||||
{% if pat_registered and repo_total_count %}
|
||||
<p class="text-xs text-muted-foreground">{{ repo_total_count }} total</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
<section class="card">
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full min-w-180 text-left text-sm">
|
||||
<thead class="bg-muted text-xs font-semibold uppercase tracking-[0.05em] text-muted-foreground">
|
||||
<tr>
|
||||
<th class="px-4 py-3">Repository</th>
|
||||
<th class="px-4 py-3">Default branch</th>
|
||||
<th class="px-4 py-3">Visibility</th>
|
||||
<th class="px-4 py-3">Updated</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-border">
|
||||
{% if not pat_registered %}
|
||||
<tr class="text-muted-foreground text-center">
|
||||
<td class="px-4 py-5" colspan="4">Register a Forgejo access token to view your repositories.</td>
|
||||
</tr>
|
||||
{% elif repo_error %}
|
||||
<tr class="text-center text-warning-foreground">
|
||||
<td class="px-4 py-5" colspan="4" role="alert">{{ repo_error }}</td>
|
||||
</tr>
|
||||
{% elif repos %}
|
||||
{% for repo in repos %}
|
||||
<tr class="hover:bg-muted/40">
|
||||
<td class="px-4 py-3">
|
||||
<a href="{{ repo.html_url }}"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="font-medium text-foreground hover:underline">{{ repo.repo_name }}</a>
|
||||
{% if repo.description %}
|
||||
<p class="mt-1 max-w-xl truncate text-xs text-muted-foreground">{{ repo.description }}</p>
|
||||
{% endif %}
|
||||
{% if repo.archived %}
|
||||
<span class="badge-warning mt-1 inline-flex">Archived</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td class="px-4 py-3 font-mono text-xs">{{ repo.default_branch }}</td>
|
||||
<td class="px-4 py-3">
|
||||
{% if repo.visibility == "public" %}
|
||||
<span class="badge-success">Public</span>
|
||||
{% else %}
|
||||
<span class="badge bg-muted text-muted-foreground">Private</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td class="px-4 py-3 whitespace-nowrap text-xs text-muted-foreground">{{ repo.updated_at.strftime("%b %d, %Y") }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr class="text-muted-foreground text-center">
|
||||
<td class="px-4 py-5" colspan="4">No owned repositories found on this page.</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% if pat_registered and not repo_error and (repo_has_previous_page or repo_has_next_page) %}
|
||||
<nav class="flex items-center justify-between border-t border-border px-4 py-3" aria-label="Repository pages">
|
||||
{% if repo_has_previous_page %}
|
||||
<a href="/dashboard?page={{ repo_page - 1 }}" class="btn btn-sm btn-secondary">Previous</a>
|
||||
{% else %}
|
||||
<span></span>
|
||||
{% endif %}
|
||||
<span class="text-xs text-muted-foreground">Page {{ repo_page }}</span>
|
||||
{% if repo_has_next_page %}
|
||||
<a href="/dashboard?page={{ repo_page + 1 }}" class="btn btn-sm btn-secondary">Next</a>
|
||||
{% else %}
|
||||
<span></span>
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endif %}
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue