Skip to content

Commit

Permalink
fix: sonarqube scanner reported bug, code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsengyushiang committed Nov 6, 2023
1 parent 3907719 commit 1612903
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 66 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM node:18-alpine AS base

# Stage 1: Build the application
FROM node:18-alpine as builder
FROM base AS builder

# Set the working directory
WORKDIR /app
Expand All @@ -8,13 +10,13 @@ WORKDIR /app
ENV NODE_ENV production

# Copy source code
COPY ./ ./
COPY . .

# Install and build
RUN yarn && yarn build

# Stage 2: Create the final image
FROM node:18-alpine
FROM base AS runner

# Set the working directory
WORKDIR /app
Expand Down
5 changes: 3 additions & 2 deletions apps/editors/decoration/ModeSwitch/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from "uuid";
import { useEffect } from "react";
import Toolbar from "../../../../components/Toolbar";
import Icons from "../../../../components/Icon";
Expand All @@ -13,9 +14,9 @@ const ModeSwitch = ({ mode, setMode, data }) => {
const changeMode = (mode) => () => setMode(mode);
return (
<Toolbar>
{data.map(({ Component, targetMode }, index) => (
{data.map(({ Component, targetMode }) => (
<Component
key={index}
key={uuid()}
$highlight={mode === targetMode}
onClick={changeMode(targetMode)}
/>
Expand Down
5 changes: 3 additions & 2 deletions components/MediaManager/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from "uuid";
import React from "react";
import {
Css3DObject,
Expand Down Expand Up @@ -68,8 +69,8 @@ const MediaManager = ({ three, data, readonly: globalReadonly }) => {
}
};

return data.map((prop, index) => (
<React.Fragment key={index}>{getMediaByType(prop)}</React.Fragment>
return data.map((prop) => (
<React.Fragment key={uuid()}>{getMediaByType(prop)}</React.Fragment>
));
};

Expand Down
3 changes: 2 additions & 1 deletion components/ObjectSelector/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from "uuid";
import { useState, forwardRef, useMemo } from "react";

import { MeshIndexMap } from "@pano-to-mesh/three";
Expand Down Expand Up @@ -39,7 +40,7 @@ const ObjectSelector = ({ media, mouse, ...props }, ref) => {
const Component = data.type === MEDIA_3D.MODEL ? Model : Basic;
return (
<Component
key={index}
key={uuid()}
type={data.type}
data={data.data}
transformation={data.transformation}
Expand Down
5 changes: 3 additions & 2 deletions components/RouteSwitch/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from "uuid";
import { useRouter } from "next/router";
import Icons from "../Icon";
import { FloatDiv, Item } from "./styled";
Expand All @@ -24,8 +25,8 @@ const RouterSwitch = ({ data }) => {

return (
<FloatDiv>
{data.map(({ link, Icon }, index) => (
<Item key={index} onClick={redirectTo(link)}>
{data.map(({ link, Icon }) => (
<Item key={uuid()} onClick={redirectTo(link)}>
<Icon $highlight={needsHighlight(link)} />
</Item>
))}
Expand Down
5 changes: 3 additions & 2 deletions components/Select/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { v4 as uuid } from "uuid";
import { Wrapper, Current, Menu, MenuItem, HighlightMenuItem } from "./styled";

const Select = ({ candidates, current, onChange }) => {
return (
<Wrapper>
<Current tabIndex={0}>{current}</Current>
<Menu tabIndex={1}>
{candidates.map((candidate,index) => {
{candidates.map((candidate) => {
const Component =
candidate === current ? HighlightMenuItem : MenuItem;
return (
<Component
key={index}
key={uuid()}
onClick={() => {
onChange(candidate);
}}
Expand Down
128 changes: 77 additions & 51 deletions packages/three/components/TransformControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,73 @@ export const TRANSFORM_CONTROLS_MODE = {
const TransformControls = (() => {
const object = new THREE.Mesh();

const initControls = ({ three, setTransformControls }) => {
const { scene, cameraControls } = three;

const control = new Controls(
cameraControls.getCamera(),
cameraControls.domElement
);
setTransformControls(control);
control.setSpace("local");
object.frustumCulled = false;
scene.add(object);

control.attach(object);
scene.add(control);

return () => {
scene.remove(object);
scene.remove(control);
control.dispose();
};
};

const bindChangedEvents = ({
three,
transformControls,
onChange,
onDraggingChanged,
}) => {
if (!transformControls) return;

const { cameraControls } = three;
const { onBeforeRender } = object;
const draggingChanged = (event) => {
const dragging = event.value;
onDraggingChanged(dragging);
cameraControls.setEnable(!dragging);
object.onBeforeRender = dragging
? () => {
onChange({
position: object.position.toArray(),
scale: object.scale.toArray(),
quaternion: object.quaternion.toArray(),
});
}
: onBeforeRender;
};
transformControls.addEventListener("dragging-changed", draggingChanged);

return () => {
transformControls.removeEventListener(
"dragging-changed",
draggingChanged
);
};
};

const changeControlsMode = ({ mode, transformControls }) => {
if (!transformControls) return;
transformControls.setMode(mode);
};

const setControlsTransform = ({ position, scale, quaternion }) => {
if (position) object.position.fromArray(position);
if (quaternion) object.quaternion.fromArray(quaternion);
if (scale) object.scale.fromArray(scale);
};

return ({
three,
position,
Expand All @@ -23,65 +90,24 @@ const TransformControls = (() => {
const [transformControls, setTransformControls] = useState(null);

useEffect(() => {
const { scene, cameraControls } = three;

const control = new Controls(
cameraControls.getCamera(),
cameraControls.domElement
);
setTransformControls(control);
control.setSpace("local");
object.frustumCulled = false;
scene.add(object);

control.attach(object);
scene.add(control);

return () => {
scene.remove(object);
scene.remove(control);
control.dispose();
};
return initControls({ three, setTransformControls });
}, [three]);

useEffect(() => {
if (!transformControls) return;

const { cameraControls } = three;
const { onBeforeRender } = object;
const draggingChanged = (event) => {
const dragging = event.value;
onDraggingChanged(dragging);
cameraControls.setEnable(!dragging);
object.onBeforeRender = dragging
? () => {
onChange({
position: object.position.toArray(),
scale: object.scale.toArray(),
quaternion: object.quaternion.toArray(),
});
}
: onBeforeRender;
};
transformControls.addEventListener("dragging-changed", draggingChanged);

return () => {
transformControls.removeEventListener(
"dragging-changed",
draggingChanged
);
};
return bindChangedEvents({
three,
transformControls,
onChange,
onDraggingChanged,
});
}, [three, transformControls, onChange, onDraggingChanged]);

useEffect(() => {
if (!transformControls) return;
transformControls.setMode(mode);
}, [three, mode, transformControls]);
changeControlsMode({ mode, transformControls });
}, [mode, transformControls]);

useEffect(() => {
if (position) object.position.fromArray(position);
if (quaternion) object.quaternion.fromArray(quaternion);
if (scale) object.scale.fromArray(scale);
setControlsTransform({ position, scale, quaternion });
}, [position, scale, quaternion]);

return null;
Expand Down
3 changes: 1 addition & 2 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Document from "next/document";
import { Html, Head, Main, NextScript } from "next/document";
import Document, { Html, Head, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";

export default class MyDocument extends Document {
Expand Down
5 changes: 4 additions & 1 deletion public/resources/media.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title>Pano-to-mesh</title>
</head>
<body>
<h1>Pano-to-mesh</h1>
</body>
Expand Down

0 comments on commit 1612903

Please sign in to comment.