-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathMain.elm
159 lines (131 loc) · 4.05 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
module Main exposing (main)
import Browser exposing (UrlRequest(..))
import Browser.Navigation exposing (Key)
import Json.Decode as Json
import Parsing
import Types exposing (Model, Msg(..), Route(..))
import Updates exposing (update)
import Url exposing (Url)
import Utils.Api as Api
import Utils.DateTimePicker.Utils exposing (FirstDayOfWeek(..))
import Utils.Filter exposing (nullFilter)
import Utils.Types exposing (ApiData(..))
import Views
import Views.AlertList.Types exposing (initAlertList)
import Views.SilenceForm.Types exposing (initSilenceForm)
import Views.SilenceList.Types exposing (initSilenceList)
import Views.SilenceView.Types exposing (initSilenceView)
import Views.Status.Types exposing (initStatusModel)
main : Program Json.Value Model Msg
main =
Browser.application
{ init = init
, update = update
, view =
\model ->
{ title = "Alertmanager"
, body = [ Views.view model ]
}
, subscriptions = always Sub.none
, onUrlRequest =
\request ->
case request of
Internal url ->
NavigateToInternalUrl (Url.toString url)
External url ->
NavigateToExternalUrl url
, onUrlChange = urlUpdate
}
init : Json.Value -> Url -> Key -> ( Model, Cmd Msg )
init flags url key =
let
route =
Parsing.urlParser url
filter =
case route of
AlertsRoute filter_ ->
filter_
SilenceListRoute filter_ ->
filter_
_ ->
nullFilter
prod =
flags
|> Json.decodeValue (Json.field "production" Json.bool)
|> Result.withDefault False
defaultCreator =
flags
|> Json.decodeValue (Json.field "defaultCreator" Json.string)
|> Result.withDefault ""
groupExpandAll =
flags
|> Json.decodeValue (Json.field "groupExpandAll" Json.bool)
|> Result.withDefault False
apiUrl =
if prod then
Api.makeApiUrl url.path
else
Api.makeApiUrl "http://localhost:9093/"
libUrl =
if prod then
url.path
else
"/"
firstDayOfWeek =
flags
|> Json.decodeValue (Json.field "firstDayOfWeek" Json.string)
|> Result.withDefault "Sunday"
|> (\d ->
case d of
"Sunday" ->
Sunday
_ ->
Monday
)
in
update (urlUpdate url)
(Model
(initSilenceList key)
(initSilenceView key)
(initSilenceForm key firstDayOfWeek)
(initAlertList key groupExpandAll)
route
filter
initStatusModel
url.path
apiUrl
libUrl
Loading
Loading
Loading
defaultCreator
groupExpandAll
key
{ firstDayOfWeek = firstDayOfWeek
}
)
urlUpdate : Url -> Msg
urlUpdate url =
let
route =
Parsing.urlParser url
in
case route of
SilenceListRoute maybeFilter ->
NavigateToSilenceList maybeFilter
SilenceViewRoute silenceId ->
NavigateToSilenceView silenceId
SilenceFormEditRoute silenceId ->
NavigateToSilenceFormEdit silenceId
SilenceFormNewRoute params ->
NavigateToSilenceFormNew params
AlertsRoute filter ->
NavigateToAlerts filter
StatusRoute ->
NavigateToStatus
SettingsRoute ->
NavigateToSettings
TopLevelRoute ->
RedirectAlerts
NotFoundRoute ->
NavigateToNotFound