Only use prev and next buttons for pagination on user dashboard (#33981)

The pagination on the user dashboard sounds unnecessary, this will
change it to a prev/next buttons. For instances with around `10 million`
records in the action table, this option affects how the user dashboard
is loaded on first visit.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
Lunny Xiao
2025-03-23 12:52:43 -07:00
committed by GitHub
parent 189873719b
commit 30ee082e48
7 changed files with 65 additions and 42 deletions

View File

@ -4,6 +4,8 @@
package paginator package paginator
import "code.gitea.io/gitea/modules/util"
/* /*
In template: In template:
@ -32,25 +34,43 @@ Output:
// Paginator represents a set of results of pagination calculations. // Paginator represents a set of results of pagination calculations.
type Paginator struct { type Paginator struct {
total int // total rows count total int // total rows count, -1 means unknown
pagingNum int // how many rows in one page totalPages int // total pages count, -1 means unknown
current int // current page number current int // current page number
curRows int // current page rows count
pagingNum int // how many rows in one page
numPages int // how many pages to show on the UI numPages int // how many pages to show on the UI
} }
// New initialize a new pagination calculation and returns a Paginator as result. // New initialize a new pagination calculation and returns a Paginator as result.
func New(total, pagingNum, current, numPages int) *Paginator { func New(total, pagingNum, current, numPages int) *Paginator {
if pagingNum <= 0 { pagingNum = max(pagingNum, 1)
pagingNum = 1 totalPages := util.Iif(total == -1, -1, (total+pagingNum-1)/pagingNum)
if total >= 0 {
current = min(current, totalPages)
} }
if current <= 0 { current = max(current, 1)
current = 1 return &Paginator{
total: total,
totalPages: totalPages,
current: current,
pagingNum: pagingNum,
numPages: numPages,
} }
p := &Paginator{total, pagingNum, current, numPages} }
if p.current > p.TotalPages() {
p.current = p.TotalPages() func (p *Paginator) SetCurRows(rows int) {
// For "unlimited paging", we need to know the rows of current page to determine if there is a next page.
// There is still an edge case: when curRows==pagingNum, then the "next page" will be an empty page.
// Ideally we should query one more row to determine if there is really a next page, but it's impossible in current framework.
p.curRows = rows
if p.total == -1 && p.current == 1 && !p.HasNext() {
// if there is only one page for the "unlimited paging", set total rows/pages count
// then the tmpl could decide to hide the nav bar.
p.total = rows
p.totalPages = util.Iif(p.total == 0, 0, 1)
} }
return p
} }
// IsFirst returns true if current page is the first page. // IsFirst returns true if current page is the first page.
@ -72,7 +92,10 @@ func (p *Paginator) Previous() int {
// HasNext returns true if there is a next page relative to current page. // HasNext returns true if there is a next page relative to current page.
func (p *Paginator) HasNext() bool { func (p *Paginator) HasNext() bool {
return p.total > p.current*p.pagingNum if p.total == -1 {
return p.curRows >= p.pagingNum
}
return p.current*p.pagingNum < p.total
} }
func (p *Paginator) Next() int { func (p *Paginator) Next() int {
@ -84,10 +107,7 @@ func (p *Paginator) Next() int {
// IsLast returns true if current page is the last page. // IsLast returns true if current page is the last page.
func (p *Paginator) IsLast() bool { func (p *Paginator) IsLast() bool {
if p.total == 0 { return !p.HasNext()
return true
}
return p.total > (p.current-1)*p.pagingNum && !p.HasNext()
} }
// Total returns number of total rows. // Total returns number of total rows.
@ -97,10 +117,7 @@ func (p *Paginator) Total() int {
// TotalPages returns number of total pages. // TotalPages returns number of total pages.
func (p *Paginator) TotalPages() int { func (p *Paginator) TotalPages() int {
if p.total == 0 { return p.totalPages
return 1
}
return (p.total + p.pagingNum - 1) / p.pagingNum
} }
// Current returns current page number. // Current returns current page number.
@ -135,10 +152,10 @@ func getMiddleIdx(numPages int) int {
// If value is -1 means "..." that more pages are not showing. // If value is -1 means "..." that more pages are not showing.
func (p *Paginator) Pages() []*Page { func (p *Paginator) Pages() []*Page {
if p.numPages == 0 { if p.numPages == 0 {
return []*Page{} return nil
} else if p.numPages == 1 && p.TotalPages() == 1 { } else if p.total == -1 || (p.numPages == 1 && p.TotalPages() == 1) {
// Only show current page. // Only show current page.
return []*Page{{1, true}} return []*Page{{p.current, true}}
} }
// Total page number is less or equal. // Total page number is less or equal.

View File

@ -76,9 +76,7 @@ func TestPaginator(t *testing.T) {
t.Run("Only current page", func(t *testing.T) { t.Run("Only current page", func(t *testing.T) {
p := New(0, 10, 1, 1) p := New(0, 10, 1, 1)
pages := p.Pages() pages := p.Pages()
assert.Len(t, pages, 1) assert.Empty(t, pages) // no "total", so no pages
assert.Equal(t, 1, pages[0].Num())
assert.True(t, pages[0].IsCurrent())
p = New(1, 10, 1, 1) p = New(1, 10, 1, 1)
pages = p.Pages() pages = p.Pages()

View File

@ -137,11 +137,10 @@ func Dashboard(ctx *context.Context) {
return return
} }
ctx.Data["Feeds"] = feeds pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5).WithCurRows(len(feeds))
pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5)
pager.AddParamFromRequest(ctx.Req) pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.Data["Feeds"] = feeds
ctx.HTML(http.StatusOK, tplDashboard) ctx.HTML(http.StatusOK, tplDashboard)
} }

View File

@ -21,12 +21,18 @@ type Pagination struct {
// NewPagination creates a new instance of the Pagination struct. // NewPagination creates a new instance of the Pagination struct.
// "pagingNum" is "page size" or "limit", "current" is "page" // "pagingNum" is "page size" or "limit", "current" is "page"
// total=-1 means only showing prev/next
func NewPagination(total, pagingNum, current, numPages int) *Pagination { func NewPagination(total, pagingNum, current, numPages int) *Pagination {
p := &Pagination{} p := &Pagination{}
p.Paginater = paginator.New(total, pagingNum, current, numPages) p.Paginater = paginator.New(total, pagingNum, current, numPages)
return p return p
} }
func (p *Pagination) WithCurRows(n int) *Pagination {
p.Paginater.SetCurRows(n)
return p
}
func (p *Pagination) AddParamFromRequest(req *http.Request) { func (p *Pagination) AddParamFromRequest(req *http.Request) {
for key, values := range req.URL.Query() { for key, values := range req.URL.Query() {
if key == "page" || len(values) == 0 || (len(values) == 1 && values[0] == "") { if key == "page" || len(values) == 0 || (len(values) == 1 && values[0] == "") {

View File

@ -15,24 +15,17 @@ import (
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
) )
func userFeedCacheKey(userID int64) string { func userFeedCacheKey(userID int64) string {
return fmt.Sprintf("user_feed_%d", userID) return fmt.Sprintf("user_feed_%d", userID)
} }
func GetFeedsForDashboard(ctx context.Context, opts activities_model.GetFeedsOptions) (activities_model.ActionList, int64, error) { func GetFeedsForDashboard(ctx context.Context, opts activities_model.GetFeedsOptions) (activities_model.ActionList, int, error) {
opts.DontCount = opts.RequestedTeam == nil && opts.Date == "" opts.DontCount = opts.RequestedTeam == nil && opts.Date == ""
results, cnt, err := activities_model.GetFeeds(ctx, opts) results, cnt, err := activities_model.GetFeeds(ctx, opts)
if err != nil { return results, util.Iif(opts.DontCount, -1, int(cnt)), err
return nil, 0, err
}
if opts.DontCount {
cnt, err = cache.GetInt64(userFeedCacheKey(opts.Actor.ID), func() (int64, error) {
return activities_model.CountUserFeeds(ctx, opts.Actor.ID)
})
}
return results, cnt, err
} }
// GetFeeds returns actions according to the provided options // GetFeeds returns actions according to the provided options

View File

@ -2,32 +2,42 @@
{{$paginationLink := $.Link}} {{$paginationLink := $.Link}}
{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}} {{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}}
{{with .Page.Paginater}} {{with .Page.Paginater}}
{{if gt .TotalPages 1}} {{if or (eq .TotalPages -1) (gt .TotalPages 1)}}
{{$showFirstLast := gt .TotalPages 1}}
<div class="center page buttons"> <div class="center page buttons">
<div class="ui borderless pagination menu"> <div class="ui borderless pagination menu">
{{if $showFirstLast}}
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$paginationLink}}{{if $paginationParams}}?{{$paginationParams}}{{end}}"{{end}}> <a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$paginationLink}}{{if $paginationParams}}?{{$paginationParams}}{{end}}"{{end}}>
{{svg "gitea-double-chevron-left" 16 "tw-mr-1"}} {{svg "gitea-double-chevron-left" 16 "tw-mr-1"}}
<span class="navigation_label">{{ctx.Locale.Tr "admin.first_page"}}</span> <span class="navigation_label">{{ctx.Locale.Tr "admin.first_page"}}</span>
</a> </a>
{{end}}
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$paginationLink}}?page={{.Previous}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}> <a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$paginationLink}}?page={{.Previous}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
{{svg "octicon-chevron-left" 16 "tw-mr-1"}} {{svg "octicon-chevron-left" 16 "tw-mr-1"}}
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.previous"}}</span> <span class="navigation_label">{{ctx.Locale.Tr "repo.issues.previous"}}</span>
</a> </a>
{{range .Pages}} {{$pages := .Pages}}
{{$pagesLen := len $pages}}
{{range $pages}}
{{if eq .Num -1}} {{if eq .Num -1}}
<a class="disabled item">...</a> <a class="disabled item">...</a>
{{else}} {{else}}
<a class="{{if .IsCurrent}}active {{end}}item" {{if not .IsCurrent}}href="{{$paginationLink}}?page={{.Num}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>{{.Num}}</a> {{/* do not highlight the current page if there is only one page */}}
<a class="{{if and .IsCurrent (gt $pagesLen 1)}}active {{end}}item" {{if not .IsCurrent}}href="{{$paginationLink}}?page={{.Num}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>{{.Num}}</a>
{{end}} {{end}}
{{end}} {{end}}
<a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$paginationLink}}?page={{.Next}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}> <a class="{{if not .HasNext}}disabled{{end}} item navigation" {{if .HasNext}}href="{{$paginationLink}}?page={{.Next}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.next"}}</span> <span class="navigation_label">{{ctx.Locale.Tr "repo.issues.next"}}</span>
{{svg "octicon-chevron-right" 16 "tw-ml-1"}} {{svg "octicon-chevron-right" 16 "tw-ml-1"}}
</a> </a>
{{if $showFirstLast}}
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$paginationLink}}?page={{.TotalPages}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}> <a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$paginationLink}}?page={{.TotalPages}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
<span class="navigation_label">{{ctx.Locale.Tr "admin.last_page"}}</span> <span class="navigation_label">{{ctx.Locale.Tr "admin.last_page"}}</span>
{{svg "gitea-double-chevron-right" 16 "tw-ml-1"}} {{svg "gitea-double-chevron-right" 16 "tw-ml-1"}}
</a> </a>
{{end}}
</div> </div>
</div> </div>
{{end}} {{end}}

View File

@ -5,7 +5,7 @@
<div class="flex-container-main"> <div class="flex-container-main">
{{template "base/alert" .}} {{template "base/alert" .}}
{{template "user/heatmap" .}} {{template "user/heatmap" .}}
{{if .Feeds}} {{if .Page.Paginater.TotalPages}}
{{template "user/dashboard/feeds" .}} {{template "user/dashboard/feeds" .}}
{{else}} {{else}}
{{template "user/dashboard/guide" .}} {{template "user/dashboard/guide" .}}