diff --git a/frontend/src/lib/components/sidebar.svelte b/frontend/src/lib/components/sidebar.svelte index 9fc597d..3ad7487 100644 --- a/frontend/src/lib/components/sidebar.svelte +++ b/frontend/src/lib/components/sidebar.svelte @@ -3,6 +3,11 @@ import Avatar from '$lib/components/ui/avatar.svelte'; import favorite_communities from '$lib/data/mock/favorites.json'; import your_communities from '$lib/data/mock/your_communities.json'; + import { createSidebarStore } from '$lib/stores/sidebar.svelte'; + + const sidebarStore = createSidebarStore(); + + $inspect(sidebarStore.state);
Create Quiblet
-
- -
- Recent - -
-
- {#each favorite_communities as community} - - {/each} + {#if sidebarStore.state.recent} +
+ +
+ Recent + +
+
+ {#each sidebarStore.state.recent as quiblet} + + {/each} +
-
-
+ {/if} +
-
+
; + +type IQuiblets = { + avatar?: string | null | undefined; + name: string; + starred: boolean; +}[]; + +export function createSidebarStore() { + const stored_sidebar_store = browser ? localStorage.getItem('sidebar_store') : null; + + const parsed_stored_quiblets: ISidebarStore = stored_sidebar_store + ? JSON.parse(stored_sidebar_store) + : {}; + + let sidebar_state = $state(parsed_stored_quiblets); + + function sync_localstorage() { + if (browser) { + localStorage.setItem('sidebar_store', JSON.stringify(sidebar_state)); + } + } + + function sort_quiblets(quiblets: IQuiblets) { + return [...quiblets].sort((a, b) => { + if (a.starred !== b.starred) { + return b.starred ? 1 : -1; + } + return a.name.localeCompare(b.name); + }); + } + + return { + get state() { + return sidebar_state; + }, + add_quiblet(quiblet: IQuiblets[number], type: string) { + // initialize empty array for type + if (!sidebar_state[type]) { + sidebar_state[type] = []; + } + + const state = sidebar_state[type]; + if (!state) return; + + const exists = state.some((q) => q.name === quiblet.name); + if (exists) return; + + sidebar_state[type] = sort_quiblets([...state, quiblet]); + sync_localstorage(); + }, + toggle_star(name: string, type: string) { + if (!sidebar_state[type]) return; + + sidebar_state[type] = sort_quiblets( + sidebar_state[type].map((q) => + q.name === name ? { ...q, starred: !q.starred } : q + ) + ); + } + }; +} diff --git a/frontend/src/routes/(app)/q/[name]/+page.svelte b/frontend/src/routes/(app)/q/[name]/+page.svelte index 51b9f2a..ade94ec 100644 --- a/frontend/src/routes/(app)/q/[name]/+page.svelte +++ b/frontend/src/routes/(app)/q/[name]/+page.svelte @@ -6,11 +6,14 @@ import type { PageData } from './$types'; import { FormatDate } from '$lib/functions/date'; import { createAuthStore } from '$lib/stores/auth.svelte'; + import { onMount } from 'svelte'; + import { createSidebarStore } from '$lib/stores/sidebar.svelte'; const { data }: { data: PageData } = $props(); const { quiblet, quibs, highlighted_quibs } = data; - const authStore = createAuthStore(); + const authStore = createAuthStore(), + sidebarStore = createSidebarStore(); const is_joined = $derived.by(() => { if (!authStore.state.is_authenticated) return false; @@ -18,6 +21,17 @@ return quiblet.members?.includes(authStore.state.profile.id); } }); + + onMount(() => { + sidebarStore.add_quiblet( + { + avatar: quiblet.avatar, + name: quiblet.name, + starred: false + }, + 'recent' + ); + });