Notify user if imported file or directory doesn't contain token files

This commit is contained in:
Andrey Fedorov 2025-06-20 09:50:00 +02:00 committed by Andrés Moya
parent 21fd56076c
commit 130cd52f79
5 changed files with 50 additions and 17 deletions

View File

@ -14,6 +14,10 @@
{:error/code :error.import/json-parse-error
:error/fn #(tr "workspace.tokens.error-parse")}
:error.import/no-token-files-found
{:error/code :error.import/no-token-files-found
:error/fn #(tr "workspace.tokens.no-token-files-found")}
:error.import/invalid-json-data
{:error/code :error.import/invalid-json-data
:error/fn #(tr "workspace.tokens.invalid-json")}

View File

@ -114,6 +114,23 @@
:on-click handle-option-click
:set-ref (fn [_] nil)}]]]))
(defn- has-token-files?
[file-paths]
(and (seq file-paths)
(some #(str/ends-with? % ".json") file-paths)))
(defn- validate-token-files
[file-stream]
(->> file-stream
(rx/reduce (fn [acc [file-path file-text]]
(conj acc [file-path file-text]))
[])
(rx/tap (fn [file-entries]
(let [file-paths (map first file-entries)]
(when-not (has-token-files? file-paths)
(throw (dwte/error-ex-info :error.import/no-token-files-found file-paths nil))))))
(rx/mapcat identity)))
(mf/defc import-modal-body*
{::mf/private true}
[]
@ -157,6 +174,7 @@
(rx/map (fn [file-text]
[(.-webkitRelativePath file)
file-text])))))
(validate-token-files)
(dwti/import-directory-stream)
(on-stream-imported "multiple"))
@ -171,23 +189,25 @@
(first))
zipfile-name (str/strip-suffix (.-name zipfile) ".zip")]
(->> (wapi/read-file-as-array-buffer zipfile)
(rx/mapcat (fn [file-content]
(let [zip-reader (uz/reader file-content)]
(->> (rx/from (uz/get-entries zip-reader))
(rx/mapcat
(fn [entries]
(->> (rx/from entries)
(rx/filter (fn [entry]
(let [filename (.-filename entry)]
(str/ends-with? filename ".json"))))
(rx/merge-map (fn [entry]
(let [filename (str/concat zipfile-name "/" (.-filename entry))
content-promise (uz/read-as-text entry)]
(-> content-promise
(.then (fn [text]
[filename text]))
(rx/from))))))))
(rx/finalize (partial uz/close zip-reader))))))
(rx/mapcat
(fn [file-content]
(let [zip-reader (uz/reader file-content)]
(->> (rx/from (uz/get-entries zip-reader))
(rx/mapcat
(fn [entries]
(->> (rx/from entries)
(rx/filter (fn [entry]
(let [filename (.-filename entry)]
(str/ends-with? filename ".json"))))
(rx/merge-map (fn [entry]
(let [filename (str/concat zipfile-name "/" (.-filename entry))
content-promise (uz/read-as-text entry)]
(-> content-promise
(.then (fn [text]
[filename text]))
(rx/from))))))))
(rx/finalize (partial uz/close zip-reader))))))
(validate-token-files)
(dwti/import-directory-stream)
(on-stream-imported "zip"))

View File

@ -39,6 +39,7 @@
}
.import-actions {
@include t.use-typography("body-small");
display: flex;
justify-content: flex-end;
gap: var(--sp-s);

View File

@ -7279,6 +7279,10 @@ msgstr "Token Set grouping is not supported yet."
msgid "workspace.tokens.import-error"
msgstr "Import Error:"
#: src/app/main/ui/workspace/tokens/modals/import.cljs
msgid "workspace.tokens.no-token-files-found"
msgstr "No tokens, sets, or themes were found in this file."
#: src/app/main/ui/workspace/tokens/modals/import.cljs:99
msgid "workspace.tokens.import-multiple-files"
msgstr "In multiple files, the file name / path are the set names."

View File

@ -7381,6 +7381,10 @@ msgstr "No hay temas."
msgid "workspace.tokens.no-themes-currently"
msgstr "Actualmente no existen temas."
#: src/app/main/ui/workspace/tokens/modals/import.cljs
msgid "workspace.tokens.no-tokens-found"
msgstr "No se han encontrado tokens, sets o temas en este fichero."
#: src/app/main/ui/workspace/tokens/modals/themes.cljs:148
msgid "workspace.tokens.num-active-sets"
msgstr "%s sets activos"