From 11bd0eaad4f9067184647f59c8e0fd7f79b8b77f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 2 Jul 2025 17:06:57 -0700 Subject: [PATCH] improvements --- models/activities/notification.go | 18 +++++++++++++ models/issues/issue.go | 25 ++++++++++++++++++ .../markup/renderhelper_issueicontitle.go | 8 ++---- templates/repo/diff/compare.tmpl | 2 +- templates/repo/issue/card.tmpl | 2 +- templates/shared/issueicon.tmpl | 26 ------------------- templates/shared/issuelist.tmpl | 2 +- .../user/notification/notification_div.tmpl | 10 +------ web_src/js/features/issue.ts | 2 +- 9 files changed, 50 insertions(+), 45 deletions(-) delete mode 100644 templates/shared/issueicon.tmpl diff --git a/models/activities/notification.go b/models/activities/notification.go index 2ca1051ed3e..4537e8cd153 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -6,6 +6,7 @@ package activities import ( "context" "fmt" + "html/template" "net/url" "strconv" @@ -16,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/svg" "code.gitea.io/gitea/modules/timeutil" "xorm.io/builder" @@ -379,6 +381,22 @@ func (n *Notification) Link(ctx context.Context) string { return "" } +func (n *Notification) IconHTML(ctx context.Context) template.HTML { + switch n.Source { + case NotificationSourceIssue, NotificationSourcePullRequest: + // n.Issue should be loaded before calling this method + return n.Issue.IconHTML(ctx) + case NotificationSourceCommit: + return svg.RenderHTML("octicon-commit", 16, "text grey") + case NotificationSourceRepository: + return svg.RenderHTML("octicon-repo", 16, "text grey") + case NotificationSourceRelease: + return svg.RenderHTML("octicon-tag", 16, "text grey") + default: + return "" + } +} + // APIURL formats a URL-string to the notification func (n *Notification) APIURL() string { return setting.AppURL + "api/v1/notifications/threads/" + strconv.FormatInt(n.ID, 10) diff --git a/models/issues/issue.go b/models/issues/issue.go index a86d50ca9da..bb6d17d334b 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/svg" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -442,6 +443,30 @@ func (issue *Issue) PatchURL() string { return "" } +/* the logic should be kept the same as getIssueIcon/getIssueColor in TS code */ +func (issue *Issue) IconHTML(ctx context.Context) template.HTML { + if issue.IsPull { + if issue.PullRequest == nil { // pull request should be loaded before calling this function + return template.HTML("No PullRequest") + } + if issue.IsClosed { + if issue.PullRequest.HasMerged { + return svg.RenderHTML("octicon-git-merge", 16, "text purple") + } + return svg.RenderHTML("octicon-git-pull-request-closed", 16, "text red") + } + if issue.PullRequest.IsWorkInProgress(ctx) { + return svg.RenderHTML("octicon-git-pull-request-draft", 16, "text grey") + } + return svg.RenderHTML("octicon-git-pull-request", 16, "text green") + } + + if issue.IsClosed { + return svg.RenderHTML("octicon-issue-closed", 16, "text red") + } + return svg.RenderHTML("octicon-issue-opened", 16, "text green") +} + // State returns string representation of issue status. func (issue *Issue) State() api.StateType { if issue.IsClosed { diff --git a/services/markup/renderhelper_issueicontitle.go b/services/markup/renderhelper_issueicontitle.go index 27b5595fa99..dd5049a4cf6 100644 --- a/services/markup/renderhelper_issueicontitle.go +++ b/services/markup/renderhelper_issueicontitle.go @@ -58,10 +58,6 @@ func renderRepoIssueIconTitle(ctx context.Context, opts markup.RenderIssueIconTi } } - htmlIcon, err := webCtx.RenderToHTML("shared/issueicon", issue) - if err != nil { - return "", err - } - - return htmlutil.HTMLFormat(`%s %s %s`, opts.LinkHref, htmlIcon, issue.Title, textIssueIndex), nil + return htmlutil.HTMLFormat(`%s %s %s`, opts.LinkHref, + issue.IconHTML(ctx), issue.Title, textIssueIndex), nil } diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 4e8ad1326c0..24cf537c055 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -187,7 +187,7 @@ {{end}} {{if .HasPullRequest}}
- {{template "shared/issueicon" .}} + {{.IconHTML ctx}}
{{ctx.RenderUtils.RenderIssueTitle .PullRequest.Issue.Title $.Repository}} #{{.PullRequest.Issue.Index}} diff --git a/templates/repo/issue/card.tmpl b/templates/repo/issue/card.tmpl index 9a8341dccaf..78625c0e882 100644 --- a/templates/repo/issue/card.tmpl +++ b/templates/repo/issue/card.tmpl @@ -12,7 +12,7 @@
- {{template "shared/issueicon" .}} + {{.IconHTML ctx}}
{{.Title | ctx.RenderUtils.RenderIssueSimpleTitle}} {{if and $.isPinnedIssueCard $.Page.IsRepoAdmin}} diff --git a/templates/shared/issueicon.tmpl b/templates/shared/issueicon.tmpl deleted file mode 100644 index bb6247c7088..00000000000 --- a/templates/shared/issueicon.tmpl +++ /dev/null @@ -1,26 +0,0 @@ -{{/* the logic should be kept the same as getIssueIcon/getIssueColor in JS code */}} -{{- if .IsPull -}} - {{- if not .PullRequest -}} - No PullRequest - {{- else -}} - {{- if .IsClosed -}} - {{- if .PullRequest.HasMerged -}} - {{- svg "octicon-git-merge" 16 "text purple" -}} - {{- else -}} - {{- svg "octicon-git-pull-request-closed" 16 "text red" -}} - {{- end -}} - {{- else -}} - {{- if .PullRequest.IsWorkInProgress ctx -}} - {{- svg "octicon-git-pull-request-draft" 16 "text grey" -}} - {{- else -}} - {{- svg "octicon-git-pull-request" 16 "text green" -}} - {{- end -}} - {{- end -}} - {{- end -}} -{{- else -}} - {{- if .IsClosed -}} - {{- svg "octicon-issue-closed" 16 "text red" -}} - {{- else -}} - {{- svg "octicon-issue-opened" 16 "text green" -}} - {{- end -}} -{{- end -}} diff --git a/templates/shared/issuelist.tmpl b/templates/shared/issuelist.tmpl index 30670c3b0fc..92d98d8d849 100644 --- a/templates/shared/issuelist.tmpl +++ b/templates/shared/issuelist.tmpl @@ -7,7 +7,7 @@ {{if $.CanWriteIssuesOrPulls}} {{end}} - {{template "shared/issueicon" .}} + {{.IconHTML ctx}}
diff --git a/templates/user/notification/notification_div.tmpl b/templates/user/notification/notification_div.tmpl index eb24288a4b8..5b4650cc11d 100644 --- a/templates/user/notification/notification_div.tmpl +++ b/templates/user/notification/notification_div.tmpl @@ -37,15 +37,7 @@ {{range $notification := .Notifications}}