Monorepo for Tangled

appview/profile: fix profile card rendering on tabs

the .ShowPunchcard variable was being referenced in pages that didn't
have the variable set. to circumvent this, the `State.profile` helper
calculates whether or not to show the punchcard, and this helper is then
substituted in all profile tabs.

Signed-off-by: oppiliappan <me@oppi.li>

+42 -43
+19 -21
appview/db/profile.go
··· 16 16 17 17 const TimeframeMonths = 7 18 18 19 - func MakeProfileTimeline(e Execer, forDid string, includePunchcard bool) (*models.ProfileTimeline, error) { 19 + func MakeProfileTimeline(e Execer, forDid string) (*models.ProfileTimeline, error) { 20 20 timeline := models.ProfileTimeline{ 21 21 ByMonth: make([]models.ByMonth, TimeframeMonths), 22 22 } ··· 98 98 }) 99 99 } 100 100 101 - if includePunchcard { 102 - punchcard, err := MakePunchcard( 103 - e, 104 - orm.FilterEq("did", forDid), 105 - orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), 106 - ) 107 - if err != nil { 108 - return nil, fmt.Errorf("error getting commits by did: %w", err) 101 + punchcard, err := MakePunchcard( 102 + e, 103 + orm.FilterEq("did", forDid), 104 + orm.FilterGte("date", time.Now().AddDate(0, -TimeframeMonths, 0)), 105 + ) 106 + if err != nil { 107 + return nil, fmt.Errorf("error getting commits by did: %w", err) 108 + } 109 + for _, punch := range punchcard.Punches { 110 + if punch.Date.After(now) { 111 + continue 109 112 } 110 - for _, punch := range punchcard.Punches { 111 - if punch.Date.After(now) { 112 - continue 113 - } 114 113 115 - monthsAgo := monthsBetween(punch.Date, now) 116 - if monthsAgo >= TimeframeMonths { 117 - // shouldn't happen; but times are weird 118 - continue 119 - } 114 + monthsAgo := monthsBetween(punch.Date, now) 115 + if monthsAgo >= TimeframeMonths { 116 + // shouldn't happen; but times are weird 117 + continue 118 + } 120 119 121 - idx := monthsAgo 122 - timeline.ByMonth[idx].Commits += punch.Count 123 - } 120 + idx := monthsAgo 121 + timeline.ByMonth[idx].Commits += punch.Count 124 122 } 125 123 126 124 return &timeline, nil
+1 -1
appview/pages/templates/layouts/profilebase.html
··· 52 52 <div class="{{ $style }} order-1 order-1"> 53 53 <div class="flex flex-col gap-4"> 54 54 {{ template "user/fragments/profileCard" .Card }} 55 - {{ if .ShowPunchcard }} 55 + {{ if .Card.Punchcard }} 56 56 {{ block "punchcard" .Card.Punchcard }} {{ end }} 57 57 {{ end }} 58 58 </div>
+22 -21
appview/state/profile.go
··· 4 4 "context" 5 5 "fmt" 6 6 "log" 7 - "log/slog" 8 7 "net/http" 9 8 "slices" 10 9 "strings" ··· 90 89 followStatus = db.GetFollowStatus(s.db, loggedInUser.Active.Did, did) 91 90 } 92 91 93 - now := time.Now() 94 - startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC) 95 - punchcard, err := db.MakePunchcard( 96 - s.db, 97 - orm.FilterEq("did", did), 98 - orm.FilterGte("date", startOfYear.Format(time.DateOnly)), 99 - orm.FilterLte("date", now.Format(time.DateOnly)), 100 - ) 101 - if err != nil { 102 - return nil, fmt.Errorf("failed to get punchcard for %s: %w", did, err) 92 + showPunchcard := s.shouldShowPunchcard(did, loggedInUser.Did()) 93 + 94 + var punchcard *models.Punchcard 95 + if showPunchcard { 96 + now := time.Now() 97 + startOfYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.UTC) 98 + punchcard, err = db.MakePunchcard( 99 + s.db, 100 + orm.FilterEq("did", did), 101 + orm.FilterGte("date", startOfYear.Format(time.DateOnly)), 102 + orm.FilterLte("date", now.Format(time.DateOnly)), 103 + ) 104 + if err != nil { 105 + return nil, fmt.Errorf("failed to get punchcard for %s: %w", did, err) 106 + } 103 107 } 104 108 105 109 return &pages.ProfileCard{ ··· 165 169 } 166 170 } 167 171 168 - loggedInUser := s.oauth.GetMultiAccountUser(r) 169 - 170 - showPunchcard := checkIfPunchcardShouldShow(s.db, l, profile.UserDid, loggedInUser.Did()) 171 - 172 - timeline, err := db.MakeProfileTimeline(s.db, profile.UserDid, showPunchcard) 172 + timeline, err := db.MakeProfileTimeline(s.db, profile.UserDid) 173 173 if err != nil { 174 174 l.Error("failed to create timeline", "err", err) 175 175 } ··· 180 180 Repos: pinnedRepos, 181 181 CollaboratingRepos: pinnedCollaboratingRepos, 182 182 ProfileTimeline: timeline, 183 - ShowPunchcard: showPunchcard, 184 183 }) 185 184 } 186 185 187 - func checkIfPunchcardShouldShow(e db.Execer, l *slog.Logger, targetDid, requesterDid string) bool { 188 - targetPunchcardPreferences, err := db.GetPunchcardPreference(e, targetDid) 186 + func (s *State) shouldShowPunchcard(targetDid, requesterDid string) bool { 187 + l := s.logger.With("helper", "shouldShowPunchcard") 188 + 189 + targetPunchcardPreferences, err := db.GetPunchcardPreference(s.db, targetDid) 189 190 if err != nil { 190 191 l.Error("failed to get target users punchcard preferences", "err", err) 191 192 return true 192 193 } 193 194 194 - requesterPunchcardPreferences, err := db.GetPunchcardPreference(e, requesterDid) 195 + requesterPunchcardPreferences, err := db.GetPunchcardPreference(s.db, requesterDid) 195 196 if err != nil { 196 197 l.Error("failed to get requester users punchcard preferences", "err", err) 197 198 return true ··· 446 447 } 447 448 448 449 func (s *State) getProfileFeed(ctx context.Context, id *identity.Identity) (*feeds.Feed, error) { 449 - timeline, err := db.MakeProfileTimeline(s.db, id.DID.String(), false) 450 + timeline, err := db.MakeProfileTimeline(s.db, id.DID.String()) 450 451 if err != nil { 451 452 return nil, err 452 453 }