Skip to content

Commit a55ae31

Browse files
committed
fix: global sidebar_state and reactivity
1 parent b74052e commit a55ae31

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

frontend/src/lib/components/sidebar.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import { createSidebarStore } from '$lib/stores/sidebar.svelte';
66
77
const sidebarStore = createSidebarStore();
8-
9-
$inspect(sidebarStore.state);
108
</script>
119

1210
<div
@@ -59,7 +57,7 @@
5957
{/each}
6058
</div>
6159
{:else}
62-
<span class="text-sm font-medium">quite! check some.</span>
60+
<span class="text-sm font-medium">Just in—take a peek.</span>
6361
{/if}
6462
</div>
6563
<div class="collapse gap-2 overflow-visible rounded-none">
@@ -87,7 +85,7 @@
8785
{/each}
8886
</div>
8987
{:else}
90-
<span class="text-sm font-medium">join some.</span>
88+
<span class="text-sm font-medium">Ready to quibble? Join in.</span>
9189
{/if}
9290
</div>
9391
<div class="collapse gap-2 overflow-visible rounded-none">

frontend/src/lib/stores/sidebar.svelte.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,38 @@ type IQuiblets = {
88
starred: boolean;
99
}[];
1010

11-
export function createSidebarStore() {
12-
const stored_sidebar_store = browser ? localStorage.getItem('sidebar_store') : null;
11+
const stored_sidebar_store = browser ? localStorage.getItem('sidebar_store') : null;
1312

14-
const parsed_stored_quiblets: ISidebarStore = stored_sidebar_store
15-
? JSON.parse(stored_sidebar_store)
16-
: {};
13+
const parsed_stored_quiblets: ISidebarStore = stored_sidebar_store
14+
? JSON.parse(stored_sidebar_store)
15+
: {};
1716

18-
const sidebar_state = $state<ISidebarStore>(
19-
// sort initial data
20-
Object.fromEntries(
21-
Object.entries(parsed_stored_quiblets).map(([key, quiblets]) => [
22-
key,
23-
sort_quiblets(quiblets)
24-
])
25-
)
26-
);
17+
const sidebar_state = $state<ISidebarStore>(
18+
// sort initial data
19+
Object.fromEntries(
20+
Object.entries(parsed_stored_quiblets).map(([key, quiblets]) => [
21+
key,
22+
sort_quiblets(quiblets)
23+
])
24+
)
25+
);
2726

28-
function sync_localstorage() {
29-
if (browser) {
30-
localStorage.setItem('sidebar_store', JSON.stringify(sidebar_state));
31-
}
27+
function sync_localstorage() {
28+
if (browser) {
29+
localStorage.setItem('sidebar_store', JSON.stringify(sidebar_state));
3230
}
31+
}
3332

34-
function sort_quiblets(quiblets: IQuiblets) {
35-
return [...quiblets].sort((a, b) => {
36-
if (a.starred !== b.starred) {
37-
return b.starred ? 1 : -1;
38-
}
39-
return a.name.localeCompare(b.name);
40-
});
41-
}
33+
function sort_quiblets(quiblets: IQuiblets) {
34+
return [...quiblets].sort((a, b) => {
35+
if (a.starred !== b.starred) {
36+
return b.starred ? 1 : -1;
37+
}
38+
return a.name.localeCompare(b.name);
39+
});
40+
}
4241

42+
export function createSidebarStore() {
4343
return {
4444
get state() {
4545
return sidebar_state;
@@ -57,6 +57,10 @@ export function createSidebarStore() {
5757
if (exists) return;
5858

5959
sidebar_state[type] = sort_quiblets([...state, quiblet]);
60+
// sidebar_state = {
61+
// ...sidebar_state,
62+
// [type]: sort_quiblets([...state, quiblet])
63+
// }
6064
sync_localstorage();
6165
},
6266
toggle_star(name: string, type: string) {

0 commit comments

Comments
 (0)