Skip to content

Commit 1e29bcc

Browse files
yardenshohamwxiaoguang
authored andcommitted
Fix elipsis button not working if the last commit loading is deferred (#29544)
Before this change, if we had more than 200 entries being deferred in loading, the entire table would get replaced thus losing any event listeners attached to the elements within the table, such as the elipsis button and commit list with tippy. With this change we remove the previous javascript code that replaced the table and use htmx to replace the table. htmx attributes added: - `hx-indicator="tr.notready td.message span"`: attach the loading spinner to the files whose last commit is still being loaded - `hx-trigger="load"` trigger the request-replace behavior as soon as possible - `hx-swap="morph"`: use the idiomorph morphing algorithm, this is the thing that makes it so the elipsis button event listener is kept during the replacement, fixing the bug because we don't actually replace the table, only modifying it - `hx-post="{{.LastCommitLoaderURL}}"`: make a post request to this url to get the table with all of the commit information As part of this change I removed the handling of partial replacement in the case we have less than 200 "not ready" files. The first reason is that I couldn't make htmx replace only a subset of returned elements, the second reason is that we have a cache implemented in the backend already so the only cost added is that we query the cache a few times (which is sure to be populated due to the initial request), and the last reason is that since the last refactor of this functionality that removed jQuery we don't properly send the "not ready" entries as the backend expects `FormData` with `f[]` and we send a JSON with `f` so we always query for all rows anyway. # Before ![before](https://github.com/go-gitea/gitea/assets/20454870/482ebfec-66c5-40cc-9c1e-e3b3bfe1bbc1) # After ![after](https://github.com/go-gitea/gitea/assets/20454870/454c517e-3a4e-4006-a49f-99cc56e0fd60) --------- Signed-off-by: Yarden Shoham <git@yardenshoham.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> (cherry picked from commit 937e8b55149388840bbf6c4d7216495bc3dd2fe9)
1 parent 12441d7 commit 1e29bcc

4 files changed

Lines changed: 12 additions & 68 deletions

File tree

routers/web/repo/view.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"code.gitea.io/gitea/modules/actions"
3636
"code.gitea.io/gitea/modules/base"
3737
"code.gitea.io/gitea/modules/charset"
38-
"code.gitea.io/gitea/modules/container"
3938
"code.gitea.io/gitea/modules/git"
4039
"code.gitea.io/gitea/modules/highlight"
4140
"code.gitea.io/gitea/modules/lfs"
@@ -881,25 +880,18 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
881880
defer cancel()
882881
}
883882

884-
selected := make(container.Set[string])
885-
selected.AddMultiple(ctx.FormStrings("f[]")...)
886-
887-
entries := allEntries
888-
if len(selected) > 0 {
889-
entries = make(git.Entries, 0, len(selected))
890-
for _, entry := range allEntries {
891-
if selected.Contains(entry.Name()) {
892-
entries = append(entries, entry)
893-
}
894-
}
895-
}
896-
897-
var latestCommit *git.Commit
898-
ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
883+
files, latestCommit, err := allEntries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
899884
if err != nil {
900885
ctx.ServerError("GetCommitsInfo", err)
901886
return nil
902887
}
888+
ctx.Data["Files"] = files
889+
for _, f := range files {
890+
if f.Commit == nil {
891+
ctx.Data["HasFilesWithoutLatestCommit"] = true
892+
break
893+
}
894+
}
903895

