enzostvs HF staff commited on
Commit
10926a7
·
1 Parent(s): b0c2459

autosubscribe user store

Browse files
src/lib/components/UserIsLogged.svelte CHANGED
@@ -1,14 +1,12 @@
1
  <script lang="ts">
2
- import { get } from "svelte/store";
3
  import { loginModalStore } from "$lib/stores/use-login-modal";
4
  import { userStore } from "$lib/stores/use-user";
5
 
6
- let user = get(userStore);
7
 
8
  const handleClick = (e: any) => {
9
  e.preventDefault();
10
  e.stopPropagation();
11
- if (!user) {
12
  loginModalStore.update(() => true);
13
  }
14
  };
@@ -20,7 +18,7 @@
20
  class="w-full cursor-pointer block"
21
  on:click={handleClick}
22
  >
23
- <div class:pointer-events-none={!user}>
24
  <slot />
25
  </div>
26
  </div>
 
1
  <script lang="ts">
 
2
  import { loginModalStore } from "$lib/stores/use-login-modal";
3
  import { userStore } from "$lib/stores/use-user";
4
 
 
5
 
6
  const handleClick = (e: any) => {
7
  e.preventDefault();
8
  e.stopPropagation();
9
+ if (!$userStore) {
10
  loginModalStore.update(() => true);
11
  }
12
  };
 
18
  class="w-full cursor-pointer block"
19
  on:click={handleClick}
20
  >
21
+ <div class:pointer-events-none={!$userStore}>
22
  <slot />
23
  </div>
24
  </div>
src/lib/components/comments/Comment.svelte CHANGED
@@ -1,6 +1,5 @@
1
  <script lang="ts">
2
  import type { CommentType } from "$lib/type";
3
- import { get } from "svelte/store";
4
  import { userStore } from "$lib/stores/use-user";
5
 
6
  export let comment: CommentType;
@@ -10,8 +9,7 @@
10
  return `${d.toLocaleDateString()} ${d.toLocaleTimeString()}`;
11
  }
12
 
13
- const user = get(userStore);
14
- const isMe = comment?.user?.sub === user?.sub;
15
  </script>
16
 
17
  <div
 
1
  <script lang="ts">
2
  import type { CommentType } from "$lib/type";
 
3
  import { userStore } from "$lib/stores/use-user";
4
 
5
  export let comment: CommentType;
 
9
  return `${d.toLocaleDateString()} ${d.toLocaleTimeString()}`;
10
  }
11
 
12
+ const isMe = comment?.user?.sub === $userStore?.sub;
 
13
  </script>
14
 
15
  <div
src/lib/components/community/reactions/Reactions.svelte CHANGED
@@ -2,11 +2,9 @@
2
  import type { ReactionType } from "$lib/type";
3
  import Add from "$lib/components/community/reactions/Add.svelte";
4
  import Reaction from "$lib/components/community/reactions/Reaction.svelte";
5
- import { get } from 'svelte/store';
6
  import { userStore } from "$lib/stores/use-user";
7
  import UserIsLogged from "$lib/components/UserIsLogged.svelte";
8
 
9
- let user = get(userStore);
10
 
11
  export let reactions: ReactionType[] = [];
12
  export let gallery_id: string;
@@ -17,7 +15,7 @@
17
  return {
18
  emoji,
19
  count: reactions.filter((reaction) => reaction.emoji === emoji).length,
20
- liked: !!reactions.find((reaction) => reaction.emoji === emoji && reaction.userId === user?.sub)
21
  };
22
  });
23
  };
@@ -37,7 +35,7 @@
37
  if (deleted) {
38
  reactions = reactions.filter((reaction) => reaction.id !== id);
39
  } else {
40
- reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
41
  }
42
  }}
43
  />
@@ -47,7 +45,7 @@
47
  reactions={groupedReactions}
48
  {gallery_id}
49
  onAdd={(emoji, id) => {
50
- reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
51
  }}
52
  />
53
  </div>
 
2
  import type { ReactionType } from "$lib/type";
3
  import Add from "$lib/components/community/reactions/Add.svelte";
4
  import Reaction from "$lib/components/community/reactions/Reaction.svelte";
 
