1
1
package issues
2
2
3
3
import (
4
- "fmt"
5
4
"github.com/gdamore/tcell/v2"
6
5
"github.com/mk-5/fjira/internal/app"
7
6
"github.com/mk-5/fjira/internal/jira"
8
7
"github.com/mk-5/fjira/internal/ui"
9
8
"strings"
9
+ "time"
10
10
)
11
11
12
12
const (
13
- MaxJqlLines = 1000
14
13
DefaultJqlQuery = "created >= -30d order by created DESC"
15
- MaxJqlLength = 2000
14
+ BadRequest = "Bad Request"
16
15
)
17
16
18
17
type jqlSearchView struct {
19
18
app.View
20
- api jira.Api
21
- bottomBar * app.ActionBar
22
- fuzzyFind * app. FuzzyFind
23
- jqlStorage * jqlStorage
19
+ api jira.Api
20
+ fuzzyFind * app.FuzzyFind
21
+ issues []jira. Issue
22
+ jql string
24
23
}
25
24
26
25
func NewJqlSearchView (api jira.Api ) app.View {
27
- bottomBar := ui .CreateBottomActionBarWithItems ([]ui.NavItemConfig {
28
- ui.NavItemConfig {Action : ui .ActionNew , Text1 : ui .MessageNew , Text2 : "[F1]" , Key : tcell .KeyF1 },
29
- })
30
- bottomBar .AddItem (ui .NewDeleteItem ())
31
- bottomBar .AddItem (ui .NewCancelBarItem ())
32
26
return & jqlSearchView {
33
- api : api ,
34
- bottomBar : bottomBar ,
35
- jqlStorage : & jqlStorage {},
27
+ api : api ,
28
+ jql : DefaultJqlQuery ,
36
29
}
37
30
}
38
31
39
32
func (view * jqlSearchView ) Init () {
40
33
go view .startJqlFuzzyFind ()
41
- go view .handleBottomBarActions ()
42
34
}
43
35
44
36
func (view * jqlSearchView ) Destroy () {
45
- view . bottomBar . Destroy ()
37
+ // do nothing
46
38
}
47
39
48
40
func (view * jqlSearchView ) Draw (screen tcell.Screen ) {
49
41
if view .fuzzyFind != nil {
50
42
view .fuzzyFind .Draw (screen )
51
43
}
52
- view .bottomBar .Draw (screen )
53
44
}
54
45
55
46
func (view * jqlSearchView ) Update () {
56
- view .bottomBar .Update ()
57
47
if view .fuzzyFind != nil {
58
48
view .fuzzyFind .Update ()
59
49
}
@@ -63,11 +53,9 @@ func (view *jqlSearchView) Resize(screenX, screenY int) {
63
53
if view .fuzzyFind != nil {
64
54
view .fuzzyFind .Resize (screenX , screenY )
65
55
}
66
- view .bottomBar .Resize (screenX , screenY )
67
56
}
68
57
69
58
func (view * jqlSearchView ) HandleKeyEvent (ev * tcell.EventKey ) {
70
- view .bottomBar .HandleKeyEvent (ev )
71
59
if view .fuzzyFind != nil {
72
60
view .fuzzyFind .HandleKeyEvent (ev )
73
61
}
@@ -76,94 +64,44 @@ func (view *jqlSearchView) HandleKeyEvent(ev *tcell.EventKey) {
76
64
func (view * jqlSearchView ) startJqlFuzzyFind () {
77
65
app .GetApp ().ClearNow ()
78
66
app .GetApp ().Loading (true )
79
- jqls , err := view .jqlStorage .readAll ()
80
- if err != nil {
81
- app .Error (err .Error ())
82
- return
83
- }
84
- jqls = append (jqls , DefaultJqlQuery )
85
- view .fuzzyFind = app .NewFuzzyFind (ui .MessageJqlFuzzyFind , jqls )
86
- view .fuzzyFind .MarginBottom = 1
67
+ view .fuzzyFind = app .NewFuzzyFindWithProvider (ui .MessageJqlFuzzyFind , view .findIssues )
68
+ view .fuzzyFind .MarginBottom = 0
69
+ view .fuzzyFind .SetQuery (DefaultJqlQuery )
70
+ view .fuzzyFind .AlwaysShowAllResults ()
71
+ // higher debounce in order to give more time to change jql
72
+ view .fuzzyFind .SetDebounceMs (500 * time .Millisecond )
87
73
app .GetApp ().Loading (false )
88
- if jql := <- view .fuzzyFind .Complete ; true {
74
+ if chosen := <- view .fuzzyFind .Complete ; true {
89
75
app .GetApp ().ClearNow ()
90
76
query := view .fuzzyFind .GetQuery ()
91
- if jql .Index < 0 && strings .TrimSpace (query ) == "" {
77
+ if chosen .Index < 0 && strings .TrimSpace (query ) == "" {
92
78
// do nothing
93
79
return
94
80
}
95
- if jql .Index < 0 {
96
- // do nothing, restart view
97
- app .GetApp (). SetView ( NewJqlSearchView ( view .api ) )
81
+ if chosen .Index >= 0 {
82
+ chosenIssue := view . issues [ chosen . Index ]
83
+ app .GoTo ( "issue" , chosenIssue . Key , view .reopen , view . api )
98
84
return
99
85
}
100
- view .fuzzyFind = nil
101
- app .GoTo ("issues-search-jql" , jqls [jql .Index ], view .api )
102
- //GoIntoIssuesSearchForJql(jqls[jql.Index], view.api)
103
86
}
104
87
}
105
88
106
- func (view * jqlSearchView ) handleBottomBarActions () {
107
- for {
108
- action := <- view .bottomBar .Action
109
- switch action {
110
- case ui .ActionCancel :
111
- view .cancel ()
112
- return
113
- case ui .ActionNew :
114
- view .newJql ()
115
- return
116
- case ui .ActionDelete :
117
- go view .confirmJqlDelete (view .fuzzyFind .GetSelectedItem ())
118
- }
119
- }
89
+ func (view * jqlSearchView ) reopen () {
90
+ app .GoTo ("jql" , view .api )
120
91
}
121
92
122
- func (view * jqlSearchView ) cancel () {
123
- if view .fuzzyFind != nil {
124
- app .GetApp ().Quit ()
125
- }
126
- if view .fuzzyFind == nil {
127
- app .GetApp ().SetView (NewJqlSearchView (view .api ))
93
+ func (view * jqlSearchView ) findIssues (query string ) []string {
94
+ app .GetApp ().LoadingWithText (true , ui .MessageSearchIssuesLoading )
95
+ issues , err := view .api .SearchJql (query )
96
+ app .GetApp ().Loading (false )
97
+ if err != nil && strings .Contains (err .Error (), BadRequest ) {
98
+ // do nothing, invalid JQL query
99
+ return FormatJiraIssues (view .issues )
128
100
}
129
- }
130
-
131
- func (view * jqlSearchView ) newJql () {
132
- app .GetApp ().SetView (ui .NewTextWriterView (& ui.TextWriterArgs {
133
- Header : ui .MessageTypeJqlAndSave ,
134
- GoBack : func () {
135
- app .GetApp ().SetView (NewJqlSearchView (view .api ))
136
- },
137
- TextConsumer : func (s string ) {
138
- err := view .jqlStorage .addNew (s )
139
- if err != nil {
140
- app .Error (err .Error ())
141
- return
142
- }
143
- app .Success (ui .MessageJqlAddSuccess )
144
- },
145
- MaxLength : MaxJqlLength ,
146
- }))
147
- }
148
-
149
- func (view * jqlSearchView ) confirmJqlDelete (jql string ) {
150
- message := fmt .Sprintf (ui .MessageJqlRemoveConfirm , jql )
151
- view .fuzzyFind = nil
152
- app .GetApp ().ClearNow ()
153
- view .bottomBar .Clear ()
154
- view .bottomBar .AddItem (ui .NewYesBarItem ())
155
- view .bottomBar .AddItem (ui .NewCancelBarItem ())
156
- createNewJql := app .Confirm (app .GetApp (), message )
157
- switch createNewJql {
158
- case true :
159
- err := view .jqlStorage .remove (jql )
160
- if err != nil {
161
- app .Error (fmt .Sprintf (ui .MessageCannotAddNewJql , err ))
162
- return
163
- }
164
- app .Success (ui .MessageJqlRemoveSuccess )
165
- app .GetApp ().SetView (NewJqlSearchView (view .api ))
166
- case false :
167
- app .GetApp ().SetView (NewJqlSearchView (view .api ))
101
+ if err != nil {
102
+ app .Error (err .Error ())
103
+ return FormatJiraIssues (view .issues )
168
104
}
105
+ view .issues = issues
106
+ return FormatJiraIssues (view .issues )
169
107
}
0 commit comments