Skip to content

Commit b43a177

Browse files
committed
feat: add bookmark
1 parent 5dee1de commit b43a177

File tree

14 files changed

+126
-71
lines changed

14 files changed

+126
-71
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ cd build
5555

5656
## ToDo
5757

58-
- Bookmark
5958
- PWA
6059

6160
## Credits

api/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func Run() {
3232
if conf.Debug {
3333
r.Debug = true
3434
r.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
35+
if len(resBody) > 500 {
36+
resBody = append(resBody[:500], []byte("...")...)
37+
}
3538
r.Logger.Debugf("req: %s\nresp: %s\n", reqBody, resBody)
3639
}))
3740
}

frontend/src/lib/api/item.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type listOptions = {
77
keyword?: string;
88
feed_id?: number;
99
unread?: boolean;
10+
bookmark?: boolean;
1011
};
1112

1213
export async function listItems(options?: listOptions) {
@@ -21,10 +22,14 @@ export async function getItem(id: number) {
2122
return api.get('items/' + id).json<Item>();
2223
}
2324

24-
export async function updateItem(id: number, unread: boolean) {
25+
export async function updateItem(
26+
id: number,
27+
data: {
28+
unread?: boolean;
29+
bookmark?: boolean;
30+
}
31+
) {
2532
return api.patch('items/' + id, {
26-
json: {
27-
unread: unread
28-
}
33+
json: data
2934
});
3035
}

frontend/src/lib/api/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export type Item = {
2020
content: string;
2121
pub_date: Date;
2222
unread: boolean;
23+
bookmark: boolean;
2324
feed: { id: number; name: string };
2425
};

frontend/src/lib/components/ItemAction.svelte

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<script lang="ts">
2-
import { CheckIcon, ExternalLinkIcon, UndoIcon } from 'lucide-svelte';
2+
import {
3+
BookmarkIcon,
4+
BookmarkXIcon,
5+
CheckIcon,
6+
ExternalLinkIcon,
7+
UndoIcon
8+
} from 'lucide-svelte';
39
import type { ComponentType } from 'svelte';
410
import type { Icon } from 'lucide-svelte';
511
import * as Tooltip from '$lib/components/ui/tooltip';
@@ -12,47 +18,53 @@
1218
id: number;
1319
link: string;
1420
unread: boolean;
21+
bookmark: boolean;
1522
};
1623
1724
function getActions(
18-
unread: boolean
25+
unread: boolean,
26+
bookmark: boolean
1927
): { icon: ComponentType<Icon>; tooltip: string; handler: (e: Event) => void }[] {
20-
const list = [
21-
// { icon: BookmarkIcon, tooltip: 'Save to Bookmark', handler: handleSaveToBookmark },
22-
{ icon: ExternalLinkIcon, tooltip: 'Visit Original Link', handler: handleExternalLink }
23-
];
28+
const visitOriginalAction = {
29+
icon: ExternalLinkIcon,
30+
tooltip: 'Visit Original Link',
31+
handler: handleExternalLink
32+
};
2433
const unreadAction = unread
25-
? { icon: CheckIcon, tooltip: 'Mark as Read', handler: handleMarkAsRead }
26-
: { icon: UndoIcon, tooltip: 'Mark as Unread', handler: handleMarkAsUnread };
27-
list.unshift(unreadAction);
28-
return list;
34+
? { icon: CheckIcon, tooltip: 'Mark as Read', handler: handleToggleUnread }
35+
: { icon: UndoIcon, tooltip: 'Mark as Unread', handler: handleToggleUnread };
36+
const bookmarkAction = bookmark
37+
? { icon: BookmarkXIcon, tooltip: 'Cancel Bookmark', handler: handleToggleBookmark }
38+
: { icon: BookmarkIcon, tooltip: 'Add to Bookmark', handler: handleToggleBookmark };
39+
40+
return [unreadAction, bookmarkAction, visitOriginalAction];
2941
}
30-
$: actions = getActions(data.unread);
42+
$: actions = getActions(data.unread, data.bookmark);
3143
32-
async function handleMarkAsRead(e: Event) {
44+
async function handleToggleUnread(e: Event) {
3345
e.preventDefault();
3446
try {
35-
await updateItem(data.id, false);
47+
await updateItem(data.id, { unread: !data.unread });
48+
invalidateAll();
3649
} catch (e) {
3750
toast.error((e as Error).message);
3851
}
39-
invalidateAll();
4052
}
4153
42-
async function handleMarkAsUnread(e: Event) {
54+
function handleExternalLink(e: Event) {
55+
e.preventDefault();
56+
handleToggleUnread(e);
57+
window.open(data.link, '_target');
58+
}
59+
60+
async function handleToggleBookmark(e: Event) {
4361
e.preventDefault();
4462
try {
45-
await updateItem(data.id, true);
63+
await updateItem(data.id, { bookmark: !data.bookmark });
64+
invalidateAll();
4665
} catch (e) {
4766
toast.error((e as Error).message);
4867
}
49-
invalidateAll();
50-
}
51-
52-
function handleExternalLink(e: Event) {
53-
e.preventDefault();
54-
handleMarkAsRead(e);
55-
window.open(data.link, '_target');
5668
}
5769
</script>
5870

frontend/src/lib/components/ItemList.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
</div>
6161

6262
<div class="w-full hidden group-hover:inline-flex justify-end">
63-
<ItemAction data={{ id: item.id, link: item.link, unread: item.unread }} />
63+
<ItemAction
64+
data={{ id: item.id, link: item.link, unread: item.unread, bookmark: item.bookmark }}
65+
/>
6466
</div>
6567
</div>
6668
</Button>

frontend/src/lib/components/Navbar.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
}
1010
let links: link[] = [
1111
{ label: 'Unread', url: '/' },
12+
{ label: 'Bookmark', url: '/bookmarks' },
1213
{ label: 'All', url: '/all' },
1314
{ label: 'Feeds', url: '/feeds' }
1415
];
@@ -31,7 +32,7 @@
3132

3233
<nav class="block w-full sm:mt-3 mb-6">
3334
<div
34-
class="flex justify-around items-center w-full sm:max-w-sm mx-auto px-6 py-4 sm:rounded-2xl shadow-md sm:border bg-background"
35+
class="flex justify-around items-center w-full sm:max-w-[600px] mx-auto px-6 py-4 sm:rounded-2xl shadow-md sm:border bg-background"
3536
>
3637
<div class="flex items-center">
3738
<img src="/favicon.png" alt="logo" class="w-10" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="ts">
2+
import type { PageData } from './$types';
3+
import ItemList from '$lib/components/ItemList.svelte';
4+
import PageHead from '$lib/components/PageHead.svelte';
5+
6+
export let data: PageData;
7+
</script>
8+
9+
<svelte:head>
10+
<title>Bookmark</title>
11+
</svelte:head>
12+
13+
<PageHead title="Bookmark" />
14+
<ItemList data={data.items} />
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { listItems } from '$lib/api/item';
2+
import type { PageLoad } from './$types';
3+
4+
export const load: PageLoad = () => {
5+
return listItems({ bookmark: true });
6+
};

frontend/src/routes/items/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<p class="text-sm text-muted-foreground">
5858
{data.feed.name} / {moment(data.pub_date).format('lll')}
5959
</p>
60-
<ItemAction data={{ id: data.id, link: data.link, unread: data.unread }} />
60+
<ItemAction data={{ id: data.id, link: data.link, unread: data.unread, bookmark: data.bookmark }} />
6161
<article class="mt-6 prose dark:prose-invert prose-lg max-w-full">
6262
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
6363
{@html data.content}

0 commit comments

Comments
 (0)