Skip to content

Commit

Permalink
Add global error handler and fix elm url error
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Nov 15, 2024
1 parent 0f6d814 commit 91863ee
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 55 deletions.
13 changes: 5 additions & 8 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ 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
|> Decode.decodeValue (Decode.nullable WorkspaceContext.decode)
|> 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
}
7 changes: 2 additions & 5 deletions src/Ucm/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -53,8 +52,6 @@ init appContext workspaceContext _ =

type Msg
= NoOp
| UrlRequest Browser.UrlRequest
| UrlChange Url
| WelcomeScreenMsg WelcomeScreen.Msg
| WorkspaceScreenMsg WorkspaceScreen.Msg
| CheckUCMConnectivity
Expand Down
7 changes: 2 additions & 5 deletions src/Ucm/AppContext.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Ucm.AppContext exposing (..)

import Browser.Navigation as Nav
import Code.Config
import Code.Perspective as Perspective
import Http
Expand All @@ -20,7 +19,6 @@ type alias AppContext =
{ operatingSystem : OperatingSystem
, basePath : String
, api : HttpApi
, navKey : Nav.Key
, ucmConnected : UCMConnectivity
}

Expand All @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions src/css/app-error.css
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
@@ -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";

99 changes: 62 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<WorkspaceContext>>('workspace-contexts') || []);
const workspaceContexts = (await store.get<Array<WorkspaceContext>>('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);
}
}

0 comments on commit 91863ee

Please sign in to comment.