Skip to content

Commit

Permalink
Merge branch 'hotfix/23.09-flattenlocalstorage'
Browse files Browse the repository at this point in the history
  • Loading branch information
myrho committed Oct 25, 2023
2 parents 7c8958e + 839ab49 commit 57c5a68
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
1 change: 1 addition & 0 deletions elm.json.base
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"elm-community/easing-functions": "2.0.0",
"elm-community/html-extra": "3.4.0",
"elm-community/intdict": "3.0.0",
"elm-community/json-extra": "4.3.0",
"elm-community/list-extra": "8.6.0",
"elm-community/maybe-extra": "5.3.0",
"elm-community/result-extra": "2.4.0",
Expand Down
27 changes: 19 additions & 8 deletions src/Config/UserSettings.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Config.UserSettings exposing (..)

import Config.Graph exposing (AddressLabelType(..), TxLabelType(..))
import Json.Decode as Decode exposing (Decoder, bool, nullable, string)
import Json.Decode.Extra
import Json.Decode.Pipeline exposing (optional, required)
import Json.Encode
import Model.Currency exposing (Currency(..))
Expand Down Expand Up @@ -116,18 +117,28 @@ stringToEdgeLabel s =
Value


fromString : Decoder a -> Decoder a
fromString dec =
Decode.string
|> Decode.andThen
(Decode.decodeString dec
>> Result.mapError Decode.errorToString
>> Json.Decode.Extra.fromResult
)


decoder : Decoder UserSettings
decoder =
Decode.succeed UserSettings
|> required "selectedLanguage" string
|> optional "lightMode" (nullable bool) Nothing
|> optional "valueDetail" (nullable (Decode.string |> Decode.map stringToValueDetail)) Nothing
|> optional "valueDenomination" (nullable (Decode.string |> Decode.map stringToCurrency)) Nothing
|> optional "addressLabel" (nullable (Decode.string |> Decode.map stringToAddressLabel)) Nothing
|> optional "edgeLabel" (nullable (Decode.string |> Decode.map stringToEdgeLabel)) Nothing
|> optional "showAddressShadowLinks" (nullable bool) Nothing
|> optional "showClusterShadowLinks" (nullable bool) Nothing
|> optional "showDatesInUserLocale" (nullable bool) Nothing
|> optional "lightMode" (nullable bool |> fromString) Nothing
|> optional "valueDetail" (Decode.string |> Decode.map stringToValueDetail |> nullable) Nothing
|> optional "valueDenomination" (Decode.string |> Decode.map stringToCurrency |> nullable) Nothing
|> optional "addressLabel" (Decode.string |> Decode.map stringToAddressLabel |> nullable) Nothing
|> optional "edgeLabel" (Decode.string |> Decode.map stringToEdgeLabel |> nullable) Nothing
|> optional "showAddressShadowLinks" (nullable bool |> fromString) Nothing
|> optional "showClusterShadowLinks" (nullable bool |> fromString) Nothing
|> optional "showDatesInUserLocale" (nullable bool |> fromString) Nothing


encoder : UserSettings -> Json.Encode.Value
Expand Down
3 changes: 2 additions & 1 deletion src/Effect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ perform plugins key statusbarToken apiKey effect =
Ports.setDirty False

SaveUserSettingsEffect model ->
Ports.saveToLocalStorage ( "gs_user_settings", Config.UserSettings.encoder model )
Config.UserSettings.encoder model
|> Ports.saveToLocalStorage

ApiEffect eff ->
Effect.Api.perform apiKey (BrowserGotResponseWithHeaders statusbarToken) eff
Expand Down
4 changes: 3 additions & 1 deletion src/Init.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ init : Plugins -> Flags -> Url -> key -> ( Model key, List Effect )
init plugins flags url key =
let
settings =
Json.Decode.decodeValue Config.UserSettings.decoder flags.settings |> Result.withDefault Config.UserSettings.default
flags.localStorage
|> Json.Decode.decodeValue Config.UserSettings.decoder
|> Result.withDefault Config.UserSettings.default

( locale, localeEffect ) =
Locale.init settings
Expand Down
2 changes: 1 addition & 1 deletion src/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Url exposing (Url)


type alias Flags =
{ settings : Json.Encode.Value
{ localStorage : Json.Encode.Value
, now : Int
, width : Int
, height : Int
Expand Down
2 changes: 1 addition & 1 deletion src/Ports.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ port copyToClipboard : String -> Cmd msg
port setDirty : Bool -> Cmd msg


port saveToLocalStorage : ( String, Value ) -> Cmd msg
port saveToLocalStorage : Value -> Cmd msg



Expand Down
31 changes: 13 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ const getNavigatorLanguage = () => {

const locale = getNavigatorLanguage().split('-')[0]

let userSettings = localStorage.getItem("gs_user_settings");

let settings;
if (typeof userSettings !== 'undefined' && userSettings !== null){
settings = JSON.parse(userSettings)
} else {
settings = { "selectedLanguage": locale}
}

const docElem = document.documentElement
const body = document.getElementsByTagName('body')[0]
const width = window.innerWidth || docElem.clientWidth || body.clientWidth
Expand All @@ -37,7 +28,15 @@ for (const plugin in plugins) {
pluginFlags[plugin] = plugins[plugin].flags()
}

const app = Elm.Main.init({ flags: { settings, width, height, now, pluginFlags } })
const app = Elm.Main.init(
{ flags:
{ localStorage: {...localStorage}
, width
, height
, now
, pluginFlags
}
})

let isDirty = false

Expand Down Expand Up @@ -167,12 +166,8 @@ app.ports.setDirty.subscribe(dirty => {
isDirty = dirty
});

app.ports.saveToLocalStorage.subscribe(data_tuple => {
let k, v;
[k, v] = data_tuple;
localStorage.setItem(k, JSON.stringify(v));
app.ports.saveToLocalStorage.subscribe(data => {
for(let k in data) {
localStorage.setItem(k, data[k]);
}
});

// app.ports.loadFromLocalStorage.subscribe((key, value) => {
// app.ports.load.send(localStorage.getItem(key, value));
// });

0 comments on commit 57c5a68

Please sign in to comment.