This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.elm
715 lines (579 loc) · 22.5 KB
/
App.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
module UnisonShare.App exposing (..)
import Api
import Browser
import Browser.Navigation as Nav
import CodebaseTree
import Definition.Reference exposing (Reference)
import Env exposing (Env)
import FullyQualifiedName as FQN exposing (FQN)
import Hashvatar
import Html exposing (Html, a, div, h1, h2, nav, p, span, text)
import Html.Attributes exposing (class, classList, id, title)
import Html.Events exposing (onClick)
import Http
import KeyboardShortcut
import KeyboardShortcut.Key exposing (Key(..))
import KeyboardShortcut.KeyboardEvent as KeyboardEvent exposing (KeyboardEvent)
import Namespace exposing (NamespaceDetails)
import Perspective exposing (Perspective(..))
import PerspectiveLanding
import RemoteData
import UI
import UI.AppHeader as AppHeader
import UI.Banner as Banner
import UI.Button as Button
import UI.Click as Click exposing (Click(..))
import UI.Icon as Icon
import UI.PageLayout as PageLayout
import UI.Sidebar as Sidebar
import UI.Tooltip as Tooltip
import UnisonShare.AppModal as AppModal
import UnisonShare.Page.Catalog as Catalog
import UnisonShare.Route as Route exposing (Route)
import Url exposing (Url)
import Workspace
import Workspace.WorkspaceItems as WorkspaceItems
-- MODEL
type alias Model =
{ navKey : Nav.Key
, route : Route
, codebaseTree : CodebaseTree.Model
, workspace : Workspace.Model
, perspectiveLanding : PerspectiveLanding.Model
, catalog : Catalog.Model
, appModal : AppModal.Model
, keyboardShortcut : KeyboardShortcut.Model
, env : Env
-- This is called "toggled" and not "hidden" because the behavior of
-- toggling the sidebar on/off is inverse on mobile vs desktop
, sidebarToggled : Bool
}
init : Env -> Route -> Nav.Key -> ( Model, Cmd Msg )
init env route navKey =
let
-- TODO: This whole thing should be route driven
( workspace, workspaceCmd ) =
case route of
Route.Project _ (Route.ProjectDefinition ref) ->
Workspace.init env (Just ref)
_ ->
Workspace.init env Nothing
( codebaseTree, codebaseTreeCmd ) =
CodebaseTree.init env
fetchNamespaceDetailsCmd =
env.perspective
|> fetchNamespaceDetails
|> Maybe.map (Api.perform env.apiBasePath)
|> Maybe.withDefault Cmd.none
( catalog, catalogCmd ) =
Catalog.init env
model =
{ navKey = navKey
, route = route
, workspace = workspace
, perspectiveLanding = PerspectiveLanding.init
, codebaseTree = codebaseTree
, appModal = AppModal.init
, keyboardShortcut = KeyboardShortcut.init env.operatingSystem
, env = env
, sidebarToggled = False
, catalog = catalog
}
in
( model
, Cmd.batch
[ Cmd.map CodebaseTreeMsg codebaseTreeCmd
, Cmd.map WorkspaceMsg workspaceCmd
, Cmd.map CatalogMsg catalogCmd
, fetchNamespaceDetailsCmd
]
)
-- UPDATE
type Msg
= LinkClicked Browser.UrlRequest
| UrlChanged Url
| ChangePerspective Perspective
| FetchPerspectiveNamespaceDetailsFinished FQN (Result Http.Error NamespaceDetails)
| Keydown KeyboardEvent
| OpenDefinition Reference
| ShowModal AppModal.AppModal
| ToggleSidebar
-- sub msgs
| AppModalMsg AppModal.Msg
| CatalogMsg Catalog.Msg
| WorkspaceMsg Workspace.Msg
| PerspectiveLandingMsg PerspectiveLanding.Msg
| CodebaseTreeMsg CodebaseTree.Msg
| KeyboardShortcutMsg KeyboardShortcut.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ env } as model) =
case ( model.route, msg ) of
( _, LinkClicked urlRequest ) ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.navKey (Url.toString url) )
-- External links are handled via target blank and never end up
-- here
Browser.External _ ->
( model, Cmd.none )
( _, UrlChanged url ) ->
let
route =
Route.fromUrl env.basePath url
model2 =
{ model | route = route }
newEnv params =
{ env | perspective = Perspective.nextFromParams env.perspective params }
in
case route of
Route.Catalog ->
let
( catalog, cmd ) =
Catalog.init model.env
in
( { model2 | catalog = catalog }, Cmd.map CatalogMsg cmd )
Route.Project params (Route.ProjectDefinition ref) ->
let
( workspace, cmd ) =
Workspace.open (newEnv params) model.workspace ref
model3 =
{ model2 | workspace = workspace, env = newEnv params }
( model4, fetchPerspectiveCmd ) =
fetchPerspectiveAndCodebaseTree env.perspective model3
in
( model4, Cmd.batch [ Cmd.map WorkspaceMsg cmd, fetchPerspectiveCmd ] )
Route.Project params Route.ProjectRoot ->
fetchPerspectiveAndCodebaseTree env.perspective { model2 | env = newEnv params }
( _, ChangePerspective perspective ) ->
navigateToPerspective model perspective
( _, FetchPerspectiveNamespaceDetailsFinished fqn details ) ->
let
perspective =
case env.perspective of
Namespace p ->
if FQN.equals p.fqn fqn then
Namespace { p | details = RemoteData.fromResult details }
else
env.perspective
_ ->
env.perspective
nextEnv =
{ env | perspective = perspective }
in
( { model | env = nextEnv }, Cmd.none )
( _, Keydown event ) ->
keydown model event
( _, OpenDefinition ref ) ->
navigateToDefinition model ref
( _, ShowModal modal ) ->
let
( appModal, cmd ) =
AppModal.show modal
in
( { model | appModal = appModal }, Cmd.map AppModalMsg cmd )
( _, ToggleSidebar ) ->
( { model | sidebarToggled = not model.sidebarToggled }, Cmd.none )
-- Sub msgs
( _, AppModalMsg amMsg ) ->
let
( am, amCmd, out ) =
AppModal.update env amMsg model.appModal
( newModel, cmd ) =
case out of
AppModal.OpenDefinition ref ->
navigateToDefinition { model | appModal = am } ref
_ ->
( { model | appModal = am }, Cmd.none )
in
( newModel, Cmd.batch [ Cmd.map AppModalMsg amCmd, cmd ] )
( Route.Catalog, CatalogMsg cMsg ) ->
let
( catalog, cmd ) =
Catalog.update cMsg model.catalog
in
( { model | catalog = catalog }, Cmd.map CatalogMsg cmd )
( Route.Project _ _, WorkspaceMsg wMsg ) ->
let
( workspace, wCmd, outMsg ) =
Workspace.update env wMsg model.workspace
model2 =
{ model | workspace = workspace }
( model3, cmd ) =
handleWorkspaceOutMsg model2 outMsg
in
( model3, Cmd.batch [ cmd, Cmd.map WorkspaceMsg wCmd ] )
( Route.Project _ Route.ProjectRoot, PerspectiveLandingMsg rMsg ) ->
let
( perspectiveLanding, outMsg ) =
PerspectiveLanding.update rMsg model.perspectiveLanding
model2 =
{ model | perspectiveLanding = perspectiveLanding }
in
case outMsg of
PerspectiveLanding.OpenDefinition ref ->
navigateToDefinition model2 ref
PerspectiveLanding.ShowFinderRequest ->
showFinder model2 Nothing
PerspectiveLanding.None ->
( model2, Cmd.none )
( Route.Project _ _, CodebaseTreeMsg cMsg ) ->
let
( codebaseTree, cCmd, outMsg ) =
CodebaseTree.update env cMsg model.codebaseTree
model2 =
{ model | codebaseTree = codebaseTree }
( model3, cmd ) =
case outMsg of
CodebaseTree.None ->
( model2, Cmd.none )
CodebaseTree.OpenDefinition ref ->
-- reset sidebarToggled to close it on mobile, but keep it open on desktop
let
model4 =
{ model2 | sidebarToggled = False }
in
navigateToDefinition model4 ref
CodebaseTree.ChangePerspectiveToNamespace fqn ->
fqn
|> Perspective.toNamespacePerspective model.env.perspective
|> navigateToPerspective model
in
( model3, Cmd.batch [ cmd, Cmd.map CodebaseTreeMsg cCmd ] )
( _, KeyboardShortcutMsg kMsg ) ->
let
( keyboardShortcut, cmd ) =
KeyboardShortcut.update kMsg model.keyboardShortcut
in
( { model | keyboardShortcut = keyboardShortcut }, Cmd.map KeyboardShortcutMsg cmd )
_ ->
( model, Cmd.none )
-- UPDATE HELPERS
navigateToDefinition : Model -> Reference -> ( Model, Cmd Msg )
navigateToDefinition model ref =
( model, Route.navigateToDefinition model.navKey model.route ref )
navigateToPerspective : Model -> Perspective -> ( Model, Cmd Msg )
navigateToPerspective model perspective =
let
-- Update all open references to be hash based to ensure that we can
-- refresh the page and fetch them appropriately even if they are
-- outside of the current perspective
workspace =
Workspace.replaceWorkspaceItemReferencesWithHashOnly model.workspace
-- Re-navigate to the currently open definition by hash
focusedReferenceRoute =
workspace.workspaceItems
|> WorkspaceItems.focusedReference
|> Maybe.map (Route.toDefinition model.route)
|> Maybe.withDefault model.route
changeRouteCmd =
Route.replacePerspective model.navKey (Perspective.toParams perspective) focusedReferenceRoute
in
( { model | workspace = workspace }, changeRouteCmd )
fetchPerspectiveAndCodebaseTree : Perspective -> Model -> ( Model, Cmd Msg )
fetchPerspectiveAndCodebaseTree oldPerspective ({ env } as model) =
let
( codebaseTree, codebaseTreeCmd ) =
CodebaseTree.init env
fetchNamespaceDetailsCmd =
env.perspective
|> fetchNamespaceDetails
|> Maybe.map (Api.perform env.apiBasePath)
|> Maybe.withDefault Cmd.none
in
if Perspective.needsFetching env.perspective then
( { model | codebaseTree = codebaseTree }
, Cmd.batch
[ Cmd.map CodebaseTreeMsg codebaseTreeCmd
, fetchNamespaceDetailsCmd
]
)
else if not (Perspective.equals oldPerspective env.perspective) then
( model, Cmd.map CodebaseTreeMsg codebaseTreeCmd )
else
( model, Cmd.none )
handleWorkspaceOutMsg : Model -> Workspace.OutMsg -> ( Model, Cmd Msg )
handleWorkspaceOutMsg model out =
case out of
Workspace.None ->
( model, Cmd.none )
Workspace.ShowFinderRequest withinNamespace ->
showFinder model withinNamespace
Workspace.Focused ref ->
( model, Route.navigateToDefinition model.navKey model.route ref )
Workspace.Emptied ->
( model, Route.navigateToCurrentPerspective model.navKey model.route )
Workspace.ChangePerspectiveToNamespace fqn ->
fqn
|> Perspective.toNamespacePerspective model.env.perspective
|> navigateToPerspective model
keydown : Model -> KeyboardEvent -> ( Model, Cmd Msg )
keydown model keyboardEvent =
let
shortcut =
KeyboardShortcut.fromKeyboardEvent model.keyboardShortcut keyboardEvent
noOp =
( model, Cmd.none )
toggleSidebar =
( { model | sidebarToggled = not model.sidebarToggled }, Cmd.none )
in
case shortcut of
KeyboardShortcut.Chord Ctrl (B _) ->
toggleSidebar
KeyboardShortcut.Chord Meta (B _) ->
toggleSidebar
KeyboardShortcut.Chord Shift QuestionMark ->
let
( am, amCmd ) =
AppModal.show AppModal.KeyboardShortcutsModal
in
( { model | appModal = am }, Cmd.map AppModalMsg amCmd )
-- TODO: Move exit by Escape into AppModal module
KeyboardShortcut.Sequence _ Escape ->
if AppModal.modalIs model.appModal AppModal.KeyboardShortcutsModal then
( { model | appModal = AppModal.close }, Cmd.none )
else
noOp
_ ->
noOp
showFinder : Model -> Maybe FQN -> ( Model, Cmd Msg )
showFinder model withinNamespace =
let
( am, amCmd ) =
AppModal.showFinder model.env withinNamespace
in
( { model | appModal = am }, Cmd.map AppModalMsg amCmd )
-- EFFECTS
fetchNamespaceDetails : Perspective -> Maybe (Api.ApiRequest NamespaceDetails Msg)
fetchNamespaceDetails perspective =
case perspective of
Namespace { fqn } ->
fqn
|> Api.namespace perspective
|> Api.toRequest Namespace.decodeDetails (FetchPerspectiveNamespaceDetailsFinished fqn)
|> Just
_ ->
Nothing
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ KeyboardEvent.subscribe KeyboardEvent.Keydown Keydown
, Sub.map WorkspaceMsg (Workspace.subscriptions model.workspace)
]
-- VIEW
appTitle : Maybe msg -> AppHeader.AppTitle msg
appTitle clickMsg =
let
appTitle_ =
case clickMsg of
Nothing ->
AppHeader.Disabled
Just msg ->
AppHeader.Clickable msg
in
appTitle_ (h1 [] [ text "Unison", span [ class "context unison-share" ] [ text "Share" ] ])
viewAppHeader : Model -> AppHeader.AppHeader Msg
viewAppHeader model =
let
changePerspectiveMsg =
case model.env.perspective of
Codebase codebaseHash ->
ChangePerspective (Codebase codebaseHash)
Namespace { codebaseHash } ->
ChangePerspective (Codebase codebaseHash)
appTitle_ =
appTitle (Just changePerspectiveMsg)
banner =
Just
(Banner.promotion "article"
"New Article: Spark-like distributed datasets in under 100 lines of Unison"
(ExternalHref "https://www.unison-lang.org/articles/distributed-datasets/")
"Check it out!"
)
in
{ menuToggle = Just ToggleSidebar
, appTitle = appTitle_
, banner = banner
, rightButton = Just (Button.button (ShowModal AppModal.PublishModal) "Publish on Unison Share" |> Button.share)
}
viewSidebarHeader : Env -> Html Msg
viewSidebarHeader env =
case env.perspective of
Codebase _ ->
UI.nothing
Namespace { fqn, details } ->
let
-- Imprecise, but close enough, approximation of overflowing,
-- which results in a slight faded left edge A better way would
-- be to measure the DOM like we do for overflowing docs, but
-- thats quite involved...
isOverflowing =
fqn |> FQN.toString |> String.length |> (\l -> l > 20)
download =
Button.iconThenLabel (ShowModal (AppModal.DownloadModal fqn)) Icon.download "Download latest version"
|> Button.small
|> Button.view
|> List.singleton
|> Sidebar.headerItem []
hashvatar =
details
|> RemoteData.map (Namespace.hash >> Hashvatar.view)
|> RemoteData.withDefault Hashvatar.empty
in
Sidebar.header
[ Sidebar.headerItem
[ classList [ ( "is-overflowing", isOverflowing ) ] ]
[ hashvatar
, h2 [ class "namespace" ] [ FQN.view fqn ]
]
, download
, UI.divider
]
viewMainSidebarCollapseButton : Model -> Html Msg
viewMainSidebarCollapseButton model =
div
[ class "collapse-sidebar-button" ]
[ Button.icon ToggleSidebar
(if model.sidebarToggled then
Icon.chevronRight
else
Icon.chevronLeft
)
|> Button.small
|> Button.view
]
subMenu : List ( String, Click Msg )
subMenu =
[ ( "Unison website", ExternalHref "https://unisonweb.org" )
, ( "Docs", ExternalHref "https://unisonweb.org/docs" )
, ( "Language Reference", ExternalHref "https://unisonweb.org/docs/language-reference" )
, ( "Community", ExternalHref "https://unisonweb.org/community" )
, ( "Report a bug", OnClick (ShowModal AppModal.ReportBugModal) )
]
unisonSubmenu : Html Msg
unisonSubmenu =
Tooltip.tooltip
(Icon.unisonMark
|> Icon.withClass "sidebar-unison-submenu"
|> Icon.view
)
(Tooltip.textMenu subMenu)
|> Tooltip.withPosition Tooltip.RightOf
|> Tooltip.withArrow Tooltip.End
|> Tooltip.view
viewMainSidebar : Model -> List (Html Msg)
viewMainSidebar model =
let
perspective =
model.env.perspective
changePerspectiveMsg =
Perspective.toNamespacePerspective perspective >> ChangePerspective
sidebarContent =
if Perspective.isCodebasePerspective perspective then
let
base =
FQN.fromString "unison.base"
in
Sidebar.section "Popular libraries"
[ Sidebar.item (changePerspectiveMsg base) (FQN.toString base)
]
else
UI.nothing
in
[ viewMainSidebarCollapseButton model
, div [ class "expanded-content" ]
[ viewSidebarHeader model.env
, div [ class "sidebar-scroll-area" ]
[ sidebarContent
, Sidebar.section
"Namespaces and Definitions"
[ Html.map CodebaseTreeMsg (CodebaseTree.view model.codebaseTree) ]
, nav []
(List.map
(\( l, c ) -> Click.view [] [ text l ] c)
subMenu
++ [ a [ class "show-keyboard-shortcuts", onClick (ShowModal AppModal.KeyboardShortcutsModal) ]
[ text "Keyboard Shortcuts"
, KeyboardShortcut.view model.keyboardShortcut (KeyboardShortcut.single QuestionMark)
]
]
)
]
]
, div [ class "collapsed-content" ]
[ unisonSubmenu
, Tooltip.tooltip
(a
[ class "show-help-collapsed", onClick (ShowModal AppModal.KeyboardShortcutsModal) ]
[ KeyboardShortcut.view model.keyboardShortcut (KeyboardShortcut.single QuestionMark)
]
)
(Tooltip.Text "Keyboard Shortcuts")
|> Tooltip.withPosition Tooltip.RightOf
|> Tooltip.withArrow Tooltip.Middle
|> Tooltip.view
]
]
viewAppLoading : Html msg
viewAppLoading =
div [ id "app" ]
[ AppHeader.view (AppHeader.appHeader (appTitle Nothing))
, PageLayout.view
(PageLayout.FullLayout
{ content = PageLayout.PageContent [] }
)
]
viewAppError : Http.Error -> Html msg
viewAppError error =
div [ id "app" ]
[ AppHeader.view (AppHeader.appHeader (appTitle Nothing))
, PageLayout.view
(PageLayout.FullLayout
{ content =
PageLayout.PageContent
[ div [ class "app-error" ]
[ Icon.view Icon.warn
, p [ title (Api.errorToString error) ]
[ text "Unison Share could not be started." ]
]
]
}
)
]
view : Model -> Browser.Document Msg
view model =
let
appHeader =
AppHeader.view (viewAppHeader model)
withSidebar pageContent =
PageLayout.SidebarLayout
{ sidebar = viewMainSidebar model
, sidebarToggled = model.sidebarToggled
, content = PageLayout.PageContent [ pageContent ]
}
page =
case model.route of
Route.Catalog ->
Html.map CatalogMsg (Catalog.view model.catalog)
Route.Project _ Route.ProjectRoot ->
Html.map PerspectiveLandingMsg
(PerspectiveLanding.view
model.env
model.perspectiveLanding
)
|> withSidebar
|> PageLayout.view
Route.Project _ (Route.ProjectDefinition _) ->
Html.map WorkspaceMsg (Workspace.view model.workspace)
|> withSidebar
|> PageLayout.view
in
{ title = "Unison Share"
, body =
[ div [ id "app" ]
[ appHeader
, page
, Html.map AppModalMsg (AppModal.view model.env model.appModal)
]
]
}