aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Main.hs
blob: 3a2c5a6cd94e092ea0067d7342b6c8b10cbc0a59 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
module Main where

#ifndef ghcjs_HOST_OS
import Language.Javascript.JSaddle.Warp as JSaddle
#endif

import Api
import Data.Bifunctor
import Data.Default
import Data.Function
import Miso
import Miso.String (toMisoString)
import NeatInterpolation qualified as Q
import Page (Page, initialPage, updatePage, viewPage)
import Page qualified as Page
import Route (parseURI)
import Version

data Model
  = Loading
  | Failed String
  | Loaded LoadedState
  deriving (Show, Eq)

data LoadedState = LoadedState
  { collections :: [String],
    schemaVersion :: Version,
    page :: Maybe (Either String Page)
  }
  deriving (Show, Eq)

instance Default Model where
  def = Loading

data Action
  = -- Loading
    SetLoaded (Either String LoadedState)
  | -- Loaded
    NoOp
  | Init URI
  | HandleURI URI
  | HandlePage Page.Action
  | SetPage (Either String Page)
  deriving (Show, Eq)

#ifndef ghcjs_HOST_OS
runApp :: JSM () -> IO ()
runApp f = JSaddle.debugOr 8080 (f >> syncPoint) JSaddle.jsaddleApp
#else
runApp :: IO () -> IO ()
runApp app = app
#endif

main :: IO ()
main = runApp $ do
  uri <- getCurrentURI
  startApp App {initialAction = Init uri, ..}
  where
    model = def
    update = updateModel
    view = viewModel
    events = defaultEvents
    subs = [uriSub HandleURI]
    mountPoint = Nothing
    logLevel = Off

updateModel :: Action -> Model -> Effect Action Model
updateModel _ (Failed err) = noEff (Failed err)
updateModel (Init uri) Loading =
  Loading <# do
    page <- Just <$> initialPage (parseURI uri)
    schemaVersion' <- fetchSchemaVersion
    collections' <- fetchCollections
    pure $ SetLoaded do
      schemaVersion <- schemaVersion'
      collections <- collections'
      pure LoadedState {..}
updateModel (Init _) m = noEff m
updateModel (SetLoaded (Left err)) Loading = noEff (Failed err)
updateModel (SetLoaded (Right state)) Loading = noEff (Loaded state)
updateModel (SetLoaded _) m = noEff m
updateModel _ Loading = noEff Loading
updateModel NoOp (Loaded s) = noEff (Loaded s)
updateModel (HandleURI uri) (Loaded s) =
  Loaded s <# do
    let route = parseURI uri
    SetPage <$> initialPage route
updateModel (SetPage page) (Loaded s) = noEff (Loaded s {page = Just page})
updateModel (HandlePage action) (Loaded s) =
  case s.page of
    Just (Right page) ->
      fmap Loaded $
        updatePage action page
          & bimap HandlePage (\page -> s {page = Just (Right page)})
    _ -> noEff (Loaded s)

viewModel :: Model -> View Action
viewModel Loading = text ".."
viewModel (Failed s) = err s
viewModel (Loaded s) =
  div_ [] $
    [ viewCss,
      viewHeader s,
      nav_ [] [viewCollections s],
      main_ [] $
        [ HandlePage <$> maybe (text "..") (either err viewPage) s.page
        ]
    ]

viewCss :: View Action
viewCss =
  node
    HTML
    "style"
    Nothing
    [type_ "text/css"]
    [ text
        ( toMisoString
            [Q.text|
/* normalize */
* {
  box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh; }

/* typography */
html {
  font: Iosevka; }

/* layout */
body > div {
  display: flex;
  flex-flow: row nowrap;
  min-height: 100vh;
  padding-top: 64px;
  align-items: stretch; }
header {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  height: 64px; }
nav, main {
  min-height: 100%; }
nav {
  flex: 0 0 200px; }
main {
  flex: 1 1 auto; }

/* borders */
header {
  border-bottom: 1px solid gray; }
nav {
  border-right: 1px solid gray; }

/* padding */
nav, header, main {
  padding: 16px; }

/* scrolling */
body > div {
  overflow: visible; }
header {
  overflow: visible; }
nav, main {
  overflow: auto; }

/* header */
header {
  display: flex;
  align-items: center; }
header section {
  margin-left: auto; }
header section:first-child {
  margin-left: 0; }

/* table layout */
th, td {
  text-align: left;
  padding: 0 16px;
  line-height: 52px;
  text-overflow: ellipsis;
}

/* table borders */
table {
  border-collapse: collapse;
  border-left: 1px solid gray;
  border-right: 1px solid gray; }
th, td {
  border-top: 1px solid gray;
  border-bottom: 1px solid gray; }
|]
        )
    ]

err :: String -> View action
err = text . toMisoString . ("err! " <>)

viewHeader :: LoadedState -> View Action
viewHeader s =
  header_ [] $
    [ section_ [] [h1_ [] [a_ [href_ "#"] [text "acms"]]],
      section_ [] (viewBranch s)
    ]

viewBranch :: LoadedState -> [View Action]
viewBranch s =
  [ text (toMisoString (versionToString s.schemaVersion)),
    text " ",
    select_ [] [option_ [] [text "main"]]
  ]

viewCollections :: LoadedState -> View Action
viewCollections s =
  section_ [] $
    [ span_
        []
        [ text "collections",
          text " ",
          a_ [href_ "#collection/new"] [text "+new"]
        ],
      ol_ [] $
        [ li_
            []
            [ a_
                [href_ (toMisoString ("#collection/" <> collection))]
                [text (toMisoString collection)]
            ]
          | collection <- s.collections
        ]
    ]