Skip to content

Commit

Permalink
Crude working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanWinther committed Oct 31, 2023
1 parent 1b274f7 commit 5b5c685
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 11 deletions.
20 changes: 18 additions & 2 deletions src/UnisonLocal/Api.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module UnisonLocal.Api exposing (codebaseApiEndpointToEndpoint, namespace)
module UnisonLocal.Api exposing (codebaseApiEndpointToEndpoint, namespace, projects, projectBranches)

import Code.BranchRef as BranchRef
import Code.CodebaseApi as CodebaseApi
Expand All @@ -14,7 +14,7 @@ import Lib.HttpApi exposing (Endpoint(..))
import Maybe.Extra as MaybeE
import Regex
import UnisonLocal.CodeBrowsingContext exposing (CodeBrowsingContext(..))
import UnisonLocal.ProjectName as ProjectName
import UnisonLocal.ProjectName as ProjectName exposing (ProjectName)
import Url.Builder exposing (QueryParameter, int, string)


Expand All @@ -30,6 +30,22 @@ namespace context perspective fqn =
}


projects : Endpoint
projects =
GET
{ path = [ "projects" ]
, queryParams = []
}


projectBranches : ProjectName -> Endpoint
projectBranches projectName =
GET
{ path = [ "projects", ProjectName.toApiString projectName, "branches" ]
, queryParams = []
}


codebaseApiEndpointToEndpoint : CodeBrowsingContext -> CodebaseApi.CodebaseEndpoint -> Endpoint
codebaseApiEndpointToEndpoint context cbEndpoint =
let
Expand Down
153 changes: 144 additions & 9 deletions src/UnisonLocal/Page/HomePage.elm
Original file line number Diff line number Diff line change
@@ -1,42 +1,177 @@
module UnisonLocal.Page.HomePage exposing (..)

import Code.BranchRef as BranchRef exposing (BranchSlug(..))
import Code.Perspective as Perspective
import Html exposing (li, p, text, ul)
import Json.Decode as Decode
import Json.Decode.Pipeline exposing (required)
import Lib.HttpApi as HttpApi
import Lib.Util as Util
import RemoteData exposing (RemoteData(..), WebData)
import UI.Click as Click
import UI.PageContent as PageContent
import UI.PageLayout as PageLayout exposing (PageFooter(..))
import UI.StatusBanner as StatusBanner
import UI.PageTitle as PageTitle
import UnisonLocal.Api as LocalApi
import UnisonLocal.AppContext exposing (AppContext)
import UnisonLocal.AppDocument as AppDocument exposing (AppDocument)
import UnisonLocal.AppHeader as AppHeader
import UnisonLocal.ProjectName as ProjectName exposing (ProjectName)
import UnisonLocal.Route as Route


type alias Model =
()
{ projects : List ProjectWithBranches }


type alias ProjectWithBranches =
{ projectName : ProjectName
, branches : List { branchName : BranchSlug }
}


init : AppContext -> ( Model, Cmd Msg )
init _ =
( (), Cmd.none )
init appContext =
let
fetchProjectsCmd =
fetchProjects FetchProjectsFinished
|> HttpApi.perform appContext.api
in
( { projects = [] }
, fetchProjectsCmd
)


fetchProjects :
(WebData (List { projectName : ProjectName }) -> msg)
-> HttpApi.ApiRequest (List { projectName : ProjectName }) msg
fetchProjects finishedMsg =
LocalApi.projects
|> HttpApi.toRequest decodeProjectList (RemoteData.fromResult >> finishedMsg)


fetchProjectBranches :
(WebData (List { branchName : BranchSlug }) -> msg)
-> ProjectName
-> HttpApi.ApiRequest (List { branchName : BranchSlug }) msg
fetchProjectBranches finishedMsg projectName =
LocalApi.projectBranches projectName
|> HttpApi.toRequest decodeBranchList (RemoteData.fromResult >> finishedMsg)


decodeProjectList : Decode.Decoder (List { projectName : ProjectName })
decodeProjectList =
let
makeProjectName projectName =
{ projectName = projectName }
in
Decode.succeed makeProjectName
|> required "projectName" ProjectName.decode
|> Decode.list


decodeBranchList : Decode.Decoder (List { branchName : BranchSlug })
decodeBranchList =
let
makeBranchName branchName =
{ branchName = branchName }
in
Decode.succeed makeBranchName
|> required "branchName" branchSlugDecode
|> Decode.list


branchSlugDecode : Decode.Decoder BranchSlug
branchSlugDecode =
Decode.map BranchRef.branchSlugFromString Decode.string
|> Decode.andThen (Util.decodeFailInvalid "Invalid BranchName")


type Msg
= NoOp
= FetchProjectsFinished (WebData (List { projectName : ProjectName }))
| FetchProjectBranchesFinished ProjectName (WebData (List { branchName : BranchSlug }))


update : AppContext -> Msg -> Model -> ( Model, Cmd Msg )
update _ _ model =
( model, Cmd.none )
update appContext msg model =
case msg of
FetchProjectsFinished (Success projects) ->
( { projects =
projects
|> List.map (\{ projectName } -> ProjectWithBranches projectName [])
}
, let
fetchProjectBranchesCmd projectName =
fetchProjectBranches (FetchProjectBranchesFinished projectName) projectName
|> HttpApi.perform appContext.api
in
projects
|> List.map (\{ projectName } -> fetchProjectBranchesCmd projectName)
|> Cmd.batch
)

FetchProjectsFinished _ ->
( model, Cmd.none )

FetchProjectBranchesFinished projectName (Success branches) ->
( { projects =
model.projects
|> List.map
(\project ->
if project.projectName == projectName then
ProjectWithBranches project.projectName branches

else
project
)
}
, Cmd.none
)

FetchProjectBranchesFinished _ _ ->
( model, Cmd.none )


view : Model -> AppDocument Msg
view _ =
view { projects } =
let
appHeader =
AppHeader.appHeader

projectList =
projects
|> List.map
(\{ projectName, branches } ->
let
defaultBranch =
List.head branches
|> Maybe.map (\{ branchName } -> branchName)
|> Maybe.withDefault (BranchSlug "main")
in
li []
[ Click.href
(String.join "/" [ "/projects", ProjectName.toString projectName, BranchRef.branchSlugToString defaultBranch ++ "/" ])
|> Click.view [] [ text <| ProjectName.toString projectName ]
]
)

nonProjectCodeParagraph =
p []
[ text "or "
, Route.nonProjectCodeRoot Perspective.relativeRootPerspective
|> Route.toUrlString
|> Click.href
|> Click.view [] [ text "view all non-project code" ]
, text "."
]

page =
PageLayout.centeredNarrowLayout
(PageContent.oneColumn
[ StatusBanner.info "Type `ui` from within a Project in UCM to view that project."
[ ul [] projectList
, nonProjectCodeParagraph
]
|> PageContent.withPageTitle (PageTitle.title "Open a project")
)
(PageFooter [])
|> PageLayout.withSubduedBackground
Expand Down

0 comments on commit 5b5c685

Please sign in to comment.