Skip to content

Commit b89cb4d

Browse files
committed
Merge branch 'feat/add-more-error-toasts'
2 parents 8b3386e + b56d24d commit b89cb4d

File tree

6 files changed

+41
-20
lines changed

6 files changed

+41
-20
lines changed

src/drawControllers/svg.drawController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Point} from '@flatten-js/core';
22
import {DEFAULT_TEXT_OPTIONS, type DrawController} from './DrawController';
33
import {SVG_MARGIN} from '../App.consts.ts';
4+
import {toast} from 'react-toastify';
45
import type {TextOptions} from '../entities/TextEntity.ts';
56
import {triggerReactUpdate} from '../state.ts';
67
import {StateVariable} from '../helpers/undo-stack.ts';
@@ -228,6 +229,7 @@ export class SvgDrawController implements DrawController {
228229
canvas.height = imageElement.height;
229230
const ctx = canvas.getContext('2d');
230231
if (!ctx) {
232+
toast.warn('Failed to create canvas context');
231233
console.warn('Failed to create canvas context');
232234
return;
233235
}

src/helpers/convert-svg-path-to-line-segments.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {toast} from 'react-toastify';
2+
13
// A small type alias for clarity.
24
type Point = { x: number; y: number };
35

@@ -455,6 +457,7 @@ export function svgPathToSegments(
455457
break;
456458
}
457459
default: {
460+
toast.error(`Unsupported SVG command type: ${type} ${args.join(' ')}`);
458461
console.error(`unsupported SVG command type: ${type} ${args.join(' ')}`);
459462
// Unsupported commands can be skipped.
460463
idx = args.length;

src/helpers/draw-functions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import type {DrawController} from '../drawControllers/DrawController';
55
import type {ScreenCanvasDrawController} from '../drawControllers/screenCanvas.drawController';
66
import type {Entity} from '../entities/Entity';
77
import {getLayers, isEntityHighlighted, isEntitySelected} from '../state';
8+
import {toast} from 'react-toastify';
89

910
export function drawEntities(drawController: DrawController, entities: Entity[]) {
1011
for (const entity of entities) {
1112
const layer = getLayers().find((layer) => layer.id === entity.layerId);
1213
if (!layer) {
14+
toast.error(`Failed to find layer for entity: ${entity?.id}`);
1315
console.error('Failed to find layer for entity: ', entity);
1416
continue;
1517
}

src/helpers/import-export-handlers/import-entities-from-svg.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {toast} from 'react-toastify';
12
import {CircleEntity} from '../../entities/CircleEntity';
23
import type {Entity} from '../../entities/Entity';
34
import {LineEntity} from '../../entities/LineEntity';
@@ -12,6 +13,7 @@ import {middle} from '../middle.ts';
1213

1314
function svgChildrenToEntities(root: RootNode): Entity[] {
1415
if (!root.children || !root.children?.[0]) {
16+
toast.error('Failed to load SVG file since it appears to be empty');
1517
console.error(new Error('Empty SVG file'));
1618
return [];
1719
}
@@ -84,6 +86,9 @@ function svgChildrenToEntities(root: RootNode): Entity[] {
8486
entities.push(new LineEntity(getActiveLayerId(), startPoint, endPoint));
8587
} else {
8688
// stop
89+
toast.error(
90+
`Error processing SVG polygon: expected an even number of points, but got ${coords.length}`
91+
);
8792
console.error(`expected even number of points but got: ${coords.length}`);
8893
}
8994
}
@@ -108,31 +113,36 @@ export function importEntitiesFromSvgFile(file: File | null | undefined) {
108113

109114
const reader = new FileReader();
110115
reader.addEventListener('load', async () => {
111-
const svg = reader.result as string;
116+
try {
117+
const svg = reader.result as string;
112118

113-
const data = parse(svg);
119+
const data = parse(svg);
114120

115-
const svgEntities: Entity[] = svgChildrenToEntities(data);
121+
const svgEntities: Entity[] = svgChildrenToEntities(data);
116122

117-
// We still need to flip the image top to bottom since the coordinate system of svg has a y-axis that goes down
118-
// And the world coordinate system of this application has a mathematical y-axis that goes up
119-
const boundingBox = getBoundingBoxOfMultipleEntities(svgEntities);
120-
const centerPoint = new Point(
121-
middle(boundingBox.minX, boundingBox.maxX),
122-
middle(boundingBox.minY, boundingBox.maxY)
123-
);
123+
// We still need to flip the image top to bottom since the coordinate system of svg has a y-axis that goes down
124+
// And the world coordinate system of this application has a mathematical y-axis that goes up
125+
const boundingBox = getBoundingBoxOfMultipleEntities(svgEntities);
126+
const centerPoint = new Point(
127+
middle(boundingBox.minX, boundingBox.maxX),
128+
middle(boundingBox.minY, boundingBox.maxY)
129+
);
124130

125-
const mirrorAxis = new LineEntity(
126-
getActiveLayerId(),
127-
centerPoint,
128-
new Point(centerPoint.x + 1, centerPoint.y)
129-
);
130-
for (const svgEntity of svgEntities) {
131-
svgEntity.mirror(mirrorAxis);
132-
}
131+
const mirrorAxis = new LineEntity(
132+
getActiveLayerId(),
133+
centerPoint,
134+
new Point(centerPoint.x + 1, centerPoint.y)
135+
);
136+
for (const svgEntity of svgEntities) {
137+
svgEntity.mirror(mirrorAxis);
138+
}
133139

134-
setEntities([...getEntities(), ...svgEntities]);
135-
resolve();
140+
setEntities([...getEntities(), ...svgEntities]);
141+
resolve();
142+
} catch (error) {
143+
toast.error('Failed to load SVG file');
144+
console.error(error);
145+
}
136146
});
137147
reader.readAsText(file, 'utf-8');
138148
});

src/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {Point} from '@flatten-js/core';
22
import {isEqual} from 'es-toolkit';
33
import type {Actor, MachineSnapshot} from 'xstate';
4+
import {toast} from 'react-toastify';
45
import {type HoverPoint, HtmlEvent, type Layer, type SnapPoint, type StateMetaData,} from './App.types';
56
import type {ScreenCanvasDrawController} from './drawControllers/screenCanvas.drawController';
67
import type {Entity} from './entities/Entity';
@@ -214,6 +215,7 @@ export const setActiveToolActor = (
214215
setLastStateInstructions(stateInstructions || null);
215216
},
216217
error: (err) => {
218+
toast.error(`Error in tool actor: ${err?.message}`);
217219
console.error('Error in tool actor', {err, newToolActor});
218220
}
219221
}

src/tools/select-tool.helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
SELECTION_RECTANGLE_WIDTH,
1010
} from '../App.consts';
1111
import {RectangleEntity} from '../entities/RectangleEntity';
12+
import {toast} from 'react-toastify';
1213
import {findClosestEntity} from '../helpers/find-closest-entity';
1314
import {
1415
getActiveLayerId,
@@ -74,6 +75,7 @@ export function selectEntitiesInsideRectangle(
7475
getEntities().map((entity): string | null => {
7576
const layer = getLayers().find((layer) => layer.id === entity.layerId);
7677
if (!layer) {
78+
toast.error(`Failed to find layer for entity: ${entity?.id}`);
7779
console.error('Failed to find layer for entity', entity);
7880
return null;
7981
}

0 commit comments

Comments
 (0)