5
  import { userStore } from "$lib/stores/use-user";
6
  import UserIsLogged from "$lib/components/UserIsLogged.svelte";
7
 
 
8
 
9
  export let reactions: ReactionType[] = [];
10
  export let gallery_id: string;
 
15
  return {
16
  emoji,
17
  count: reactions.filter((reaction) => reaction.emoji === emoji).length,
18
+ liked: !!reactions.find((reaction) => reaction.emoji === emoji && reaction.userId === $userStore?.sub)
19
  };
20
  });
21
  };
 
35
  if (deleted) {
36
  reactions = reactions.filter((reaction) => reaction.id !== id);
37
  } else {
38
+ reactions = [...reactions, { emoji, userId: $userStore?.sub, galleryId: gallery_id, id }];
39
  }
40
  }}
41
  />
 
45
  reactions={groupedReactions}
46
  {gallery_id}
47
  onAdd={(emoji, id) => {
48
+ reactions = [...reactions, { emoji, userId: $userStore?.sub, galleryId: gallery_id, id }];
49
  }}
50
  />
51
  </div>
src/lib/components/generate/Response.svelte CHANGED
@@ -1,20 +1,16 @@
1
  <script lang="ts">
2
- import { get } from "svelte/store";
3
  import { generationStore } from "$lib/stores/use-generation";
4
-
5
  import Button from "$lib/components/Button.svelte";
6
  import { userStore } from "$lib/stores/use-user";
7
 
8
- let generation = get(generationStore);
9
  export let loading_generation: boolean = false;
10
 
11
  let loading: boolean = false;
12
- let user = get(userStore)
13
 