904896
if !loadLatestCommitData(ctx, latestCommit) {
905897
return nil

templates/repo/view_list.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<table id="repo-files-table" class="ui single line table gt-mt-0" data-last-commit-loader-url="{{.LastCommitLoaderURL}}">
1+
<table id="repo-files-table" class="ui single line table gt-mt-0" {{if .HasFilesWithoutLatestCommit}}hx-indicator="tr.notready td.message span" hx-trigger="load" hx-swap="morph" hx-post="{{.LastCommitLoaderURL}}"{{end}}>
22
<thead>
33
<tr class="commit-list">
4-
<th colspan="2" {{if not .LatestCommit}}class="notready"{{end}}>
4+
<th colspan="2">
55
{{template "repo/latest_commit" .}}
66
</th>
77
<th class="text grey right age">{{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When ctx.Locale}}{{end}}{{end}}</th>
@@ -55,7 +55,7 @@
5555
{{$commitLink := printf "%s/commit/%s" $.RepoLink (PathEscape $commit.ID.String)}}
5656
{{RenderCommitMessageLinkSubject $.Context $commit.Message $commitLink ($.Repository.ComposeMetas ctx)}}
5757
{{else}}
58-
<div class="ui active tiny slow centered inline"></div>
58+
<div class="ui active tiny slow centered inline"></div>
5959
{{end}}
6060
</span>
6161
</td>

web_src/js/features/repo-commit.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {createTippy} from '../modules/tippy.js';
22
import {toggleElem} from '../utils/dom.js';
3-
import {parseDom} from '../utils.js';
4-
import {POST} from '../modules/fetch.js';
53

64
export function initRepoEllipsisButton() {
75
for (const button of document.querySelectorAll('.js-toggle-commit-body')) {
@@ -14,47 +12,6 @@ export function initRepoEllipsisButton() {
1412
}
1513
}
1614

17-
export async function initRepoCommitLastCommitLoader() {
18-
const entryMap = {};
19-
20-
const entries = Array.from(document.querySelectorAll('table#repo-files-table tr.notready'), (el) => {
21-
const entryName = el.getAttribute('data-entryname');
22-
entryMap[entryName] = el;
23-
return entryName;
24-
});
25-
26-
if (entries.length === 0) {
27-
return;
28-
}
29-
30-
const lastCommitLoaderURL = document.querySelector('table#repo-files-table').getAttribute('data-last-commit-loader-url');
31-
32-
if (entries.length > 200) {
33-
// For more than 200 entries, replace the entire table
34-
const response = await POST(lastCommitLoaderURL);
35-
const data = await response.text();
36-
document.querySelector('table#repo-files-table').outerHTML = data;
37-
return;
38-
}
39-
40-
// For fewer entries, update individual rows
41-
const response = await POST(lastCommitLoaderURL, {data: {'f': entries}});
42-
const data = await response.text();
43-
const doc = parseDom(data, 'text/html');
44-
for (const row of doc.querySelectorAll('tr')) {
45-
if (row.className === 'commit-list') {
46-
document.querySelector('table#repo-files-table .commit-list')?.replaceWith(row);
47-
continue;
48-
}
49-
// there are other <tr> rows in response (eg: <tr class="has-parent">)
50-
// at the moment only the "data-entryname" rows should be processed
51-
const entryName = row.getAttribute('data-entryname');
52-
if (entryName) {
53-
entryMap[entryName]?.replaceWith(row);
54-
}
55-
}
56-
}
57-
5815
export function initCommitStatuses() {
5916
for (const element of document.querySelectorAll('[data-tippy="commit-statuses"]')) {
6017
const top = document.querySelector('.repository.file.list') || document.querySelector('.repository.diff');

web_src/js/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ import {
3333
initRepoPullRequestAllowMaintainerEdit,
3434
initRepoPullRequestReview, initRepoIssueSidebarList, initArchivedLabelHandler,
3535
} from './features/repo-issue.js';
36-
import {
37-
initRepoEllipsisButton,
38-
initRepoCommitLastCommitLoader,
39-
initCommitStatuses,
40-
} from './features/repo-commit.js';
36+
import {initRepoEllipsisButton, initCommitStatuses} from './features/repo-commit.js';
4137
import {
4238
initFootLanguageMenu,
4339
initGlobalButtonClickOnEnter,
@@ -148,7 +144,6 @@ onDomReady(() => {
148144
initRepoCommentForm();
149145
initRepoEllipsisButton();
150146
initRepoDiffCommitBranchesAndTags();
151-
initRepoCommitLastCommitLoader();
152147
initRepoEditor();
153148
initRepoGraphGit();
154149
initRepoIssueContentHistory();

0 commit comments

Comments
 (0)