mirror of
1
Fork 0

Fix the settings tab highlighting

When visiting a repos `/settings/units` page, highlight the active tab
properly: "Add more..." if the tab is displayed, or "Settings"
otherwise.

Fixes #3188.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 65ed86e396)
This commit is contained in:
Gergely Nagy 2024-04-12 19:11:21 +02:00 committed by GitHub
parent 89d751ef1b
commit fd5abfd244
2 changed files with 96 additions and 1 deletions

View File

@ -174,12 +174,14 @@
{{end}} {{end}}
{{if .Permission.IsAdmin}} {{if .Permission.IsAdmin}}
{{$highlightSettings := true}}
{{if and .SignedUser.EnableRepoUnitHints (not (.Repository.AllUnitsEnabled ctx))}} {{if and .SignedUser.EnableRepoUnitHints (not (.Repository.AllUnitsEnabled ctx))}}
{{$highlightSettings = false}}
<a class="{{if .PageIsRepoSettingsUnits}}active {{end}}item" href="{{.RepoLink}}/settings/units"> <a class="{{if .PageIsRepoSettingsUnits}}active {{end}}item" href="{{.RepoLink}}/settings/units">
{{svg "octicon-diff-added"}} {{ctx.Locale.Tr "repo.settings.units.add_more"}} {{svg "octicon-diff-added"}} {{ctx.Locale.Tr "repo.settings.units.add_more"}}
</a> </a>
{{end}} {{end}}
<a class="{{if and .PageIsRepoSettings (not .PageIsRepoSettingsUnits)}}active {{end}} item" href="{{.RepoLink}}/settings"> <a class="{{if and .PageIsRepoSettings (or $highlightSettings (not .PageIsRepoSettingsUnits))}}active {{end}} item" href="{{.RepoLink}}/settings">
{{svg "octicon-tools"}} {{ctx.Locale.Tr "repo.settings"}} {{svg "octicon-tools"}} {{ctx.Locale.Tr "repo.settings"}}
</a> </a>
{{end}} {{end}}

View File

@ -14,9 +14,11 @@ import (
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
gitea_context "code.gitea.io/gitea/services/context" gitea_context "code.gitea.io/gitea/services/context"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
user_service "code.gitea.io/gitea/services/user"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -32,6 +34,97 @@ func TestRepoSettingsUnits(t *testing.T) {
session.MakeRequest(t, req, http.StatusOK) session.MakeRequest(t, req, http.StatusOK)
} }
func TestRepoAddMoreUnitsHighlighting(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
session := loginUser(t, user.Name)
// Make sure there are no disabled repos in the settings!
setting.Repository.DisabledRepoUnits = []string{}
unit_model.LoadUnitConfig()
// Create a known-good repo, with some units disabled.
repo, _, f := CreateDeclarativeRepo(t, user, "", []unit_model.Type{
unit_model.TypeCode,
unit_model.TypePullRequests,
unit_model.TypeProjects,
unit_model.TypeActions,
unit_model.TypeIssues,
unit_model.TypeWiki,
}, []unit_model.Type{unit_model.TypePackages}, nil)
defer f()
setUserHints := func(t *testing.T, hints bool) func() {
saved := user.EnableRepoUnitHints
assert.NoError(t, user_service.UpdateUser(db.DefaultContext, user, &user_service.UpdateOptions{
EnableRepoUnitHints: optional.Some(hints),
}))
return func() {
assert.NoError(t, user_service.UpdateUser(db.DefaultContext, user, &user_service.UpdateOptions{
EnableRepoUnitHints: optional.Some(saved),
}))
}
}
assertHighlight := func(t *testing.T, page, uri string, highlighted bool) {
t.Helper()
req := NewRequest(t, "GET", fmt.Sprintf("%s/settings%s", repo.Link(), page))
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, fmt.Sprintf(".overflow-menu-items a[href='%s'].active", fmt.Sprintf("%s/settings%s", repo.Link(), uri)), highlighted)
}
t.Run("hints enabled", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
defer setUserHints(t, true)()
t.Run("settings", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
// Visiting the /settings page, "Settings" is highlighted
assertHighlight(t, "", "", true)
// ...but "Add more" isn't.
assertHighlight(t, "", "/units", false)
})
t.Run("units", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
// Visiting the /settings/units page, "Add more" is highlighted
assertHighlight(t, "/units", "/units", true)
// ...but "Settings" isn't.
assertHighlight(t, "/units", "", false)
})
})
t.Run("hints disabled", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
defer setUserHints(t, false)()
t.Run("settings", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
// Visiting the /settings page, "Settings" is highlighted
assertHighlight(t, "", "", true)
// ...but "Add more" isn't (it doesn't exist).
assertHighlight(t, "", "/units", false)
})
t.Run("units", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
// Visiting the /settings/units page, "Settings" is highlighted
assertHighlight(t, "/units", "", true)
// ...but "Add more" isn't (it doesn't exist)
assertHighlight(t, "/units", "/units", false)
})
})
}
func TestRepoAddMoreUnits(t *testing.T) { func TestRepoAddMoreUnits(t *testing.T) {
defer tests.PrepareTestEnv(t)() defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})