enzostvs HF staff commited on
Commit
83deba1
β€’
1 Parent(s): 83d9f5e

keep model in URL to be able to share it or refresh

Browse files
src/lib/components/models/Card.svelte CHANGED
@@ -2,6 +2,8 @@
2
  import { modelStore } from "$lib/stores/use-model";
3
  import type { ModelCard } from "$lib/type";
4
  import Icon from "@iconify/svelte";
 
 
5
 
6
  export let card: ModelCard;
7
 
@@ -9,6 +11,8 @@
9
  const request = await fetch(`/api/models/${card?.id?.replace("/", "@")}?full=true`);
10
  const { model } = await request.json();
11
  modelStore.set(model);
 
 
12
  };
13
  </script>
14
 
 
2
  import { modelStore } from "$lib/stores/use-model";
3
  import type { ModelCard } from "$lib/type";
4
  import Icon from "@iconify/svelte";
5
+ import { goto } from "$app/navigation";
6
+ import { page } from "$app/stores";
7
 
8
  export let card: ModelCard;
9
 
 
11
  const request = await fetch(`/api/models/${card?.id?.replace("/", "@")}?full=true`);
12
  const { model } = await request.json();
13
  modelStore.set(model);
14
+ $page.url.searchParams.set('model', card?.id);
15
+ goto(`?${$page.url.searchParams.toString()}`);
16
  };
17
  </script>
18
 
src/lib/components/models/drawer/Drawer.svelte CHANGED
@@ -1,6 +1,7 @@
1
  <script lang="ts">
2
  import { clickoutside } from '@svelte-put/clickoutside';
3
-
 
4
  import { get } from "svelte/store";
5
  import Icon from "@iconify/svelte";
6
 
@@ -16,6 +17,9 @@
16
 
17
  const handleClose = () => {
18
  modelStore.set(undefined);
 
 
 
19
  };
20
  </script>
21
 
 
1
  <script lang="ts">
2
  import { clickoutside } from '@svelte-put/clickoutside';
3
+ import { goto } from "$app/navigation";
4
+ import { page } from "$app/stores";
5
  import { get } from "svelte/store";
6
  import Icon from "@iconify/svelte";
7
 
 
17
 
18
  const handleClose = () => {
19
  modelStore.set(undefined);
20
+
21
+ $page.url.searchParams.delete('model');
22
+ goto(`?${$page.url.searchParams.toString()}`);
23
  };
24
  </script>
25
 
src/routes/+page.svelte CHANGED
@@ -1,6 +1,8 @@
1
  <script lang="ts">
2
  import { browser } from "$app/environment";
3
  import InfiniteScroll from "svelte-infinite-scroll";
 
 
4
  import Button from "$lib/components/Button.svelte";
5
  import Card from "$lib/components/models/Card.svelte";
6
  import Input from "$lib/components/fields/Input.svelte";
@@ -23,6 +25,10 @@
23
 
24
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
25
 
 
 
 
 
26
  const handleFetchMore = async () => {
27
  form = {...form, page: (Number(form.page) + 1).toString()};
28
  refetch(true);
@@ -41,7 +47,7 @@
41
  const refetch = async (add: boolean) => {
42
  const request = await fetch(`/api/models?${new URLSearchParams(form)}`);
43
  const response = await request.json();
44
- if (add) data = {...data, cards: [...data.cards, ...response.cards ]};
45
  else data = response;
46
  }
47
  </script>
@@ -56,7 +62,7 @@
56
  <SubmitModel onClose={() => submitModelDialog = false} />
57
  </Dialog>
58
  <h1 class="text-white font-semibold text-2xl">
59
- Explore Models ({data.total_items})
60
  </h1>
61
  <div class="flex items-start sm:items-center justify-between mt-5 flex-col sm:flex-row gap-5 sm:justify-between">
62
  <Radio options={MODELS_FILTER_OPTIONS} value="{form.filter}" onChange={handleChangeFilter} />
@@ -93,13 +99,13 @@
93
  <Input value={form.search} placeholder="Filter by model name" onChange={handleChangeSearch} />
94
  </div>
95
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
96
- {#each data.cards as card}
97
  <Card card={card} />
98
  {/each}
99
  <InfiniteScroll
100
  elementScroll="{elementScroll ?? undefined}"
101
  threshold={100}
102
- hasMore={data.total_items > data.cards.length}
103
  on:loadMore={handleFetchMore}
104
  />
105
  <GoTop />
 
1
  <script lang="ts">
2
  import { browser } from "$app/environment";
3
  import InfiniteScroll from "svelte-infinite-scroll";
4
+
5
+ import { modelStore } from "$lib/stores/use-model.js";
6
  import Button from "$lib/components/Button.svelte";
7
  import Card from "$lib/components/models/Card.svelte";
8
  import Input from "$lib/components/fields/Input.svelte";
 
25
 
26
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
27
 
28
+ if (data?.model?.id) {
29
+ modelStore.set(data.model);
30
+ }
31
+
32
  const handleFetchMore = async () => {
33
  form = {...form, page: (Number(form.page) + 1).toString()};
34
  refetch(true);
 
47
  const refetch = async (add: boolean) => {
48
  const request = await fetch(`/api/models?${new URLSearchParams(form)}`);
49
  const response = await request.json();
50
+ if (add) data = {...data, models: [...data.models, ...response.models ]};
51
  else data = response;
52
  }
53
  </script>
 
62
  <SubmitModel onClose={() => submitModelDialog = false} />
63
  </Dialog>
64
  <h1 class="text-white font-semibold text-2xl">
65
+ Explore Models ({data?.total_items ?? 0})
66
  </h1>
67
  <div class="flex items-start sm:items-center justify-between mt-5 flex-col sm:flex-row gap-5 sm:justify-between">
68
  <Radio options={MODELS_FILTER_OPTIONS} value="{form.filter}" onChange={handleChangeFilter} />
 
99
  <Input value={form.search} placeholder="Filter by model name" onChange={handleChangeSearch} />
100
  </div>
101
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
102
+ {#each data.models as card}
103
  <Card card={card} />
104
  {/each}
105
  <InfiniteScroll
106
  elementScroll="{elementScroll ?? undefined}"
107
  threshold={100}
108
+ hasMore={data.total_items > data.models.length}
109
  on:loadMore={handleFetchMore}
110
  />
111
  <GoTop />
src/routes/+page.ts CHANGED
@@ -1,10 +1,29 @@
1
- export async function load({ fetch }) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  const response = await fetch("/api/models?page=0&filter=hotest", {
3
  method: "GET",
4
  headers: {
5
  "Content-Type": "application/json"
6
  }
7
  })
8
- const data = await response.json()
9
- return data
 
 
 
 
10
  }
 
1
+ export async function load({ fetch, url }) {
2
+ const model_param = url.searchParams.get("model")
3
+
4
+ let model;
5
+
6
+ if (model_param) {
7
+ const model_request = await fetch(`/api/models/${model_param?.replace("/", "@")}`, {
8
+ method: "GET",
9
+ headers: {
10
+ "Content-Type": "application/json"
11
+ }
12
+ })
13
+ const model_response = await model_request?.clone().json().catch(() => null);
14
+ model = model_response
15
+ }
16
+
17
  const response = await fetch("/api/models?page=0&filter=hotest", {
18
  method: "GET",
19
  headers: {
20
  "Content-Type": "application/json"
21
  }
22
  })
23
+ const models = await response.json()
24
+ return {
25
+ models: models?.cards ?? [],
26
+ total_items: models?.total_items ?? 0,
27
+ model: model?.model ?? null
28
+ }
29
  }