module Components.Progress exposing (Category(..), Context, Data, Item, Model, Msg(..), System(..), changeLocale, defaultSystem, enter, fromJlpt, hskDecoder, initial, jlptKanjiDecoder, jlptVocabDecoder, key, update, view) import Dict exposing (Dict) import Html exposing (Html, a, button, details, div, h2, h3, li, p, section, span, summary, text, ul) import Html.Attributes as Attr exposing (class, href, title, type_) import Html.Events exposing (onClick) import Http import Json.Decode as Decode exposing (Decoder) import LucideIcons import String import Svg.Attributes as SvgAttr import Utils.Persistence exposing (Snapshot) import Utils.Subjects as Subjects exposing (Subject) type System = Hsk | Tocfl | Jlpt type Category = Characters | Vocabulary type alias Item = { level : Int, slug : String } type alias Data = { chars : List Item, vocab : List Item } type alias Count = { learned : Int, total : Int } type alias Model = { level : Maybe Int , system : System , selectedGrade : Maybe ( Category, Int ) , lists : Dict String (Result String Data) , jlptKanji : Maybe (Result String (List Item)) , jlptVocab : Maybe (Result String (List Item)) } type Msg = SelectLevel Int | SelectSystem System | SelectGrade Category Int | Loaded System (Result Http.Error Data) | LoadedJlptKanji (Result Http.Error (List Item)) | LoadedJlptVocab (Result Http.Error (List Item)) initial : Model initial = { level = Nothing , system = Hsk , selectedGrade = Nothing , lists = Dict.empty , jlptKanji = Nothing , jlptVocab = Nothing } {-| Ensure the selected list is loaded without discarding cached lists. Called when the dashboard becomes available, not during application startup. -} enter : (Msg -> msg) -> Model -> ( Model, Cmd msg ) enter toMsg model = if Dict.member (key model.system) model.lists then ( model, Cmd.none ) else ( { model | lists = Dict.insert (key model.system) (Err "loading") model.lists } , load toMsg model.system ) {-| Reset locale-specific selections while retaining immutable list data. The application decides when the subject set changed; this feature decides how its own state responds. -} changeLocale : String -> Model -> Model changeLocale locale model = { model | system = defaultSystem locale, level = Nothing, selectedGrade = Nothing } update : (Msg -> msg) -> Int -> Msg -> Model -> ( Model, Cmd msg ) update toMsg userLevel msg model = case msg of SelectLevel requested -> ( { model | level = Just (clamp 1 (max 1 userLevel) requested) }, Cmd.none ) SelectSystem system -> enter toMsg { model | system = system, selectedGrade = Nothing } SelectGrade category level -> ( { model | selectedGrade = if model.selectedGrade == Just ( category, level ) then Nothing else Just ( category, level ) } , Cmd.none ) Loaded system result -> ( { model | lists = Dict.insert (key system) (Result.mapError loadError result) model.lists } , Cmd.none ) LoadedJlptKanji result -> ( combineJlpt { model | jlptKanji = Just (Result.mapError loadError result) }, Cmd.none ) LoadedJlptVocab result -> ( combineJlpt { model | jlptVocab = Just (Result.mapError loadError result) }, Cmd.none ) combineJlpt : Model -> Model combineJlpt model = case ( model.jlptKanji, model.jlptVocab ) of ( Just kanji, Just vocab ) -> { model | lists = Dict.insert "JLPT" (Result.map2 fromJlpt kanji vocab) model.lists } _ -> model loadError : Http.Error -> String loadError _ = "progress_load_failed" load : (Msg -> msg) -> System -> Cmd msg load toMsg system = case system of Hsk -> Http.get { url = "/static/gen/progress/hsk.json" , expect = Http.expectJson (Loaded Hsk >> toMsg) hskDecoder } Tocfl -> Http.get { url = "/static/gen/progress/tocfl.json" , expect = Http.expectJson (Loaded Tocfl >> toMsg) hskDecoder } Jlpt -> Cmd.batch [ Http.get { url = "/static/gen/progress/jlpt-kanji.json" , expect = Http.expectJson (LoadedJlptKanji >> toMsg) jlptKanjiDecoder } , Http.get { url = "/static/gen/progress/jlpt-vocab.json" , expect = Http.expectJson (LoadedJlptVocab >> toMsg) jlptVocabDecoder } ] {-| Shared inputs are supplied, never copied into the feature's model. -} type alias Context = { locale : String , subjects : List Subject , snapshot : Snapshot , tr : String -> String } view : (Msg -> msg) -> Context -> Model -> Html msg view toMsg context model = viewDetails { level = Maybe.withDefault (max 1 context.snapshot.level) model.level , userLevel = context.snapshot.level , locale = context.locale , subjects = context.subjects , snapshot = context.snapshot , system = model.system , progress = Dict.get (key model.system) model.lists |> Maybe.withDefault (Err "loading") , selectedGrade = model.selectedGrade , onLevel = SelectLevel >> toMsg , onSystem = SelectSystem >> toMsg , onGrade = \category level -> toMsg (SelectGrade category level) , tr = context.tr } type alias Options msg = { level : Int , userLevel : Int , locale : String , subjects : List Subject , snapshot : Snapshot , system : System , progress : Result String Data , selectedGrade : Maybe ( Category, Int ) , onLevel : Int -> msg , onSystem : System -> msg , onGrade : Category -> Int -> msg , tr : String -> String } key : System -> String key system = case system of Hsk -> "HSK" Tocfl -> "TOCFL" Jlpt -> "JLPT" defaultSystem : String -> System defaultSystem locale = if locale == "ja" then Jlpt else if locale == "zh_TW" then Tocfl else Hsk hskDecoder : Decoder Data hskDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "traditional" Decode.string) ) |> Decode.map (\items -> { chars = List.filter (\item -> String.length item.slug == 1) items , vocab = List.filter (\item -> String.length item.slug > 1) items } ) jlptKanjiDecoder : Decoder (List Item) jlptKanjiDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "kanji" Decode.string) ) jlptVocabDecoder : Decoder (List Item) jlptVocabDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "chars" Decode.string) ) fromJlpt : List Item -> List Item -> Data fromJlpt chars vocab = { chars = chars, vocab = vocab } viewDetails : Options msg -> Html msg viewDetails options = let bySlug = Dict.fromList (List.map (\subject -> ( subject.slug, subject )) options.subjects) levelTiles kind = options.subjects |> List.filter (\subject -> subject.level == options.level && subject.kind == kind) |> List.sortBy .position |> List.map (tile options.snapshot) systemButton system = button [ type_ "button" , class (if system == options.system then "selected" else "" ) , onClick (options.onSystem system) ] [ text (options.tr (String.toLower (key system))) ] learned kind = options.subjects |> List.filter (\subject -> subject.kind == kind) |> List.filter (\subject -> Dict.get subject.id options.snapshot.assignments |> Maybe.map (\assignment -> assignment.startedAt /= Nothing) |> Maybe.withDefault False ) |> List.length in div [ class "progress-page" ] [ section [ class "level-stats" ] [ div [ class "level-heading" ] [ h2 [] [ text (options.tr "level" ++ " " ++ String.fromInt options.level) ] , div [] [ button [ type_ "button", Attr.attribute "aria-label" (options.tr "previous_level"), Attr.disabled (options.level <= 1), onClick (options.onLevel (options.level - 1)) ] [ arrow "arrow-left" ] , button [ type_ "button", Attr.attribute "aria-label" (options.tr "next_level"), Attr.disabled (options.level >= max 1 options.userLevel), onClick (options.onLevel (options.level + 1)) ] [ arrow "arrow-right" ] ] ] , div [ class "totals" ] [ h2 [] [ text (options.tr "total_learned") ] , if List.any (\subject -> subject.kind == "Radical") options.subjects then p [] [ text (options.tr "radicals" ++ ": " ++ String.fromInt (learned "Radical")) ] else text "" , p [] [ text (options.tr "characters" ++ ": " ++ String.fromInt (learned "Character")) ] , p [] [ text (options.tr "vocabulary" ++ ": " ++ String.fromInt (learned "Vocabulary")) ] ] , levelSection (options.tr "radicals") (levelTiles "Radical") , levelSection (options.tr "characters") (levelTiles "Character") , levelSection (options.tr "vocabulary") (levelTiles "Vocabulary") ] , section [ class "grade-system" ] [ h2 [ class "sr-only" ] [ text (options.tr "proficiency_lists") ] , div [ class "progress-tabs" ] (if options.locale == "ja" then [ systemButton Jlpt ] else [ systemButton Hsk, systemButton Tocfl ] ) , case options.progress of Err message -> p [] [ text (options.tr message) ] Ok data -> div [] [ div [ class "stats" ] [ div [ class "levels" ] [ h3 [] [ text (options.tr "characters") ] , gradeRows options bySlug Characters data.chars ] , div [ class "levels" ] [ h3 [] [ text (options.tr "vocabulary") ] , gradeRows options bySlug Vocabulary data.vocab ] ] , case options.selectedGrade of Nothing -> text "" Just ( category, level ) -> let items = if category == Characters then data.chars else data.vocab in levelSection "" (items |> List.filter (\item -> item.level == level) |> List.filterMap (\item -> Dict.get item.slug bySlug) |> List.map (tile options.snapshot) ) ] ] ] arrow : String -> Html msg arrow name = let options = [ SvgAttr.class "ui-icon" ] in case name of "arrow-left" -> LucideIcons.arrowLeftIcon options "arrow-right" -> LucideIcons.arrowRightIcon options _ -> span [ class "ui-icon" ] [] gradeLabel : Options msg -> Int -> String gradeLabel options level = (case options.system of Hsk -> options.tr "hsk" Tocfl -> options.tr "tocfl" Jlpt -> "N" ) ++ " " ++ String.fromInt level levelSection : String -> List (Html msg) -> Html msg levelSection heading tiles = if List.isEmpty tiles then text "" else if heading /= "" then details [ class "progress-section", Attr.attribute "open" "" ] [ summary [] [ text heading ] , div [ class "subject-grid" ] tiles ] else div [ class "progress-section", Attr.attribute "open" "" ] [ div [ class "subject-grid" ] tiles ] tile : Snapshot -> Subject -> Html msg tile snapshot subject = let assignment = Dict.get subject.id snapshot.assignments status = case assignment of Just value -> if value.completedAt /= Nothing || value.markedCompleted || value.passedAt /= Nothing then "passed" else if value.startedAt /= Nothing then "started" else "locked" Nothing -> "locked" in a [ href ("#!/reference/subject/" ++ subject.id) , class ("subject-tile " ++ String.toLower subject.kind ++ " " ++ status) , title (Subjects.primaryMeaning subject) ] [ text subject.character ] gradeRows : Options msg -> Dict String Subject -> Category -> List Item -> Html msg gradeRows options bySlug category items = let tally item current = let prior = Dict.get item.level current |> Maybe.withDefault { learned = 0, total = 0 } studied = Dict.get item.slug bySlug |> Maybe.andThen (\subject -> Dict.get subject.id options.snapshot.assignments) |> Maybe.map (\assignment -> assignment.startedAt /= Nothing) |> Maybe.withDefault False in Dict.insert item.level { learned = prior.learned + (if studied then 1 else 0 ) , total = prior.total + 1 } current groups = List.foldl tally Dict.empty items |> Dict.toList ordered = if options.system == Jlpt then List.reverse groups else groups in ul [ class "grade-list" ] (List.map (\( level, counts ) -> li [] [ button [ type_ "button" , class (if options.selectedGrade == Just ( category, level ) then "grade-level selected " ++ (if category == Characters then "character" else "vocabulary" ) else "grade-level" ) , onClick (options.onGrade category level) ] [ span [ class "bold" ] [ text (gradeLabel options level ++ ": ") ] , span [] [ text (String.fromInt (round (toFloat counts.learned / toFloat counts.total * 100)) ++ "% (" ++ String.fromInt counts.learned ++ "/" ++ String.fromInt counts.total ++ ")" ) ] ] ] ) ordered )