Skip to content

Commit

Permalink
fix: reactive add/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Aug 11, 2024
1 parent d02d5b2 commit 7a69748
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/lib/components/DragAdd.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { toast } from "svelte-sonner"
import { z } from "zod"
import ConfirmAddDialog from "$lib/components/ConfirmAddDialog.svelte"
import { cn } from "$/lib/utils/style"
import ConfirmAddDialog from "$lib/components/ConfirmAddDialog.svelte"
const parseData = (text: string | undefined): string | void => {
try {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/lib/components/bookmark-row/DeleteDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<div>
<h3 class="mb-4 text-lg font-bold">Delete</h3>
<div>
Are you sure you want to delete <b>{bookmark?.url}</b>? <br /><br />This action cannot be
undone. This will permanently delete your bookmark.
Are you sure you want to delete <b class="break-all">{bookmark?.url}</b>? <br /><br />This
action cannot be undone. This will permanently delete your bookmark.
</div>
</div>
<div class="flex flex-col gap-4 sm:flex-row sm:justify-end">
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/lib/state/bookmarks.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export class BookmarksService {
}

add(bookmark: Bookmark | Bookmark[]) {
if (Array.isArray(bookmark)) {
bookmark.forEach((bk) => {
if (this.bookmarks.find((savedBookmark) => savedBookmark.id === bk.id)) return
this.bookmarks.unshift(bk)
})
} else {
if (this.bookmarks.find((savedBookmark) => savedBookmark.id === bookmark.id)) return
this.bookmarks.unshift(bookmark)
}
}

append(bookmark: Bookmark | Bookmark[]) {
if (Array.isArray(bookmark)) {
bookmark.forEach((bk) => {
if (this.bookmarks.find((savedBookmark) => savedBookmark.id === bk.id)) return
Expand All @@ -33,6 +45,15 @@ export class BookmarksService {
}
}

upsert(bookmark: Bookmark) {
const bookmarkIndex = this.bookmarks.findIndex((bk) => bk.id === bookmark.id)
if (bookmarkIndex) {
this.bookmarks[bookmarkIndex] = bookmark
} else {
this.bookmarks.push(bookmark)
}
}

find(bookmarkId: string) {
return this.bookmarks.find((bk) => bk.id === bookmarkId)
}
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/lib/state/feeds.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export class FeedsService {
}

add(feeds: Feed | Feed[]) {
if (Array.isArray(feeds)) {
feeds.forEach((feed) => {
if (this.feeds.find((savedFeed) => savedFeed.id === feed.id)) return
this.feeds.unshift(feed)
})
} else {
if (this.feeds.find((savedFeed) => savedFeed.id === feeds.id)) return
this.feeds.unshift(feeds)
}
}

append(feeds: Feed | Feed[]) {
if (Array.isArray(feeds)) {
feeds.forEach((feed) => {
if (this.feeds.find((savedFeed) => savedFeed.id === feed.id)) return
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/routes/(dashboard)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
setContext(FeedEntriesService, feedEntriesService)
setContext(BookmarksService, bookmarksService)
$effect(() => {
// Deal with invalidated and re-run 'Load' functions
if ($page.data.bookmarks.data.length) {
// Add newly created to state
if (!bookmarksService.find($page.data.bookmarks.data[0])) {
bookmarksService.add($page.data.bookmarks.data[0])
}
// Update any potentially changed bookmarks
$page.data.bookmarks.data.forEach((bk: LoadBookmarkFlatTags) => {
bookmarksService.update(bk)
})
}
})
const ui = useInterface()
// Set current user preferences to store
Expand Down
26 changes: 13 additions & 13 deletions apps/web/src/routes/(dashboard)/archives/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
import { page } from "$app/stores"
const ui = useInterface()
const bookmarkStore = getContext(BookmarksService)
const bookmarksService = getContext(BookmarksService)
let pageNumber = $state(0)
const rootElement = $state<HTMLElement>()
const limitLoadCount = 20
const logger = new Logger({ level: loggerLevels.DEBUG })
$effect(() => {
bookmarkStore.bookmarks = $page.data.bookmarks.data
})
// $effect(() => {
// bookmarkStore.bookmarks = $page.data.bookmarks.data
// })
if ($page.data.error) {
logger.error(String($page.data.error))
Expand Down Expand Up @@ -95,10 +95,10 @@
}
if (searchResults.data.length) {
bookmarkStore.add(searchResults.data as any[])
bookmarksService.append(searchResults.data)
}
if (bookmarkStore.bookmarks.length >= searchResults.count) {
if (bookmarksService.bookmarks.length >= searchResults.count) {
loaderState.complete()
} else {
loaderState.loaded()
Expand All @@ -118,15 +118,15 @@
if (e.key === "ArrowDown" || e.key === "ArrowUp" || e.key === "j" || e.key === "k") {
e.preventDefault()
const currentActiveElement = e.target as HTMLElement
const currentActiveElementIndex = bookmarkStore.bookmarks.findIndex(
const currentActiveElementIndex = bookmarksService.bookmarks.findIndex(
(item) => item.id === currentActiveElement.dataset.id,
)
const nextIndex =
e.key === "ArrowDown" || e.key === "j"
? currentActiveElementIndex + 1
: currentActiveElementIndex - 1
const nextElement = document.querySelector(
`[data-id="${bookmarkStore.bookmarks[nextIndex]?.id}"]`,
`[data-id="${bookmarksService.bookmarks[nextIndex]?.id}"]`,
) as HTMLElement
if (nextElement) {
Expand All @@ -136,10 +136,10 @@
if (e.key === "o") {
e.preventDefault()
const currentActiveElement = e.target as HTMLElement
const currentActiveElementIndex = bookmarkStore.bookmarks.findIndex(
const currentActiveElementIndex = bookmarksService.bookmarks.findIndex(
(item) => item.id === currentActiveElement.dataset.id,
)
const targetLink = bookmarkStore.bookmarks[currentActiveElementIndex]?.url
const targetLink = bookmarksService.bookmarks[currentActiveElementIndex]?.url
if (!targetLink) {
toast.error("No item selected")
return
Expand Down Expand Up @@ -177,14 +177,14 @@

<Navbar />
<main class="align-start flex flex-col justify-start gap-2 overflow-y-scroll outline-none">
{#if bookmarkStore.bookmarks?.length}
{#if bookmarksService.bookmarks?.length}
<div class="h-full">
<InfiniteLoader triggerLoad={loadMore} intersectionOptions={{ root: rootElement }}>
{#each bookmarkStore.bookmarks as item (item.id)}
{#each bookmarksService.bookmarks as item (item.id)}
<BookmarkRow bookmark={item} />
{/each}
{#snippet noData()}
{#if bookmarkStore.bookmarks.length >= 10}
{#if bookmarksService.bookmarks.length >= 10}
<div class="text-2xl">No more data</div>
{/if}
{/snippet}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/routes/(dashboard)/bookmarks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
}
if (searchResults.data.length) {
bookmarkService.add(searchResults.data)
bookmarkService.append(searchResults.data)
}
if (bookmarkService.bookmarks.length >= searchResults.count) {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/routes/(dashboard)/feeds/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
}
if (searchResults.data.length) {
feedEntriesService.add(searchResults.data)
feedEntriesService.append(searchResults.data)
}
if (feedEntriesService.feedEntries.length >= searchResults.count) {
Expand Down

0 comments on commit 7a69748

Please sign in to comment.