TypeScript
perf(web): the new project hook compiles without manual memoization (#20675)
Julius Marminge36c1fe50ac97
diff --git a/apps/web/src/hooks/useNewProject.ts b/apps/web/src/hooks/useNewProject.tsindex 54bcd19d069..095d0924b85 100644--- a/apps/web/src/hooks/useNewProject.ts+++ b/apps/web/src/hooks/useNewProject.ts@@ -8,7 +8,6 @@ import {squashAtomCommandFailure,} from "@t3tools/client-runtime/state/runtime";import type { EnvironmentId } from "@t3tools/contracts";-import { useCallback } from "react";import { stackedThreadToast, toastManager } from "~/components/ui/toast";import { waitForProject } from "~/state/entities";@@ -30,113 +29,107 @@ export function useNewProject() {});const handleNewThread = useNewThreadHandler();- const publishNewRepository = useCallback(- async (input: {- readonly environmentId: EnvironmentId;- readonly workspaceRoot: string;- readonly target: NewProjectPublishTarget;- }) => {- const { label } = input.target.definition;- const result = await publishRepository({- environmentId: input.environmentId,- input: {- cwd: input.workspaceRoot,- provider: input.target.definition.kind,- repository: getNewProjectRepository(input.target, input.workspaceRoot),- visibility: "private",- },- });- if (result._tag === "Failure") {- if (!isAtomCommandInterrupted(result)) {- toastManager.add(- stackedThreadToast({- type: "error",- title: `Could not create the ${label} repository`,- description: `${errorMessage(squashAtomCommandFailure(result))} Use Publish Repository in the Git menu to try again.`,- }),- );- }- return;- }- toastManager.add(- stackedThreadToast({- type: "success",- title: `Published to ${label}`,- description: result.value.repository.nameWithOwner,- }),- );- },- [publishRepository],- );-- return useCallback(- async (input: {- readonly environmentId: EnvironmentId;- readonly name: string;- readonly publishTo: NewProjectPublishTarget | null;- }): Promise<boolean> => {- const result = await createNew({- environmentId: input.environmentId,- input: { name: input.name },- });- if (result._tag === "Failure") {- if (!isAtomCommandInterrupted(result)) {- toastManager.add(- stackedThreadToast({- type: "error",- title: "Could not create the project",- description: errorMessage(squashAtomCommandFailure(result)),- }),- );- }- return false;- }-- const { projectId, workspaceRoot, commitError } = result.value;- toastManager.add(- stackedThreadToast(- commitError === undefined- ? { type: "success", title: `Created ${input.name}`, description: workspaceRoot }- : {- type: "warning",- title: `Created ${input.name} without a first commit`,- description: `${commitError} The project is in ${workspaceRoot}.`,- },- ),- );- if (input.publishTo) {- void publishNewRepository({- environmentId: input.environmentId,- workspaceRoot,- target: input.publishTo,- });- }-- const projectRef = scopeProjectRef(input.environmentId, projectId);- // Drafts key off the project's stored path, so wait for the create event- // to reach the store before opening one.- const project = await waitForProject(projectRef).catch((error: unknown) => {+ const publishNewRepository = async (input: {+ readonly environmentId: EnvironmentId;+ readonly workspaceRoot: string;+ readonly target: NewProjectPublishTarget;+ }) => {+ const { label } = input.target.definition;+ const result = await publishRepository({+ environmentId: input.environmentId,+ input: {+ cwd: input.workspaceRoot,+ provider: input.target.definition.kind,+ repository: getNewProjectRepository(input.target, input.workspaceRoot),+ visibility: "private",+ },+ });+ if (result._tag === "Failure") {+ if (!isAtomCommandInterrupted(result)) {toastManager.add(stackedThreadToast({type: "error",- title: "Failed to open project",- description: `${errorMessage(error)} It will appear in the sidebar once this client catches up.`,+ title: `Could not create the ${label} repository`,+ description: `${errorMessage(squashAtomCommandFailure(result))} Use Publish Repository in the Git menu to try again.`,}),);- return null;- });- if (project === null) return true;- await handleNewThread(projectRef).catch((error: unknown) => {+ }+ return;+ }+ toastManager.add(+ stackedThreadToast({+ type: "success",+ title: `Published to ${label}`,+ description: result.value.repository.nameWithOwner,+ }),+ );+ };++ return async (input: {+ readonly environmentId: EnvironmentId;+ readonly name: string;+ readonly publishTo: NewProjectPublishTarget | null;+ }): Promise<boolean> => {+ const result = await createNew({+ environmentId: input.environmentId,+ input: { name: input.name },+ });+ if (result._tag === "Failure") {+ if (!isAtomCommandInterrupted(result)) {toastManager.add(stackedThreadToast({type: "error",- title: "Failed to open project",- description: errorMessage(error),+ title: "Could not create the project",+ description: errorMessage(squashAtomCommandFailure(result)),}),);+ }+ return false;+ }++ const { projectId, workspaceRoot, commitError } = result.value;+ toastManager.add(+ stackedThreadToast(+ commitError === undefined+ ? { type: "success", title: `Created ${input.name}`, description: workspaceRoot }+ : {+ type: "warning",+ title: `Created ${input.name} without a first commit`,+ description: `${commitError} The project is in ${workspaceRoot}.`,+ },+ ),+ );+ if (input.publishTo) {+ void publishNewRepository({+ environmentId: input.environmentId,+ workspaceRoot,+ target: input.publishTo,});- return true;- },- [createNew, handleNewThread, publishNewRepository],- );+ }++ const projectRef = scopeProjectRef(input.environmentId, projectId);+ // Drafts key off the project's stored path, so wait for the create event+ // to reach the store before opening one.+ const project = await waitForProject(projectRef).catch((error: unknown) => {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Failed to open project",+ description: `${errorMessage(error)} It will appear in the sidebar once this client catches up.`,+ }),+ );+ return null;+ });+ if (project === null) return true;+ await handleNewThread(projectRef).catch((error: unknown) => {+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Failed to open project",+ description: errorMessage(error),+ }),+ );+ });+ return true;+ };}