Completed
Push — main ( 28a13c...ee7c69 )
by Yume
24s queued 21s
created

partials.Navbar   F

Complexity

Conditions 15

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 44
nop 2
dl 0
loc 57
rs 2.9998
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like partials.Navbar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// Code generated by templ - DO NOT EDIT.
2
3
// templ: version: v0.2.598
4
package partials
5
6
//lint:file-ignore SA4006 This context is only used if a nested component is present.
7
8
import "github.com/a-h/templ"
9
import "context"
10
import "io"
11
import "bytes"
12
13
func LogoImg() templ.Component {
14
	return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
15
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
16
		if !templ_7745c5c3_IsBuffer {
17
			templ_7745c5c3_Buffer = templ.GetBuffer()
18
			defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
19
		}
20
		ctx = templ.InitializeContext(ctx)
21
		templ_7745c5c3_Var1 := templ.GetChildren(ctx)
22
		if templ_7745c5c3_Var1 == nil {
23
			templ_7745c5c3_Var1 = templ.NopComponent
24
		}
25
		ctx = templ.ClearChildren(ctx)
26
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<picture><source media=\"(min-width: 768px)\" srcset=\"/static/img/memnix_80x80.avif\" type=\"image/avif\"> <source media=\"(min-width: 768px)\" srcset=\"/static/img/memnix_80x80.webp\" type=\"image/webp\"> <source media=\"(max-width: 767px)\" srcset=\"/static/img/memnix_64x64.avif\" type=\"image/avif\"> <source media=\"(max-width: 767px)\" srcset=\"/static/img/memnix_64x64.webp\" type=\"image/webp\"> <img src=\"/static/img/icon.png\" class=\"mx-auto h-14 w-14 md:h-20 md:w-20\" alt=\"Memnix app logo\" width=\"80\" height=\"80\"></picture>")
27
		if templ_7745c5c3_Err != nil {
28
			return templ_7745c5c3_Err
29
		}
30
		if !templ_7745c5c3_IsBuffer {
31
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
32
		}
33
		return templ_7745c5c3_Err
34
	})
35
}
36
37
func Navbar(username string, fromProtected bool) templ.Component {
38
	return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
39
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
40
		if !templ_7745c5c3_IsBuffer {
41
			templ_7745c5c3_Buffer = templ.GetBuffer()
42
			defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
43
		}
44
		ctx = templ.InitializeContext(ctx)
45
		templ_7745c5c3_Var2 := templ.GetChildren(ctx)
46
		if templ_7745c5c3_Var2 == nil {
47
			templ_7745c5c3_Var2 = templ.NopComponent
48
		}
49
		ctx = templ.ClearChildren(ctx)
50
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"navbar transition-all md:px-5\" data-testid=\"navbar\"><div class=\"navbar-start\" data-testid=\"navbarStart\">")
51
		if templ_7745c5c3_Err != nil {
52
			return templ_7745c5c3_Err
53
		}
54
		templ_7745c5c3_Err = LogoImg().Render(ctx, templ_7745c5c3_Buffer)
55
		if templ_7745c5c3_Err != nil {
56
			return templ_7745c5c3_Err
57
		}
58
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div><div class=\"navbar-center\" data-testid=\"navbarCenter\"><a hx-swap=\"transition:true\" href=\"/\" class=\"text-3xl font-bold\" preload>Memnix</a></div><div class=\"navbar-end\" data-testid=\"navbarEnd\">")
59
		if templ_7745c5c3_Err != nil {
60
			return templ_7745c5c3_Err
61
		}
62
		if fromProtected {
63
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span class=\"font-bold text-indigo-700 mr-8\" data-testid=\"username\">")
64
			if templ_7745c5c3_Err != nil {
65
				return templ_7745c5c3_Err
66
			}
67
			var templ_7745c5c3_Var3 string
68
			templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(username)
69
			if templ_7745c5c3_Err != nil {
70
				return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/partials/navbar.partial.templ`, Line: 25, Col: 15}
71
			}
72
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
73
			if templ_7745c5c3_Err != nil {
74
				return templ_7745c5c3_Err
75
			}
76
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span> <button hx-swap=\"transition:true\" hx-post=\"/todo/logout\" hx-confirm=\"Are you sure you want to log out?\" hx-target=\"body\" hx-push-url=\"true\" class=\"btn btn-ghost text-lg\" data-testid=\"logoutButton\">Logout</button>")
77
			if templ_7745c5c3_Err != nil {
78
				return templ_7745c5c3_Err
79
			}
80
		} else {
81
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a preload hx-swap=\"transition:true\" data-testid=\"loginButton\" class=\"btn btn-primary text-lg\" href=\"/login\">Login</a>")
82
			if templ_7745c5c3_Err != nil {
83
				return templ_7745c5c3_Err
84
			}
85
		}
86
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></nav>")
87
		if templ_7745c5c3_Err != nil {
88
			return templ_7745c5c3_Err
89
		}
90
		if !templ_7745c5c3_IsBuffer {
91
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
92
		}
93
		return templ_7745c5c3_Err
94
	})
95
}
96