diff --git a/src/Main.elm b/src/Main.elm index afc4d86..5f6862d 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -13,8 +13,8 @@ import Url exposing (Url) -- PROGRAM -appInit : AppContext.Flags -> Url -> Nav.Key -> ( App.Model, Cmd App.Msg ) -appInit flags url navKey = +appInit : AppContext.Flags -> ( App.Model, Cmd App.Msg ) +appInit flags = let workspaceContext = flags.workspaceContext @@ -22,18 +22,15 @@ appInit flags url navKey = |> Result.withDefault Nothing in App.init - (AppContext.init flags navKey) + (AppContext.init flags) workspaceContext - url main : Program AppContext.Flags App.Model App.Msg main = - Browser.application + Browser.document { init = appInit - , update = App.update , view = App.view + , update = App.update , subscriptions = App.subscriptions - , onUrlRequest = App.UrlRequest - , onUrlChange = App.UrlChange } diff --git a/src/Ucm/App.elm b/src/Ucm/App.elm index 170e70e..7c30dcd 100644 --- a/src/Ucm/App.elm +++ b/src/Ucm/App.elm @@ -9,7 +9,6 @@ import Ucm.AppContext as AppContext exposing (AppContext) import Ucm.WelcomeScreen as WelcomeScreen exposing (OutMsg(..)) import Ucm.Workspace.WorkspaceContext exposing (WorkspaceContext) import Ucm.WorkspaceScreen as WorkspaceScreen -import Url exposing (Url) type Screen @@ -23,8 +22,8 @@ type alias Model = } -init : AppContext -> Maybe WorkspaceContext -> Url -> ( Model, Cmd Msg ) -init appContext workspaceContext _ = +init : AppContext -> Maybe WorkspaceContext -> ( Model, Cmd Msg ) +init appContext workspaceContext = let ( screen, cmd ) = case workspaceContext of @@ -53,8 +52,6 @@ init appContext workspaceContext _ = type Msg = NoOp - | UrlRequest Browser.UrlRequest - | UrlChange Url | WelcomeScreenMsg WelcomeScreen.Msg | WorkspaceScreenMsg WorkspaceScreen.Msg | CheckUCMConnectivity diff --git a/src/Ucm/AppContext.elm b/src/Ucm/AppContext.elm index 12670a9..29aafb0 100644 --- a/src/Ucm/AppContext.elm +++ b/src/Ucm/AppContext.elm @@ -1,6 +1,5 @@ module Ucm.AppContext exposing (..) -import Browser.Navigation as Nav import Code.Config import Code.Perspective as Perspective import Http @@ -20,7 +19,6 @@ type alias AppContext = { operatingSystem : OperatingSystem , basePath : String , api : HttpApi - , navKey : Nav.Key , ucmConnected : UCMConnectivity } @@ -33,12 +31,11 @@ type alias Flags = } -init : Flags -> Nav.Key -> AppContext -init flags navKey = +init : Flags -> AppContext +init flags = { operatingSystem = OS.fromString flags.operatingSystem , basePath = flags.basePath , api = HttpApi.httpApi False flags.apiUrl Nothing - , navKey = navKey , ucmConnected = Connected } diff --git a/src/css/app-error.css b/src/css/app-error.css new file mode 100644 index 0000000..4d9ae62 --- /dev/null +++ b/src/css/app-error.css @@ -0,0 +1,22 @@ +.app-error { + margin: 6rem auto; + width: 32rem; + height: 6rem; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + background: var(--u-color_critical_element_subdued); + color: var(--u-color_critical_text-on-element_subdued); + font-size: var(--font-size-base); + border-radius: var(--border-radius-base); + padding: 2rem; +} + +.app-error h2 { + color: var(--u-color_critical_text-on-element); +} + +.app-error p { + margin-bottom: 0; +} diff --git a/src/main.css b/src/main.css index d717937..8524fe5 100644 --- a/src/main.css +++ b/src/main.css @@ -1,7 +1,9 @@ @import "./css/unison-theme.css"; +@import "./css/app-error.css"; @import "./css/window.css"; @import "./css/ucm/search-project-sheet.css"; @import "./css/ucm/search-branch-sheet.css"; @import "./css/ucm/welcome-screen.css"; @import "./css/ucm/workspace-screen.css"; @import "./css/ucm/command-palette-modal.css"; + diff --git a/src/main.ts b/src/main.ts index 8476251..966a4ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,49 +23,74 @@ console.log("Starting UCM"); // @ts-ignore import { Elm } from './Main.elm'; -type WorkspaceContext = { - projectName: string; - branchRef: string -} -/* -console.log("Loading Store"); -const store = await Store.load('settings.json'); -console.log("Store loaded"); +try { + type WorkspaceContext = { + projectName: string; + branchRef: string + } + console.log("Loading Store"); + const store = await Store.load('settings.json'); + console.log("Store loaded"); -const workspaceContexts = (await store.get>('workspace-contexts') || []); + const workspaceContexts = (await store.get>('workspace-contexts') || []); -const unlisten = await getCurrentWindow().onCloseRequested(async (ev) => { - console.log("TODO"); - ev.preventDefault(); -}); + const unlisten = await getCurrentWindow().onCloseRequested(async (ev) => { + console.log("TODO"); + ev.preventDefault(); + }); -// you need to call unlisten if your handler goes out of scope e.g. the component is unmounted -unlisten(); -*/ + // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted + unlisten(); -// ELM STUFF + // ELM STUFF -const flags = { - operatingSystem: detectOs(window.navigator), - basePath: "", - apiUrl: "http://127.0.0.1:4444/asdf/api", - workspaceContext: undefined, // workspaceContexts[0], -}; + const flags = { + operatingSystem: detectOs(window.navigator), + basePath: "", + apiUrl: "http://127.0.0.1:4444/asdf/api", + workspaceContext: workspaceContexts[0], + }; -preventDefaultGlobalKeyboardEvents(); + preventDefaultGlobalKeyboardEvents(); -console.log("Starting Elm app"); -const app = Elm.Main.init({ flags }); -console.log("Elm app started"); + console.log("Starting Elm app"); + const app = Elm.Main.init({ flags }); + console.log("Elm app started"); -if (app.ports) { - app.ports.saveWorkspaceContext?.subscribe(async (_workspaceContext: WorkspaceContext) => { - /* - console.log("saving contexts", workspaceContext); - try { - await store.set("workspace-contexts", [workspaceContext]) - } catch (ex) { - console.error(ex); - }*/ - }); + if (app.ports) { + app.ports.saveWorkspaceContext?.subscribe(async (workspaceContext: WorkspaceContext) => { + console.log("saving contexts", workspaceContext); + try { + await store.set("workspace-contexts", [workspaceContext]) + } catch (ex) { + console.error(ex); + } + }); + } +} +catch (ex) { + console.error(ex); + + const $body = document.querySelector("body"); + const $err = document.createElement("div"); + $err.className = "app-error"; + const $errHeader = document.createElement("h2"); + $errHeader.innerHTML = "Could not start application"; + $err.appendChild($errHeader); + const $errMessage = document.createElement("p"); + + if ($body) { + if (typeof ex === "string") { + $errMessage.innerHTML = ex; + } + else if (ex instanceof Error) { + $errMessage.innerHTML = ex.message + } + else { + $errMessage.innerHTML = `An unknown error occured: ${ex}`; + } + + $err.appendChild($errMessage); + $body.appendChild($err); + } }