Skip to content

Commit

Permalink
fix: desktop local settings
Browse files Browse the repository at this point in the history
  • Loading branch information
feng19 committed Dec 4, 2024
1 parent de45c97 commit ca117a5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
32 changes: 28 additions & 4 deletions assets/svelte/dashboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,36 @@
true
);
let curr_settings = localStorage.getItem("x-trace-curr-settings");
if (curr_settings) {
live.pushEvent("apply-settings", { encoded: curr_settings, quiet: true });
// load current settings
if (window.__TAURI__) {
const { readTextFile, BaseDirectory } = window.__TAURI__.fs;
readTextFile("curr_settings.json", {
baseDir: BaseDirectory.AppData,
}).then((content) => {
if (content.length > 0) {
live.pushEvent("apply-settings", { encoded: content, quiet: true });
}
});
} else {
let curr_settings = localStorage.getItem("x-trace-curr-settings");
if (curr_settings) {
live.pushEvent("apply-settings", {
encoded: curr_settings,
quiet: true,
});
}
}
// save current settings
live.handleEvent("save-curr-settings", ({ encoded }) => {
localStorage.setItem("x-trace-curr-settings", encoded);
if (window.__TAURI__) {
const { writeTextFile, BaseDirectory } = window.__TAURI__.fs;
writeTextFile("curr_settings.json", encoded, {
baseDir: BaseDirectory.AppData,
});
} else {
localStorage.setItem("x-trace-curr-settings", encoded);
}
});
});
</script>
Expand Down
39 changes: 30 additions & 9 deletions assets/svelte/settings_local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@ import { toast } from "svelte-sonner";

function createStore() {
const store = writable({ selected: null, items: [] });
let dump = () => {
localStorage.setItem("x-trace-settings", JSON.stringify(get(store).items));
};
function load() {
if (window.__TAURI__) {
const { readTextFile, BaseDirectory } = window.__TAURI__.fs;
readTextFile("settings.json", { baseDir: BaseDirectory.AppData }).then(
(content) => {
if (content.length > 0) {
store.update((obj) => ({ ...obj, items: JSON.parse(content) }));
}
}
);
} else {
let all_settings = localStorage.getItem("x-trace-settings");
if (all_settings) {
store.update((obj) => ({ ...obj, items: JSON.parse(all_settings) }));
}
}
}

function dump() {
let content = JSON.stringify(get(store).items);
if (window.__TAURI__) {
const { writeTextFile, BaseDirectory } = window.__TAURI__.fs;
writeTextFile("settings.json", content, {
baseDir: BaseDirectory.AppData,
});
} else {
localStorage.setItem("x-trace-settings", content);
}
}

return {
subscribe: store.subscribe,
select: (id) => {
console.log("select id:", id);
store.update((obj) => ({ ...obj, selected: id }));
},
load: () => {
let all_settings = localStorage.getItem("x-trace-settings");
if (all_settings) {
store.update((obj) => ({ ...obj, items: JSON.parse(all_settings) }));
}
},
load: load,
dump: dump,
remove: (item) => {
store.update((obj) => ({
Expand Down
2 changes: 0 additions & 2 deletions lib/x_trace_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ defmodule XTraceWeb.CoreComponents do
Provides core UI components.
"""
use Phoenix.Component
# alias Phoenix.LiveView.JS

end
2 changes: 1 addition & 1 deletion lib/x_trace_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<main class="h-screen overflow-hidden">
<%= @inner_content %>
{@inner_content}
</main>
4 changes: 2 additions & 2 deletions lib/x_trace_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title suffix=" · Phoenix Framework">
<%= assigns[:page_title] || "XTrace" %>
{assigns[:page_title] || "XTrace"}
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white">
<%= @inner_content %>
{@inner_content}
</body>
</html>

0 comments on commit ca117a5

Please sign in to comment.