Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add indicator when there are new items added #72

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/components/UserCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
items: number;
};
};
export let newItems = false;
</script>

<a class="card" data-sveltekit-preload-data href="/wishlists/{user.username}">
Expand All @@ -25,6 +26,13 @@
<span>
{!hideCount && user._count ? `${user._count.items}/` : ""}{user.items.length}
</span>
{#if newItems}
<iconify-icon
class="text-primary-700-200-token opacity-40"
icon="ion:ellipse-sharp"
width="10px"
/>
{/if}
</div>
</div>
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/lib/stores/viewed-items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Item } from "@prisma/client";
import { localStorageStore } from "@skeletonlabs/skeleton";
import type { Writable } from "svelte/store";

export const viewedItems: Writable<Record<string, string>> = localStorageStore("viewedItems", {});

export const hashItems = async (items: Partial<Item>[]): Promise<string> => {
const itemIds = items
.map(({ id }) => id)
.toSorted()
.join("");
return await hash(itemIds);
};

export const hash = async (data: string): Promise<string> => {
const encoder = new TextEncoder();
const digest = await crypto.subtle.digest("SHA-256", encoder.encode(data));
return btoa(String.fromCharCode(...new Uint8Array(digest)));
};
1 change: 1 addition & 0 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const load = (async ({ locals }) => {
}
},
select: {
id: true,
username: true,
name: true,
picture: true,
Expand Down
16 changes: 15 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
import UserCard from "$lib/components/UserCard.svelte";
import { fade } from "svelte/transition";
import ApprovalAlert from "$lib/components/wishlists/ApprovalAlert.svelte";
import { hash, hashItems, viewedItems } from "$lib/stores/viewed-items";

export let data: PageData;
type PageUserData = (typeof data)["users"][0];

const hasNewItems = async (user: PageUserData) => {
if (user.items.length === 0) return false;
const userHash = await hash(user.id);
const currentHash = await hashItems(user.items);
const viewedHash = $viewedItems[userHash];
return currentHash !== viewedHash;
};
</script>

<div class="flex flex-col space-y-4" in:fade>
<ApprovalAlert approvalCount={data.me._count.items} />
<UserCard hideCount user={data.me} />

{#each data.users as user}
<UserCard {user} />
{#await hasNewItems(user)}
<UserCard {user} />
{:then newItems}
<UserCard {newItems} {user} />
{/await}
{/each}
</div>

Expand Down
7 changes: 4 additions & 3 deletions src/routes/wishlists/[username]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,21 @@ export const load: PageServerLoad = async ({ locals, params, depends, url }) =>
}
});

const listOwner = await client.user.findUnique({
const listOwner = await client.user.findUniqueOrThrow({
where: {
username: params.username
},
select: {
name: true
name: true,
id: true
}
});

return {
user: session.user,
listOwner: {
isMe: params.username === session.user.username,
name: listOwner?.name
...listOwner
},
items: wishlistItems.filter((item) => item.approved),
approvals: wishlistItems.filter((item) => !item.approved),
Expand Down
7 changes: 6 additions & 1 deletion src/routes/wishlists/[username]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { isInstalled } from "$lib/stores/is-installed";
import empty from "$lib/assets/no_wishes.svg";
import SortBy from "$lib/components/wishlists/chips/SortBy.svelte";
import { hash, hashItems, viewedItems } from "$lib/stores/viewed-items";

export let data: PageData;

Expand Down Expand Up @@ -54,7 +55,11 @@
}, 5000);
};

onMount(pollUpdate);
onMount(async () => {
const userHash = await hash(data.listOwner.id);
$viewedItems[userHash] = await hashItems(data.items);
pollUpdate();
});
onDestroy(() => clearTimeout(pollTimeout));

$: if (!$idle && !polling) {
Expand Down
Loading