fix: favorites bar
This commit is contained in:
1 parent
7903c74e16
commit
3a079b01f1
8 files changed
+51
-37
No files matched your search
+3
-3
@@ -11,6 +11,7 @@ import {
|
|||||||
LemmyHttp,
|
LemmyHttp,
|
||||||
type GetSiteResponse,
|
type GetSiteResponse,
|
||||||
type MyUserInfo,
|
type MyUserInfo,
|
||||||
|
type Community,
|
||||||
} from 'lemmy-js-client'
|
} from 'lemmy-js-client'
|
||||||
import { get, writable } from 'svelte/store'
|
import { get, writable } from 'svelte/store'
|
||||||
import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js'
|
import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js'
|
||||||
@@ -43,7 +44,7 @@ export interface Profile {
|
|||||||
user?: PersonData
|
user?: PersonData
|
||||||
username?: string
|
username?: string
|
||||||
avatar?: string
|
avatar?: string
|
||||||
favorites?: ResumableItem[]
|
favorites?: Community[]
|
||||||
color?: string
|
color?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,11 +152,10 @@ profile.subscribe(async (p) => {
|
|||||||
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||||
}
|
}
|
||||||
if (!p) return
|
if (!p) return
|
||||||
if (p.user) return
|
|
||||||
|
|
||||||
instance.set(p.instance)
|
instance.set(p.instance)
|
||||||
// fetch the user because p.user is undefined
|
// fetch the user because p.user is undefined
|
||||||
if (p?.jwt) {
|
if (p?.jwt && !p?.user) {
|
||||||
const user = await userFromJwt(p.jwt, p.instance)
|
const user = await userFromJwt(p.jwt, p.instance)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
if (!user?.user)
|
if (!user?.user)
|
||||||
|
|||||||
@@ -1,52 +1,64 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { profile } from '$lib/auth'
|
||||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||||
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||||
import { communityLink } from '$lib/lemmy/generic'
|
import { communityLink } from '$lib/lemmy/generic'
|
||||||
import { addResumable } from '$lib/lemmy/item'
|
import { addFavorite } from '$lib/lemmy/item'
|
||||||
|
import { hasFavorite } from '$lib/lemmy/user'
|
||||||
import type { Community } from 'lemmy-js-client'
|
import type { Community } from 'lemmy-js-client'
|
||||||
import { Button } from 'mono-svelte'
|
import { Button } from 'mono-svelte'
|
||||||
import { Icon, Star } from 'svelte-hero-icons'
|
import { Icon, Star, Trash } from 'svelte-hero-icons'
|
||||||
import { flip } from 'svelte/animate'
|
import { flip } from 'svelte/animate'
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from 'svelte/easing'
|
||||||
import { slide } from 'svelte/transition'
|
import { slide } from 'svelte/transition'
|
||||||
|
|
||||||
export let items: Community[]
|
export let items: Community[]
|
||||||
|
export let isFavorites = false
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#each items.sort( (a, b) => a.title.localeCompare(b.title) ) as follow (follow.id)}
|
{#each items.sort( (a, b) => a.title.localeCompare(b.title) ) as follow (follow.id)}
|
||||||
<div
|
<div
|
||||||
class="inline-flex w-full items-center px-3"
|
class="inline-flex w-full items-center pl-3 hover:bg-slate-100 hover:dark:bg-zinc-800
|
||||||
|
transition-colors rounded-lg"
|
||||||
animate:flip={{ duration: 500, easing: expoOut }}
|
animate:flip={{ duration: 500, easing: expoOut }}
|
||||||
>
|
>
|
||||||
<div class="flex-none grid group">
|
<div class="flex-none grid group">
|
||||||
|
{#if $profile && !hasFavorite($profile, follow.id)}
|
||||||
<Button
|
<Button
|
||||||
rounding="pill"
|
rounding="pill"
|
||||||
size="custom"
|
size="custom"
|
||||||
class="hidden group-hover:inline-flex opacity-0 group-hover:opacity-100 transition-all z-20
|
class="hidden group-hover:inline-flex opacity-0 group-hover:opacity-100 transition-all z-20
|
||||||
items-center justify-center box-border w-7 h-7"
|
items-center justify-center box-border w-7 h-7"
|
||||||
style="grid-column: 1; grid-row: 1;"
|
style="grid-column: 1; grid-row: 1;"
|
||||||
on:click={() =>
|
on:click={() => addFavorite(follow)}
|
||||||
addResumable({
|
|
||||||
id: follow.id,
|
|
||||||
name: follow.title,
|
|
||||||
type: 'community',
|
|
||||||
url: communityLink(follow),
|
|
||||||
avatar: follow.icon,
|
|
||||||
})}
|
|
||||||
>
|
>
|
||||||
<Icon src={Star} size="16" mini />
|
<Icon src={Star} size="16" mini />
|
||||||
</Button>
|
</Button>
|
||||||
|
{:else if isFavorites && $profile && hasFavorite($profile, follow.id)}
|
||||||
|
<Button
|
||||||
|
rounding="pill"
|
||||||
|
size="custom"
|
||||||
|
class="hidden group-hover:inline-flex opacity-0 group-hover:opacity-100 transition-all z-20
|
||||||
|
items-center justify-center box-border w-7 h-7"
|
||||||
|
style="grid-column: 1; grid-row: 1;"
|
||||||
|
on:click={() => addFavorite(follow, false)}
|
||||||
|
>
|
||||||
|
<Icon src={Trash} size="16" mini />
|
||||||
|
</Button>
|
||||||
|
{/if}
|
||||||
<Avatar
|
<Avatar
|
||||||
url={follow.icon}
|
url={follow.icon}
|
||||||
alt={follow.name}
|
alt={follow.name}
|
||||||
title={follow.title}
|
title={follow.title}
|
||||||
width={28}
|
width={28}
|
||||||
class="group-hover:hidden"
|
class={!isFavorites && $profile && hasFavorite($profile, follow.id)
|
||||||
|
? ''
|
||||||
|
: 'group-hover:hidden'}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<SidebarButton
|
<SidebarButton
|
||||||
class="font-normal w-full h-max !px-1"
|
class="font-normal w-full h-max"
|
||||||
color="tertiary"
|
color="none"
|
||||||
alignment="left"
|
alignment="left"
|
||||||
href="/c/{follow.name}@{new URL(follow.actor_id).hostname}"
|
href="/c/{follow.name}@{new URL(follow.actor_id).hostname}"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -28,6 +28,8 @@
|
|||||||
import Application from '../../../../routes/admin/applications/Application.svelte'
|
import Application from '../../../../routes/admin/applications/Application.svelte'
|
||||||
import { t } from '$lib/translations'
|
import { t } from '$lib/translations'
|
||||||
import ItemList from '$lib/components/lemmy/generic/ItemList.svelte'
|
import ItemList from '$lib/components/lemmy/generic/ItemList.svelte'
|
||||||
|
|
||||||
|
$: if ($profile) console.log('profile changed')
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav
|
<nav
|
||||||
@@ -112,15 +114,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
</span>
|
</span>
|
||||||
<ItemList
|
<CommunityList isFavorites items={$profile.favorites} />
|
||||||
items={$profile.favorites.map((i) => ({
|
|
||||||
avatar: i.avatar ?? '',
|
|
||||||
id: i.id,
|
|
||||||
name: i.name,
|
|
||||||
url: i.url.toString(),
|
|
||||||
instance: 'doesnt exist',
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Expandable>
|
</Expandable>
|
||||||
<hr class="border-slate-200 dark:border-zinc-900 my-1" />
|
<hr class="border-slate-200 dark:border-zinc-900 my-1" />
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -15,12 +15,12 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
|
color="tertiary"
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{href}
|
{href}
|
||||||
color="tertiary"
|
|
||||||
alignment="left"
|
alignment="left"
|
||||||
on:click
|
on:click
|
||||||
class="hover:bg-slate-100 font-normal {selected
|
class="font-normal {selected
|
||||||
? 'text-primary-900 dark:!text-primary-100'
|
? 'text-primary-900 dark:!text-primary-100'
|
||||||
: 'text-slate-600 dark:text-zinc-400'} {$userSettings.expandSidebar
|
: 'text-slate-600 dark:text-zinc-400'} {$userSettings.expandSidebar
|
||||||
? ''
|
? ''
|
||||||
|
|||||||
@@ -182,7 +182,8 @@
|
|||||||
"settings": "Settings",
|
"settings": "Settings",
|
||||||
"blocks": "Blocks",
|
"blocks": "Blocks",
|
||||||
"credentials": "Credentials",
|
"credentials": "Credentials",
|
||||||
"moderates": "Moderates"
|
"moderates": "Moderates",
|
||||||
|
"favorites": "Favorites"
|
||||||
},
|
},
|
||||||
"translators": "Translators",
|
"translators": "Translators",
|
||||||
"accounts": "Accounts",
|
"accounts": "Accounts",
|
||||||
|
|||||||
+15
-6
@@ -1,4 +1,4 @@
|
|||||||
import { profile } from '$lib/auth'
|
import { profile, profileData } from '$lib/auth'
|
||||||
import type {
|
import type {
|
||||||
CommentReportView,
|
CommentReportView,
|
||||||
CommentView,
|
CommentView,
|
||||||
@@ -10,6 +10,7 @@ import type {
|
|||||||
Post,
|
Post,
|
||||||
PrivateMessageReportView,
|
PrivateMessageReportView,
|
||||||
PrivateMessageView,
|
PrivateMessageView,
|
||||||
|
Community,
|
||||||
} from 'lemmy-js-client'
|
} from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
@@ -81,16 +82,24 @@ export interface ResumableItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addResumable(item: ResumableItem) {
|
export function addFavorite(item: Community, add: boolean = true) {
|
||||||
const p = get(profile)
|
const pd = get(profileData)
|
||||||
|
const p = pd.profiles.find((p) => p.id == get(profile)?.id)
|
||||||
if (!p) return
|
if (!p) return
|
||||||
|
|
||||||
let favs = p.favorites ?? []
|
let favs = p.favorites ?? []
|
||||||
if (favs.map((fav) => fav.url.toString()).includes(item.url.toString()))
|
if (favs.map((fav) => fav.id).includes(item.id)) if (add) return
|
||||||
return
|
if (!add) {
|
||||||
|
favs.splice(favs.map((c) => c.id).indexOf(item.id), 1)
|
||||||
|
} else {
|
||||||
favs.unshift(item)
|
favs.unshift(item)
|
||||||
profile.update(() => ({
|
}
|
||||||
|
p.favorites = favs
|
||||||
|
profileData.update(() => pd)
|
||||||
|
profile.update((p) => ({
|
||||||
...p,
|
...p,
|
||||||
favorites: favs,
|
favorites: favs,
|
||||||
|
id: p!.id,
|
||||||
|
instance: p!.instance,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,6 @@
|
|||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
import { profile } from '$lib/auth'
|
import { profile } from '$lib/auth'
|
||||||
import { addResumable } from '$lib/lemmy/item.js'
|
|
||||||
import { t } from '$lib/translations.js'
|
import { t } from '$lib/translations.js'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|||||||
@@ -40,7 +40,6 @@
|
|||||||
} from '$lib/components/lemmy/post/helpers.js'
|
} from '$lib/components/lemmy/post/helpers.js'
|
||||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||||
import Expandable from '$lib/components/ui/Expandable.svelte'
|
import Expandable from '$lib/components/ui/Expandable.svelte'
|
||||||
import { addResumable } from '$lib/lemmy/item.js'
|
|
||||||
import { Popover } from 'mono-svelte'
|
import { Popover } from 'mono-svelte'
|
||||||
import { t } from '$lib/translations.js'
|
import { t } from '$lib/translations.js'
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user