From 47e665bc95b15a48ea5921e57ac9e23cb85bbf0a Mon Sep 17 00:00:00 2001 From: Nathan Sarrazin Date: Thu, 4 Jul 2024 01:15:04 +0200 Subject: [PATCH] better redirects & auto activate --- src/routes/tools/ToolEdit.svelte | 18 +++++++++++++++--- src/routes/tools/[toolId]/+page.svelte | 2 +- src/routes/tools/[toolId]/edit/+page.server.ts | 2 +- src/routes/tools/[toolId]/edit/+page.svelte | 4 +++- src/routes/tools/new/+page.server.ts | 7 +------ 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/routes/tools/ToolEdit.svelte b/src/routes/tools/ToolEdit.svelte index 5b6bb0b79aa..7cc43633c0a 100644 --- a/src/routes/tools/ToolEdit.svelte +++ b/src/routes/tools/ToolEdit.svelte @@ -10,6 +10,9 @@ import { colors, icons } from "$lib/utils/tools"; import { applyAction, enhance } from "$app/forms"; import { getGradioApi } from "$lib/utils/getGradioApi"; + import { useSettingsStore } from "$lib/stores/settings"; + import { goto } from "$app/navigation"; + import { base } from "$app/paths"; type ActionData = { error: boolean; @@ -40,8 +43,9 @@ let editableTool: CommunityToolEditable = tool ?? { displayName: "", description: "", - color: "blue", - icon: "wikis", + // random color & icon for new tools + color: colors[Math.floor(Math.random() * colors.length)], + icon: icons[Math.floor(Math.random() * icons.length)], baseUrl: "", endpoint: "", name: "process_image", @@ -136,6 +140,8 @@ return "str"; } } + + const settings = useSettingsStore();
{ formLoading = false; - await applyAction(result); + + if (result.type === "success" && result.data && typeof result.data.toolId === "string") { + $settings.tools = [...($settings.tools ?? []), result.data.toolId]; + goto(`${base}/tools/${result.data.toolId}`, { invalidateAll: true }); + } else { + await applyAction(result); + } }; }} > diff --git a/src/routes/tools/[toolId]/+page.svelte b/src/routes/tools/[toolId]/+page.svelte index 7482d245a68..f59e7b5d388 100644 --- a/src/routes/tools/[toolId]/+page.svelte +++ b/src/routes/tools/[toolId]/+page.svelte @@ -24,7 +24,7 @@ let previousPage: string = base; afterNavigate(({ from }) => { - if (!from?.url.pathname.includes("settings")) { + if (!from?.url.pathname.includes("tools/")) { previousPage = from?.url.toString() || previousPage; } }); diff --git a/src/routes/tools/[toolId]/edit/+page.server.ts b/src/routes/tools/[toolId]/edit/+page.server.ts index 3dfdddad266..c46ad947fd4 100644 --- a/src/routes/tools/[toolId]/edit/+page.server.ts +++ b/src/routes/tools/[toolId]/edit/+page.server.ts @@ -59,6 +59,6 @@ export const actions = { } ); - throw redirect(302, `${base}/tools`); + throw redirect(302, `${base}/tools/${tool._id.toString()}`); }, }; diff --git a/src/routes/tools/[toolId]/edit/+page.svelte b/src/routes/tools/[toolId]/edit/+page.svelte index a4d7ce8d455..5c631eb9fdf 100644 --- a/src/routes/tools/[toolId]/edit/+page.svelte +++ b/src/routes/tools/[toolId]/edit/+page.svelte @@ -10,7 +10,9 @@ export let form; afterNavigate(({ from }) => { - previousPage = from?.url.toString() || previousPage; + if (!from?.url.pathname.includes("tools/")) { + previousPage = from?.url.toString() || previousPage; + } }); diff --git a/src/routes/tools/new/+page.server.ts b/src/routes/tools/new/+page.server.ts index 5bc7c88931f..1a2b032d45b 100644 --- a/src/routes/tools/new/+page.server.ts +++ b/src/routes/tools/new/+page.server.ts @@ -65,12 +65,7 @@ export const actions = { featured: false, searchTokens: generateSearchTokens(parse.data.name), }); - // add insertedId to user settings - await collections.settings.updateOne(authCondition(locals), { - $addToSet: { tools: insertedId.toString() }, - }); - - throw redirect(302, `${base}/tools`); + return { toolId: insertedId.toString() }; }, };