GeezerVison is showing github.com — fix(web): preserve newer project action drafts when imports fail (#39… · pingdotgg/t3code@916fcdc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +72Lines changed: 72 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { EnvironmentId } from "@t3tools/contracts"; | | 2 | +import * as Cause from "effect/Cause"; | | 3 | +import { AsyncResult } from "effect/reactivity"; | | 4 | +import { expect, it } from "vite-plus/test"; | | 5 | +import { page, userEvent } from "vite-plus/test/browser"; | | 6 | + | | 7 | +import { act, render } from "../../test/render"; | | 8 | +import ProjectScriptsControl, { type ProjectScriptActionResult } from "./ProjectScriptsControl"; | | 9 | + | | 10 | +it.each(["edit", "dismiss", "newer import fails first"] as const)( | | 11 | + "keeps a failed import from replacing a newer action after %s", | | 12 | + async (nextAction) => { | | 13 | + const older = Promise.withResolvers<ProjectScriptActionResult>(); | | 14 | + const newer = Promise.withResolvers<ProjectScriptActionResult>(); | | 15 | + await using screen = await render( | | 16 | + <ProjectScriptsControl | | 17 | + environmentId={EnvironmentId.make("test")} | | 18 | + displayMode="panel" | | 19 | + scripts={[ | | 20 | + { | | 21 | + id: "local-check", | | 22 | + name: "Local check", | | 23 | + command: "echo local", | | 24 | + icon: "play", | | 25 | + runOnWorktreeCreate: false, | | 26 | + }, | | 27 | + ]} | | 28 | + fileScripts={[ | | 29 | + { name: "Shared build", command: "echo build" }, | | 30 | + { name: "Shared lint", command: "echo lint" }, | | 31 | + ]} | | 32 | + onRunScript={() => {}} | | 33 | + onAddScript={({ name }) => (name === "Shared build" ? older.promise : newer.promise)} | | 34 | + onUpdateScript={async () => AsyncResult.success(undefined)} | | 35 | + onDeleteScript={async () => AsyncResult.success(undefined)} | | 36 | + />, | | 37 | + ); | | 38 | + | | 39 | + const actions = screen.getByRole("button", { name: "Script actions" }); | | 40 | + const dialog = page.getByRole("dialog"); | | 41 | + const name = dialog.getByRole("textbox", { name: "Name" }); | | 42 | + const failure = AsyncResult.failure<void, unknown>( | | 43 | + Cause.fail(new Error("Settings write failed")), | | 44 | + ); | | 45 | + | | 46 | + await actions.click(); | | 47 | + await page.getByRole("menuitem", { name: /Shared build/ }).click(); | | 48 | + await actions.click(); | | 49 | + | | 50 | + switch (nextAction) { | | 51 | + case "edit": | | 52 | + await page.getByRole("button", { name: "Edit Local check" }).click(); | | 53 | + await act(async () => older.resolve(failure)); | | 54 | + expect(name.query()).toHaveProperty("value", "Local check"); | | 55 | + break; | | 56 | + case "dismiss": | | 57 | + await page.getByRole("menuitem", { name: "Add project script" }).click(); | | 58 | + await expect.element(dialog).toBeVisible(); | | 59 | + await userEvent.keyboard("{Escape}"); | | 60 | + await expect.element(dialog).not.toBeInTheDocument(); | | 61 | + await act(async () => older.resolve(failure)); | | 62 | + expect(dialog.query() === null).toBe(true); | | 63 | + break; | | 64 | + case "newer import fails first": | | 65 | + await page.getByRole("menuitem", { name: /Shared lint/ }).click(); | | 66 | + await act(async () => newer.resolve(failure)); | | 67 | + await act(async () => older.resolve(failure)); | | 68 | + expect(name.query()).toHaveProperty("value", "Shared lint"); | | 69 | + break; | | 70 | + } | | 71 | + }, | | 72 | +); |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ProjectScriptsControl.tsx +22-7Lines changed: 22 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,6 +47,7 @@ import { | 47 | 47 | MenuSubTrigger, | 48 | 48 | MenuSubPopup, | 49 | 49 | } from "./ui/menu"; | | 50 | +import { stackedThreadToast, toastManager } from "./ui/toast"; | 50 | 51 | import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; | 51 | 52 | import { cn } from "~/lib/utils"; | 52 | 53 | import { | @@ -108,6 +109,7 @@ export default function ProjectScriptsControl({ | 108 | 109 | setActionsMenuOpen({ presentation, scripts: false, imports: false }); | 109 | 110 | } | 110 | 111 | const [editorRequest, setEditorRequest] = useState<ProjectScriptEditorRequest | null>(null); | | 112 | + const editorRequestIdRef = React.useRef(0); | 111 | 113 |
| 112 | 114 | const primaryScript = | 113 | 115 | (preferredScriptId ? scripts.find((script) => script.id === preferredScriptId) : undefined) ?? | @@ -122,10 +124,12 @@ export default function ProjectScriptsControl({ | 122 | 124 | ); | 123 | 125 |
| 124 | 126 | const openAddDialog = () => { | | 127 | + editorRequestIdRef.current += 1; | 125 | 128 | setEditorRequest({ scriptId: null, initial: EMPTY_PROJECT_SCRIPT_INPUT }); | 126 | 129 | }; | 127 | 130 |
| 128 | 131 | const openEditDialog = (script: ProjectScript) => { | | 132 | + editorRequestIdRef.current += 1; | 129 | 133 | onRequestMenuClose?.(); | 130 | 134 | setActionsMenuOpen({ presentation, scripts: false, imports: false }); | 131 | 135 | setEditorRequest(editorRequestForScript(script, keybindings)); | @@ -135,6 +139,7 @@ export default function ProjectScriptsControl({ | 135 | 139 | scriptId === null ? onAddScript(input) : onUpdateScript(scriptId, input); | 136 | 140 |
| 137 | 141 | const importFileScript = async (fileScript: T3ProjectFileScript) => { | | 142 | + const requestId = ++editorRequestIdRef.current; | 138 | 143 | const payload: NewProjectScriptInput = { | 139 | 144 | name: fileScript.name, | 140 | 145 | command: fileScript.command, | @@ -148,14 +153,24 @@ export default function ProjectScriptsControl({ | 148 | 153 | autoOpenPreview: fileScript.previewUrl ? (fileScript.autoOpenPreview ?? false) : false, | 149 | 154 | }; | 150 | 155 | const result = await onAddScript(payload); | 151 | | - if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { | 152 | | - const error = squashAtomCommandFailure(result); | 153 | | - setEditorRequest({ | 154 | | - scriptId: null, | 155 | | - initial: payload, | 156 | | - error: thrownErrorMessage(error, m) ?? gt("Failed to import action."), | 157 | | - }); | | 156 | + if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return; | | 157 | + | | 158 | + const message = thrownErrorMessage(squashAtomCommandFailure(result), m); | | 159 | + if (requestId !== editorRequestIdRef.current) { | | 160 | + toastManager.add( | | 161 | + stackedThreadToast({ | | 162 | + type: "error", | | 163 | + title: gt("Failed to import {name}.", { name: fileScript.name }), | | 164 | + ...(message ? { description: message } : {}), | | 165 | + }), | | 166 | + ); | | 167 | + return; | 158 | 168 | } | | 169 | + setEditorRequest({ | | 170 | + scriptId: null, | | 171 | + initial: payload, | | 172 | + error: message ?? gt("Failed to import action."), | | 173 | + }); | 159 | 174 | }; | 160 | 175 |
| 161 | 176 | const importMenuItems = importableScripts.length > 0 && ( | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/de.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "{name} loslösen", | 2195 | 2195 | "c4c2d240ffe55dec": "{name} anheften", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "{name} konnte nicht importiert werden.", | 2197 | 2198 | "924d4b8e011c9127": "Aktion konnte nicht importiert werden.", | 2198 | 2199 | "0421f870f82997bc": "Importieren", | 2199 | 2200 | "06690d4ce0d19c30": "{name} bearbeiten", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/en.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "Unpin {name}", | 2195 | 2195 | "c4c2d240ffe55dec": "Pin {name}", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "Failed to import {name}.", | 2197 | 2198 | "924d4b8e011c9127": "Failed to import action.", | 2198 | 2199 | "0421f870f82997bc": "Import", | 2199 | 2200 | "06690d4ce0d19c30": "Edit {name}", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/es.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "Desfijar {name}", | 2195 | 2195 | "c4c2d240ffe55dec": "Fijar {name}", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "No se pudo importar {name}.", | 2197 | 2198 | "924d4b8e011c9127": "No se pudo importar la acción.", | 2198 | 2199 | "0421f870f82997bc": "Importar", | 2199 | 2200 | "06690d4ce0d19c30": "Editar {name}", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/fr.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "Désépingler {name}", | 2195 | 2195 | "c4c2d240ffe55dec": "Épingler {name}", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "Échec de l’import de « {name} ».", | 2197 | 2198 | "924d4b8e011c9127": "Échec de l’import de l’action.", | 2198 | 2199 | "0421f870f82997bc": "Importer", | 2199 | 2200 | "06690d4ce0d19c30": "Modifier {name}", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/ja.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2192 | 2192 | "63977cd5e4535393": "{name} のピン留めを解除", | 2193 | 2193 | "c4c2d240ffe55dec": "{name} をピン留め", | 2194 | 2194 | "a1190e56375f2032": "{action} ({shortcut})", | | 2195 | + "f16b6d8ef68bab61": "「{name}」をインポートできませんでした。", | 2195 | 2196 | "924d4b8e011c9127": "アクションをインポートできませんでした。", | 2196 | 2197 | "0421f870f82997bc": "インポート", | 2197 | 2198 | "06690d4ce0d19c30": "{name} を編集", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/ko.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2193 | 2193 | "63977cd5e4535393": "{name} 고정 해제", | 2194 | 2194 | "c4c2d240ffe55dec": "{name} 고정", | 2195 | 2195 | "a1190e56375f2032": "{action}({shortcut})", | | 2196 | + "f16b6d8ef68bab61": "‘{name}’을(를) 가져오지 못했습니다.", | 2196 | 2197 | "924d4b8e011c9127": "작업을 가져오지 못했습니다.", | 2197 | 2198 | "0421f870f82997bc": "가져오기", | 2198 | 2199 | "06690d4ce0d19c30": "{name} 편집", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/pt-BR.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "Desafixar {name}", | 2195 | 2195 | "c4c2d240ffe55dec": "Fixar {name}", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "Não foi possível importar {name}.", | 2197 | 2198 | "924d4b8e011c9127": "Falha ao importar a ação.", | 2198 | 2199 | "0421f870f82997bc": "Importar", | 2199 | 2200 | "06690d4ce0d19c30": "Editar {name}", | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/i18n/translations/ru.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2194 | 2194 | "63977cd5e4535393": "Открепить {name}", | 2195 | 2195 | "c4c2d240ffe55dec": "Закрепить {name}", | 2196 | 2196 | "a1190e56375f2032": "{action} ({shortcut})", | | 2197 | + "f16b6d8ef68bab61": "Не удалось импортировать «{name}».", | 2197 | 2198 | "924d4b8e011c9127": "Не удалось импортировать действие.", | 2198 | 2199 | "0421f870f82997bc": "Импортировать", | 2199 | 2200 | "06690d4ce0d19c30": "Изменить {name}", | |
|