Skip to content

Commit ffb8be0

Browse files
committed
client(MultistepProvider.tsx): optimize insertCard function
1 parent 7c47080 commit ffb8be0

File tree

6 files changed

+24
-208
lines changed

6 files changed

+24
-208
lines changed

client/src/components/form/PageFour.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const PageFour = () => {
4747
</Flex>
4848

4949
<Flex>
50-
<InputWrapper label="Show border">
50+
<InputWrapper label="Border">
5151
<TrueFalseInput
5252
value={card.showBorder}
5353
trueLabel="Show"
@@ -67,7 +67,7 @@ const PageFour = () => {
6767
/>
6868
</InputWrapper>
6969

70-
<InputWrapper label="Show title">
70+
<InputWrapper label="Title">
7171
<TrueFalseInput
7272
value={!card.hideTitle}
7373
trueLabel="Show"

client/src/components/lines/BadgeItem.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ const BadgeItem = ({ badge, removeBadge, lineNumber }: Props) => {
4444
}}
4545
className={cn(
4646
"flex cursor-grab select-none items-center gap-3 border border-gh-bg px-3 py-[.45rem] transition-all duration-150",
47-
dragged
48-
? "border-gh-bg-secondary bg-gh-bg-dark text-gh-bg-dark"
49-
: "bg-gh-bg-secondary text-white",
47+
dragged ? "hidden" : "bg-gh-bg-secondary text-white",
5048
grabbedBadge === undefined ? "hover:bg-gh-gray" : ""
5149
)}
5250
onClick={(e) => {

client/src/components/lines/BadgePlaceholder.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ const BadgePlaceholder = ({ lineNumber, position }: Props) => {
3939
e.preventDefault();
4040

4141
insertBadge(lineNumber, position, grabbedBadge);
42-
setHovered(false);
4342
setGrabbedBadge(undefined);
43+
setHovered(false);
4444
},
4545
[grabbedBadge, insertBadge, lineNumber, position]
4646
);
@@ -52,8 +52,11 @@ const BadgePlaceholder = ({ lineNumber, position }: Props) => {
5252
onDragLeave={() => setHovered(false)}
5353
onDragEnter={() => (shouldDropBeAllowed() ? setHovered(true) : {})}
5454
className={cn(
55-
"h-[31.73px] border-r-[.5rem] border-gh-bg",
56-
hovered ? "border-l-[.5rem] bg-gh-bg-dark" : "bg-transparent"
55+
"h-[31.73px] border-gh-bg",
56+
hovered ? "border-l-[.5rem] bg-gh-bg-dark" : "bg-transparent",
57+
grabbedBadge && grabbedBadge.badge.position + 1 === position
58+
? ""
59+
: "border-r-[.5rem]"
5760
)}
5861
style={{
5962
width:

client/src/components/ui/RepositoryLink.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const RepositoryLink = ({ user, repository, isPublic }: Props) => {
1818
description: "This is a description",
1919
});
2020

21-
useEffect(() => {
22-
axios
23-
.get<GithubResponse>(`https://api.github.com/repos/${user}/${repository}`)
24-
.then((res) => setGithubStats(res.data));
25-
}, []);
21+
// useEffect(() => {
22+
// axios
23+
// .get<GithubResponse>(`https://api.github.com/repos/${user}/${repository}`)
24+
// .then((res) => setGithubStats(res.data));
25+
// }, []);
2626

2727
return (
2828
<div>

client/src/context/MultistepContext.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,23 @@ export const MultistepProvider: FC<MultistepProviderProps> = ({ children }) => {
132132
if (position > bdt.badge.position) position--;
133133

134134
setCard((prev) => {
135-
const newCard = structuredClone(prev);
136-
const lineIdx = newCard.lines.findIndex(
137-
(x) => x.lineNumber === lineNumber
138-
);
135+
// check whether the line exists
136+
const line = prev.lines.findIndex((x) => x.lineNumber === lineNumber);
137+
if (line === -1) return prev;
139138

140-
// line with the specified lineNumber doesn't exist
141-
if (lineIdx === -1) return prev;
139+
const newCard = structuredClone(prev);
140+
const badges = newCard.lines[line].badges;
141+
const badgePositions = badges.map((badge) => badge.position);
142142

143143
// remove the old badge
144-
newCard.lines[lineIdx].badges = newCard.lines[lineIdx].badges
145-
.sort((a, z) => a.position - z.position)
146-
.filter((x) => x.position !== bdt.badge.position);
144+
const idx = badgePositions.indexOf(bdt.badge.position);
145+
if (idx !== -1) badges.splice(idx, 1);
147146

148147
// insert the new badge
149-
newCard.lines[lineIdx].badges.splice(position, 0, bdt.badge);
148+
badges.splice(position, 0, bdt.badge);
150149

151150
// rearrange the positions
152-
newCard.lines[lineIdx].badges = newCard.lines[lineIdx].badges.map(
153-
(prev, i) => ({
154-
...prev,
155-
position: i,
156-
})
157-
);
151+
badges.forEach((badge, i) => (badge.position = i));
158152

159153
return newCard;
160154
});

package-lock.json

Lines changed: 0 additions & 179 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)