Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/src/components/figures/FigureCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@

updateFigure.mutate(
{ id: figure.id, data: { styled_url: result.path, status: 'styled' } },
{ onSuccess: () => toast.success('Styled version uploaded') },
{
onSuccess: () => toast.success('Styled version uploaded'),
// The file is already in storage; only this row makes it the figure's
// styled version. Failing quietly leaves the card showing "Upload
// styled" over an upload that appeared to work.
onError: (err) => toast.error(err.message),
},
);
}

Expand All @@ -65,7 +71,7 @@
<div className="flex gap-3 sm:gap-5">
{/* Original photo */}
<div className="w-28 h-28 sm:w-36 sm:h-36 rounded-xl overflow-hidden bg-white/[0.03] border border-white/[0.04] shrink-0">
<img

Check warning on line 74 in app/src/components/figures/FigureCard.tsx

View workflow job for this annotation

GitHub Actions / verify

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element
src={originalUrl}
alt={figure.label ?? 'Figure'}
className="w-full h-full object-cover"
Expand All @@ -76,7 +82,7 @@
<div className="w-28 h-28 sm:w-36 sm:h-36 rounded-xl overflow-hidden bg-white/[0.03] border border-white/[0.04] shrink-0 relative">
{styledUrl ? (
<>
<img src={styledUrl} alt="Styled" className="w-full h-full object-cover" />

Check warning on line 85 in app/src/components/figures/FigureCard.tsx

View workflow job for this annotation

GitHub Actions / verify

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element
<Badge
className="absolute bottom-2 left-2 text-xs rounded-full"
variant="secondary"
Expand Down
82 changes: 82 additions & 0 deletions app/src/hooks/mutations-report-failure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, it } from 'vitest';
import { readFileSync, readdirSync } from 'node:fs';
import { join, relative } from 'node:path';
import ts from 'typescript';

/**
* Three separate saves shipped with no failure path: figure placement in the
* compose editor (#36), a figure's label and delete (e399f4a), and the styled
* upload on FigureCard. Each looked identical in the UI whether it stored the
* work or dropped it — the user was told nothing and lost the edit on refresh.
*
* A mutation that cannot report failure is a save the user cannot trust, so
* this walks every `.mutate(...)` / `.mutateAsync(...)` call site in the app
* and fails on any that passes no `onError`. Fixing the fourth instance by
* hand is what this test exists to prevent.
*/
describe('every mutation call site reports its failures', () => {
const srcDir = join(process.cwd(), 'src');

function filesUnder(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const path = join(dir, entry.name);
if (entry.isDirectory()) return filesUnder(path);
if (!/\.tsx?$/.test(entry.name) || /\.test\.tsx?$/.test(entry.name)) return [];
return [path];
});
}

/** react-query takes the options object as the last argument of either call. */
function handlesError(call: ts.CallExpression): boolean {
return call.arguments.some(
(arg) =>
ts.isObjectLiteralExpression(arg) &&
arg.properties.some((prop) => prop.name?.getText(arg.getSourceFile()) === 'onError'),
);
}

function unhandledIn(file: string): string[] {
const source = ts.createSourceFile(
file,
readFileSync(file, 'utf8'),
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TSX,
);
const unhandled: string[] = [];

function visit(node: ts.Node) {
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
(node.expression.name.text === 'mutate' || node.expression.name.text === 'mutateAsync') &&
!handlesError(node)
) {
const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
unhandled.push(`${relative(srcDir, file)}:${line} ${node.expression.getText(source)}`);
}
ts.forEachChild(node, visit);
}

visit(source);
return unhandled;
}

const sources = filesUnder(srcDir);

it('finds the app sources', () => {
expect(sources.length).toBeGreaterThan(0);
});

it('finds the mutation call sites it is meant to guard', () => {
const calls = sources.flatMap((file) => {
const text = readFileSync(file, 'utf8');
return text.match(/\.mutate(Async)?\(/g) ?? [];
});
expect(calls.length).toBeGreaterThan(0);
});

it('leaves no mutation without an onError', () => {
expect(sources.flatMap(unhandledIn)).toEqual([]);
});
});
Loading