From 0e8978a31ab956b4840c1854b4979e2e3ad74109 Mon Sep 17 00:00:00 2001 From: davidvader Date: Fri, 2 Feb 2024 10:22:28 -0600 Subject: [PATCH] refactor: favicons --- src/elm/Effect.elm | 8 ++++++- src/elm/Layouts/Default.elm | 3 ++- src/elm/Layouts/Default/Build.elm | 8 +++++-- src/elm/Shared.elm | 28 ++++++++++++++--------- src/elm/Shared/Msg.elm | 7 +++--- src/elm/Utils/Favicons.elm | 37 +++++-------------------------- 6 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/elm/Effect.elm b/src/elm/Effect.elm index 9be88f1af..74ad84c3b 100644 --- a/src/elm/Effect.elm +++ b/src/elm/Effect.elm @@ -9,7 +9,7 @@ module Effect exposing , sendCmd, sendMsg , pushRoute, replaceRoute, loadExternalUrl , map, toCmd - , addAlertError, addAlertSuccess, addDeployment, addOrgSecret, addRepoSchedule, addRepoSecret, addSharedSecret, alertsUpdate, chownRepo, clearRedirect, deleteOrgSecret, deleteRepoSchedule, deleteRepoSecret, deleteSharedSecret, disableRepo, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedule, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecret, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, repairRepo, replacePath, setRedirect, setTheme, updateFavorites, updateOrgSecret, updateRepo, updateRepoSchedule, updateRepoSecret, updateSharedSecret + , addAlertError, addAlertSuccess, addDeployment, addOrgSecret, addRepoSchedule, addRepoSecret, addSharedSecret, alertsUpdate, chownRepo, clearRedirect, deleteOrgSecret, deleteRepoSchedule, deleteRepoSecret, deleteSharedSecret, disableRepo, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedule, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecret, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, repairRepo, replacePath, setRedirect, setTheme, updateFavicon, updateFavorites, updateOrgSecret, updateRepo, updateRepoSchedule, updateRepoSecret, updateSharedSecret ) {-| @@ -40,6 +40,7 @@ import Shared.Msg import Task import Toasty as Alerting import Url exposing (Url) +import Utils.Favicons as Favicons import Utils.Favorites as Favorites import Utils.Theme as Theme import Vela @@ -1141,6 +1142,11 @@ getRepoBuilds options = |> sendCmd +updateFavicon : { favicon : Favicons.Favicon } -> Effect msg +updateFavicon options = + SendSharedMsg <| Shared.Msg.UpdateFavicon options + + handleHttpError : { httpError : Http.Detailed.Error String } -> Effect msg handleHttpError options = SendSharedMsg <| Shared.Msg.HandleHttpError options.httpError diff --git a/src/elm/Layouts/Default.elm b/src/elm/Layouts/Default.elm index a7cc20643..e965c9ecb 100644 --- a/src/elm/Layouts/Default.elm +++ b/src/elm/Layouts/Default.elm @@ -26,6 +26,7 @@ import RemoteData exposing (WebData) import Route exposing (Route) import Shared import Toasty as Alerting +import Utils.Favicons as Favicons import Utils.Favorites as Favorites import Utils.Helpers as Util import Utils.Theme as Theme @@ -76,7 +77,7 @@ init shared _ = ( { showIdentity = False , showHelp = False } - , Effect.none + , Effect.updateFavicon { favicon = Favicons.defaultFavicon } ) diff --git a/src/elm/Layouts/Default/Build.elm b/src/elm/Layouts/Default/Build.elm index e0f5fd4b2..b442c4c77 100644 --- a/src/elm/Layouts/Default/Build.elm +++ b/src/elm/Layouts/Default/Build.elm @@ -23,6 +23,7 @@ import Route.Path import Shared import Time import Utils.Errors +import Utils.Favicons as Favicons import Utils.Interval as Interval import Vela import View exposing (View) @@ -150,12 +151,15 @@ update props shared msg model = ( { model | build = RemoteData.Success build } - , Effect.none + , Effect.updateFavicon { favicon = Favicons.statusToFavicon build.status } ) Err error -> ( { model | build = Utils.Errors.toFailure error } - , Effect.handleHttpError { httpError = error } + , Effect.batch + [ Effect.handleHttpError { httpError = error } + , Effect.updateFavicon { favicon = Favicons.statusToFavicon Vela.Error } + ] ) -- REFRESH diff --git a/src/elm/Shared.elm b/src/elm/Shared.elm index 72e151c5d..30e13c25d 100644 --- a/src/elm/Shared.elm +++ b/src/elm/Shared.elm @@ -20,7 +20,6 @@ import Http.Detailed import Interop import Json.Decode import Json.Decode.Pipeline exposing (required) -import Json.Encode import RemoteData import Route exposing (Route) import Route.Path @@ -113,6 +112,10 @@ init flagsResult route = |> Interop.setTheme |> Effect.sendCmd + setFavicon = + Effect.updateFavicon + { favicon = Favicons.defaultFavicon } + fetchInitialToken = if String.length flags.velaRedirect == 0 then Effect.sendCmd <| Api.try Shared.Msg.TokenResponse <| Api.Operations.getToken flags.velaAPI @@ -170,12 +173,8 @@ init flagsResult route = [ setTimeZone , setTime , setTheme + , setFavicon , fetchInitialToken - - -- need to reference these interops to let the app load properly - -- this should be removed when build graph and refresh logic are implemented - , Interop.renderBuildGraph Json.Encode.null |> Effect.sendCmd - , Interop.setFavicon Json.Encode.null |> Effect.sendCmd ] ) @@ -346,7 +345,10 @@ update route msg model = Auth.Session.Authenticated _ -> [ redirectToLogin , Effect.setRedirect { redirect = velaRedirect } - , Effect.addAlertError { content = "Your session has expired or you logged in somewhere else, please log in again.", addToastIfUnique = True } + , Effect.addAlertError + { content = "Your session has expired or you logged in somewhere else, please log in again." + , addToastIfUnique = True + } ] in ( { model @@ -527,10 +529,6 @@ update route msg model = , Effect.handleHttpError { httpError = error } ) - -- BUILD GRAPH - Shared.Msg.BuildGraphInteraction _ -> - ( model, Effect.none ) - -- THEME Shared.Msg.SetTheme options -> if options.theme == model.theme then @@ -612,6 +610,14 @@ update route msg model = Interval.FiveSeconds -> ( model, Effect.none ) + -- FAVICON + Shared.Msg.UpdateFavicon options -> + let + ( newFavicon, updateFavicon ) = + Favicons.updateFavicon model.favicon options.favicon + in + ( { model | favicon = newFavicon }, updateFavicon |> Effect.sendCmd ) + subscriptions : Route () -> Model -> Sub Msg subscriptions _ model = diff --git a/src/elm/Shared/Msg.elm b/src/elm/Shared/Msg.elm index a4d7f690f..ee25a10a0 100644 --- a/src/elm/Shared/Msg.elm +++ b/src/elm/Shared/Msg.elm @@ -13,6 +13,7 @@ import Http import Http.Detailed import Time import Toasty as Alerting +import Utils.Favicons as Favicons import Utils.Favorites as Favorites import Utils.Interval as Interval import Utils.Theme as Theme @@ -49,8 +50,6 @@ type Msg -- HOOKS | GetRepoHooks { org : String, repo : String, pageNumber : Maybe Int, perPage : Maybe Int, maybeEvent : Maybe String } | GetRepoHooksResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Hook )) - -- BUILD GRAPH - | BuildGraphInteraction Vela.BuildGraphInteraction -- THEME | SetTheme { theme : Theme.Theme } -- ALERTS @@ -59,5 +58,7 @@ type Msg | AlertsUpdate (Alerting.Msg Alert) -- ERRORS | HandleHttpError (Http.Detailed.Error String) - --REFRESH + -- REFRESH | Tick { interval : Interval.Interval, time : Time.Posix } + -- FAVICON + | UpdateFavicon { favicon : Favicons.Favicon } diff --git a/src/elm/Utils/Favicons.elm b/src/elm/Utils/Favicons.elm index 118d70ece..9641382b0 100644 --- a/src/elm/Utils/Favicons.elm +++ b/src/elm/Utils/Favicons.elm @@ -3,19 +3,14 @@ SPDX-License-Identifier: Apache-2.0 --} -module Utils.Favicons exposing (..) +module Utils.Favicons exposing (Favicon, defaultFavicon, statusToFavicon, updateFavicon) import Interop import Json.Encode -import RemoteData exposing (WebData) import Url.Builder import Vela - --- STATUS FAVICONS - - type alias Favicon = String @@ -27,37 +22,17 @@ defaultFavicon = Url.Builder.absolute [ "images", "favicon.ico" ] [] -{-| setDefaultFavicon : restores the favicon to the default +{-| updateFavicon : sets the browser tab favicon -} -setDefaultFavicon : Favicon -> ( Favicon, Cmd msg ) -setDefaultFavicon currentFavicon = - if currentFavicon /= defaultFavicon then - ( defaultFavicon, Interop.setFavicon <| Json.Encode.string defaultFavicon ) +updateFavicon : Favicon -> Favicon -> ( Favicon, Cmd msg ) +updateFavicon currentFavicon newFavicon = + if currentFavicon /= newFavicon then + ( newFavicon, Interop.setFavicon <| Json.Encode.string newFavicon ) else ( currentFavicon, Cmd.none ) -{-| refreshBuildFavicon : updates the favicon, to be used on pages with status updates --} -refreshBuildFavicon : Favicon -> WebData Vela.Build -> ( Favicon, Cmd msg ) -refreshBuildFavicon currentFavicon build = - case build of - RemoteData.Success b -> - let - newFavicon = - statusToFavicon b.status - in - if currentFavicon /= newFavicon then - ( newFavicon, Interop.setFavicon <| Json.Encode.string newFavicon ) - - else - ( currentFavicon, Cmd.none ) - - _ -> - ( currentFavicon, Cmd.none ) - - {-| statusToFavicon : takes build status and returns absolute path to the appropriate favicon -} statusToFavicon : Vela.Status -> Favicon