14
  const saveImage = () => {
15
  const link = document.createElement('a');
16
- link.href = generation?.image as string;
17
- link.download = `${generation?.form?.inputs?.slice(0, 20)}.png`;
18
  document.body.appendChild(link);
19
  link.click();
20
  document.body.removeChild(link);
@@ -23,7 +19,7 @@
23
  const share = () => {
24
  if (loading) return;
25
  loading = true;
26
- fetch(`/api/community/${generation?.gallery?.id}/publish`, {
27
  method: "POST",
28
  headers: {
29
  "Content-Type": "application/json"
@@ -39,10 +35,6 @@
39
  })
40
  }
41
 
42
- generationStore.subscribe((value) => {
43
- generation = value;
44
- })
45
-
46
  // create a ms countup depending on the generation time, to show the user how long it took to generate the image
47
  let ms = 0;
48
  let interval: any;
@@ -70,7 +62,7 @@
70
  }
71
  </script>
72
 
73
- <div class=" w-full border-t xl:border-t-0 xl:border-l border-neutral-800 h-full col-span-5 xl:col-span-2" class:!border-black={!generation?.image || loading_generation}>
74
  {#if loading_generation}
75
  <div class="w-full h-full flex items-center justify-center flex-col gap-3 bg-neutral-950 relative">
76
  <p class="text-neutral-100 text-xl font-semibold">
@@ -81,13 +73,13 @@
81
  </p>
82
  </div>
83
  {:else}
84
- {#if generation?.image}
85
- {#if typeof generation?.image === "string"}
86
- <img src={generation?.image} alt="Generation" class="w-full mx-auto object-contain" />
87
  <div class="p-8 w-full">
88
  <div class="w-full flex items-center justify-end gap-4">
89
  <Button size="lg" theme="light" icon="material-symbols:save" iconPosition="right" onClick={saveImage}>Download</Button>
90
- {#if user?.sub}
91
  <Button
92
  size="lg"
93
  theme="blue"
@@ -95,9 +87,9 @@
95
  iconPosition="right"
96
  loading={loading}
97
  onClick={share}
98
- disabled={loading || generation?.already_saved}
99
  >
100
- {#if generation?.already_saved}
101
  Shared!
102
  {:else}
103
  Share with community
@@ -105,17 +97,17 @@
105
  </Button>
106
  {/if}
107
  </div>
108
- {#if generation?.form}
109
  <div class="mt-6 grid grid-cols-1 gap-4">
110
  <div>
111
  <p class="text-neutral-400 font-semibold text-xs uppercase">
112
  Model selected
113
  </p>
114
  <div class="flex items-center justify-start gap-4 px-2 py-2.5 rounded-lg cursor-pointer w-full text-left">
115
- <img src={generation?.form?.model.image} alt={generation?.form?.model.title} class="w-14 h-14 rounded-lg object-cover" />
116
  <div>
117
- <p class="text-neutral-200 text-base font-medium">{generation?.form?.model.id}</p>
118
- <p class="text-neutral-400 text-sm">{generation?.form?.model.id}</p>
119
  </div>
120
  </div>
121
  </div>
@@ -123,7 +115,7 @@
123
  <p class="text-neutral-400 font-semibold text-xs uppercase">
124
  Prompt
125
  </p>
126
- <p class="text-neutral-200 text-base font-medium mt-2">"{generation?.form.inputs}"</p>
127
  </div>
128
  </div>
129
  {/if}
 
1
  <script lang="ts">
 
2
  import { generationStore } from "$lib/stores/use-generation";
 
3
  import Button from "$lib/components/Button.svelte";
4
  import { userStore } from "$lib/stores/use-user";
5
 
 
6
  export let loading_generation: boolean = false;
7
 
8
  let loading: boolean = false;
 
9
 
10
  const saveImage = () => {
11
  const link = document.createElement('a');
12
+ link.href = $generationStore?.image as string;
13
+ link.download = `${$generationStore?.form?.inputs?.slice(0, 20)}.png`;
14
  document.body.appendChild(link);
15
  link.click();
16
  document.body.removeChild(link);
 
19
  const share = () => {
20
  if (loading) return;
21
  loading = true;
22
+ fetch(`/api/community/${$generationStore?.gallery?.id}/publish`, {
23
  method: "POST",
24
  headers: {
25
  "Content-Type": "application/json"
 
35
  })
36
  }
37
 
 
 
 
 
38
  // create a ms countup depending on the generation time, to show the user how long it took to generate the image
39
  let ms = 0;
40
  let interval: any;
 
62
  }
63
  </script>
64
 
65
+ <div class=" w-full border-t xl:border-t-0 xl:border-l border-neutral-800 h-full col-span-5 xl:col-span-2" class:!border-black={!$generationStore?.image || loading_generation}>
66
  {#if loading_generation}
67
  <div class="w-full h-full flex items-center justify-center flex-col gap-3 bg-neutral-950 relative">
68
  <p class="text-neutral-100 text-xl font-semibold">
 
73
  </p>
74
  </div>
75
  {:else}
76
+ {#if $generationStore?.image}
77
+ {#if typeof $generationStore?.image === "string"}
78
+ <img src={$generationStore?.image} alt="Generation" class="w-full mx-auto object-contain" />
79
  <div class="p-8 w-full">
80
  <div class="w-full flex items-center justify-end gap-4">
81
  <Button size="lg" theme="light" icon="material-symbols:save" iconPosition="right" onClick={saveImage}>Download</Button>
82
+ {#if $userStore?.sub}
83
  <Button
84
  size="lg"
85
  theme="blue"
 
87
  iconPosition="right"
88
  loading={loading}
89
  onClick={share}
90
+ disabled={loading || $generationStore?.already_saved}
91
  >
92
+ {#if $generationStore?.already_saved}
93
  Shared!
94
  {:else}
95
  Share with community
 
97
  </Button>
98
  {/if}
99
  </div>
100
+ {#if $generationStore?.form}
101
  <div class="mt-6 grid grid-cols-1 gap-4">
102
  <div>
103
  <p class="text-neutral-400 font-semibold text-xs uppercase">
104
  Model selected
105
  </p>
106
  <div class="flex items-center justify-start gap-4 px-2 py-2.5 rounded-lg cursor-pointer w-full text-left">
107
+ <img src={$generationStore?.form?.model.image} alt={$generationStore?.form?.model.title} class="w-14 h-14 rounded-lg object-cover" />
108
  <div>
109
+ <p class="text-neutral-200 text-base font-medium">{$generationStore?.form?.model.id}</p>
110
+ <p class="text-neutral-400 text-sm">{$generationStore?.form?.model.id}</p>
111
  </div>
112
  </div>
113
  </div>
 
115
  <p class="text-neutral-400 font-semibold text-xs uppercase">
116
  Prompt
117
  </p>
118
+ <p class="text-neutral-200 text-base font-medium mt-2">"{$generationStore?.form.inputs}"</p>
119
  </div>
120
  </div>
121
  {/if}
src/lib/components/models/Card.svelte CHANGED
@@ -1,6 +1,5 @@
1
  <script lang="ts">
2
  import type { ModelCard } from "$lib/type";
3
- import { get } from "svelte/store";
4
  import Icon from "@iconify/svelte";
5
  import { goto } from "$app/navigation";
6
  import { page } from "$app/stores";
@@ -10,7 +9,6 @@
10
 
11
  export let card: ModelCard;
12
 
13
- let user = get(userStore);
14
 
15
  const handleClick = async () => {
16
  $page.url.searchParams.set('model', card?.id);
@@ -48,7 +46,7 @@
48
  >
49
  <div class="w-full h-[350px] relative z-[1] mb-3 overflow-hidden">
50
  <img src="{card.image}" class="w-full h-full bg-center bg-cover rounded-lg object-cover object-center bg-neutral-800" alt="{card?.id}" />
51
- {#if user?.is_admin}
52
  <div
53
  class="absolute flex items-center justify-between bottom-0 left-0 w-full p-5 bg-gradient-to-t from-black to-transparent"
54
  on:click={e => e.stopPropagation()}
 
1
  <script lang="ts">
2
  import type { ModelCard } from "$lib/type";
 
3
  import Icon from "@iconify/svelte";
4
  import { goto } from "$app/navigation";
5
  import { page } from "$app/stores";
 
9
 
10
  export let card: ModelCard;
11
 
 
12
 
13
  const handleClick = async () => {
14
  $page.url.searchParams.set('model', card?.id);
 
46
  >
47
  <div class="w-full h-[350px] relative z-[1] mb-3 overflow-hidden">
48
  <img src="{card.image}" class="w-full h-full bg-center bg-cover rounded-lg object-cover object-center bg-neutral-800" alt="{card?.id}" />
49
+ {#if $userStore?.is_admin}
50
  <div
51
  class="absolute flex items-center justify-between bottom-0 left-0 w-full p-5 bg-gradient-to-t from-black to-transparent"
52
  on:click={e => e.stopPropagation()}
src/lib/components/models/Submit.svelte CHANGED
@@ -1,11 +1,9 @@
1
  <script lang="ts">
2
- import { get } from 'svelte/store';
3
  import { userStore } from "$lib/stores/use-user";
4
 
5
  import Input from "$lib/components/fields/Input.svelte";
6
  import Button from "$lib/components/Button.svelte";
7
 
8
- let user = get(userStore);
9
  export let onClose: () => void;
10
  let model = {
11
  id: '',
@@ -41,12 +39,12 @@
41
  }
42
  </script>
43
  <div class="grid grid-cols-1 gap-8">
44
- {#if user?.picture}
45
  <div class="flex items-center justify-start gap-3">
46
- <img src={user.picture} alt="User avatar" class="w-8 h-8 rounded-full border border-white inline-block" />
47
  <div class="w-full text-left text-white">
48
- <p class="text-base font-semibold">{user?.name}</p>
49
- <p class="text-xs leading-none text-neutral-400">{user?.preferred_username}</p>
50
  </div>
51
  </div>
52
  {:else}
@@ -70,7 +68,7 @@
70
  </p>
71
  <Input
72
  value={model.id}
73
- placeholder="{`${user?.preferred_username ?? 'enzostvs'}/`}"
74
  prefix="huggingface.co/"
75
  onChange={(value) => model.id = value}
76
  />
 
1
  <script lang="ts">
 
2
  import { userStore } from "$lib/stores/use-user";
3
 
4
  import Input from "$lib/components/fields/Input.svelte";
5
  import Button from "$lib/components/Button.svelte";
6
 
 
7
  export let onClose: () => void;
8
  let model = {
9
  id: '',
 
39
  }
40
  </script>
41
  <div class="grid grid-cols-1 gap-8">
42
+ {#if $userStore?.picture}
43
  <div class="flex items-center justify-start gap-3">
44
+ <img src={$userStore.picture} alt="User avatar" class="w-8 h-8 rounded-full border border-white inline-block" />
45
  <div class="w-full text-left text-white">
46
+ <p class="text-base font-semibold">{$userStore?.name}</p>
47
+ <p class="text-xs leading-none text-neutral-400">{$userStore?.preferred_username}</p>
48
  </div>
49
  </div>
50
  {:else}
 
68
  </p>
69
  <Input
70
  value={model.id}
71
+ placeholder="{`${$userStore?.preferred_username ?? 'enzostvs'}/`}"
72
  prefix="huggingface.co/"
73
  onChange={(value) => model.id = value}
74
  />
src/lib/components/sidebar/Sidebar.svelte CHANGED
@@ -1,7 +1,6 @@
1
  <script lang="ts">
2
  import cookies from 'js-cookie';
3
  import Icon from "@iconify/svelte"
4
- import { get } from 'svelte/store';
5
  import { page } from '$app/stores';
6
 
7
  import { userStore, openWindowLogin } from "$lib/stores/use-user";
@@ -12,7 +11,6 @@
12
  import { galleryStore } from '$lib/stores/use-gallery';
13
 
14
  let isOpen = false;
15
- let user = get(userStore);
16
 
17
  const handleClick = () => {
18
  const app = document.getElementById("app");
@@ -58,7 +56,7 @@
58
  {menu.label}
59
  </Menu>
60
  {/each}
61
- {#if user?.sub}
62
  <Menu href="/saved-generations">
63
  <Icon icon="mdi:heart" class="w-5 h-5" />
64
  Saved generations
@@ -72,18 +70,18 @@
72
  </Menu>
73
  </div>
74
  </div>
75
- {#if user?.picture}
76
  <footer class="text-white text-center text-base pb-8 px-8 flex items-center justify-between gap-4">
77
  <div class="flex items-center justify-start gap-4">
78
- <img src={user?.picture?.startsWith('http') ? user?.picture : `https://huggingface.co${user?.picture}`} alt="User avatar" class="w-10 h-10 rounded-full border-2 border-white inline-block" />
79
  <div class="w-full text-left">
80
  <p class="text-lg font-semibold">
81
- {user.name}
82
- {#if user?.is_admin}
83
  <span class="text-yellow-400 bg-yellow-500 bg-opacity-20 rounded-lg px-2 py-1 text-xs font-semibold ml-1">HF Staff</span>
84
  {/if}
85
  </p>
86
- <p class="text-sm leading-none text-neutral-400">{user.preferred_username}</p>
87
  </div>
88
  </div>
89
  <button on:click={logout}>
 
1
  <script lang="ts">
2
  import cookies from 'js-cookie';
3
  import Icon from "@iconify/svelte"
 
4
  import { page } from '$app/stores';
5
 
6
  import { userStore, openWindowLogin } from "$lib/stores/use-user";
 
11
  import { galleryStore } from '$lib/stores/use-gallery';
12
 
13
  let isOpen = false;
 
14
 
15
  const handleClick = () => {
16
  const app = document.getElementById("app");
 
56
  {menu.label}
57
  </Menu>
58
  {/each}
59
+ {#if $userStore?.sub}
60
  <Menu href="/saved-generations">
61
  <Icon icon="mdi:heart" class="w-5 h-5" />
62
  Saved generations
 
70
  </Menu>
71
  </div>
72
  </div>
73
+ {#if $userStore?.picture}
74
  <footer class="text-white text-center text-base pb-8 px-8 flex items-center justify-between gap-4">
75
  <div class="flex items-center justify-start gap-4">
76
+ <img src={$userStore?.picture?.startsWith('http') ? $userStore?.picture : `https://huggingface.co${$userStore?.picture}`} alt="User avatar" class="w-10 h-10 rounded-full border-2 border-white inline-block" />
77
  <div class="w-full text-left">
78
  <p class="text-lg font-semibold">
79
+ {$userStore.name}
80
+ {#if $userStore?.is_admin}
81
  <span class="text-yellow-400 bg-yellow-500 bg-opacity-20 rounded-lg px-2 py-1 text-xs font-semibold ml-1">HF Staff</span>
82
  {/if}
83
  </p>
84
+ <p class="text-sm leading-none text-neutral-400">{$userStore.preferred_username}</p>
85
  </div>
86
  </div>
87
  <button on:click={logout}>
src/routes/+page.svelte CHANGED
@@ -15,7 +15,6 @@
15
  import Drawer from "$lib/components/models/drawer/Drawer.svelte";
16
  import { onMount } from "svelte";
17
  import type { ModelCard } from "$lib/type";
18
- import { get } from "svelte/store";
19
  import { userStore } from "$lib/stores/use-user";
20
  import Add from "$lib/components/community/reactions/Add.svelte";
21
 
@@ -26,7 +25,6 @@
26
  models: [],
27
  total_items: 0,
28
  }
29
- let user = get(userStore);
30
 
31
  let form: Record<string, string> = {
32
  filter: $page.url.searchParams.get('filter') ?? "hotest",
@@ -93,7 +91,7 @@
93
  <Radio
94
  options={[
95
  ...MODELS_FILTER_OPTIONS,
96
- ...(user?.is_admin ? [
97
  {
98
  label: "Staff only",
99
  value: "staff_only",
 
15
  import Drawer from "$lib/components/models/drawer/Drawer.svelte";
16
  import { onMount } from "svelte";
17
  import type { ModelCard } from "$lib/type";
 
18
  import { userStore } from "$lib/stores/use-user";
19
  import Add from "$lib/components/community/reactions/Add.svelte";
20
 
 
25
  models: [],
26
  total_items: 0,
27
  }
 
28
 
29
  let form: Record<string, string> = {
30
  filter: $page.url.searchParams.get('filter') ?? "hotest",
 
91
  <Radio
92
  options={[
93
  ...MODELS_FILTER_OPTIONS,
94
+ ...($userStore?.is_admin ? [
95
  {
96
  label: "Staff only",
97
  value: "staff_only",
src/routes/gallery/+page.svelte CHANGED
@@ -9,12 +9,10 @@
9
  import { COMMUNITY_FILTER_OPTIONS } from "$lib/utils/index.js";
10
  import GoTop from "$lib/components/GoTop.svelte";
11
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
12
- import { get } from "svelte/store";
13
  import { userStore } from "$lib/stores/use-user";
14
  import { onMount } from "svelte";
15
  import type { CommunityCard } from "$lib/type";
16
 
17
- let user = get(userStore);
18
  let data: {
19
  cards: CommunityCard[],
20
  total_items: number,
@@ -88,7 +86,7 @@
88
  <!-- mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-5 mt-8 lg:mt-10 -->
89
  <div class="mx-auto flex flex-wrap gap-5 mt-8 lg:mt-10 justify-center md:justify-start">
90
  {#each data.cards as card}
91
- <Card card={card} form={form} displayDelete={user?.is_admin} />
92
  {/each}
93
  <InfiniteScroll
94
  elementScroll="{elementScroll ?? undefined}"
 
9
  import { COMMUNITY_FILTER_OPTIONS } from "$lib/utils/index.js";
10
  import GoTop from "$lib/components/GoTop.svelte";
11
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
 
12
  import { userStore } from "$lib/stores/use-user";
13
  import { onMount } from "svelte";
14
  import type { CommunityCard } from "$lib/type";
15
 
 
16
  let data: {
17
  cards: CommunityCard[],
18
  total_items: number,
 
86
  <!-- mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-4 3xl:grid-cols-5 gap-5 mt-8 lg:mt-10 -->
87
  <div class="mx-auto flex flex-wrap gap-5 mt-8 lg:mt-10 justify-center md:justify-start">
88
  {#each data.cards as card}
89
+ <Card card={card} form={form} displayDelete={$userStore?.is_admin} />
90
  {/each}
91
  <InfiniteScroll
92
  elementScroll="{elementScroll ?? undefined}"