Skip to content

Commit fc27cd0

Browse files
committed
refactor(ui): simplifies insertIdx, memoizes Set
Separates splice-shift correction from direction intent in insertIdx computation. Moves Set construction from click handler into createMemo so it derives once per prop change instead of per click.
1 parent 0918532 commit fc27cd0

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

src/app/components/shared/RepoLockControls.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ interface RepoLockControlsProps {
99
}
1010

1111
export default function RepoLockControls(props: RepoLockControlsProps) {
12+
const visibleSet = createMemo(() => new Set(props.visibleLockedRepos));
13+
1214
const lockInfo = createMemo(() => {
1315
const isLocked = viewState.lockedRepos.indexOf(props.repoFullName) !== -1;
1416
const visIdx = props.visibleLockedRepos.indexOf(props.repoFullName);
@@ -53,7 +55,7 @@ export default function RepoLockControls(props: RepoLockControlsProps) {
5355
<Tooltip content={lockInfo().isFirst ? "Already at top of pinned list" : "Move up"}>
5456
<button
5557
class="btn btn-ghost btn-xs"
56-
onClick={() => withFlipAnimation(() => moveLockedRepo(props.repoFullName, "up", new Set(props.visibleLockedRepos)))}
58+
onClick={() => withFlipAnimation(() => moveLockedRepo(props.repoFullName, "up", visibleSet()))}
5759
disabled={lockInfo().isFirst}
5860
aria-label={`Move ${props.repoFullName} up`}
5961
>
@@ -66,7 +68,7 @@ export default function RepoLockControls(props: RepoLockControlsProps) {
6668
<Tooltip content={lockInfo().isLast ? "Already at bottom of pinned list" : "Move down"}>
6769
<button
6870
class="btn btn-ghost btn-xs"
69-
onClick={() => withFlipAnimation(() => moveLockedRepo(props.repoFullName, "down", new Set(props.visibleLockedRepos)))}
71+
onClick={() => withFlipAnimation(() => moveLockedRepo(props.repoFullName, "down", visibleSet()))}
7072
disabled={lockInfo().isLast}
7173
aria-label={`Move ${props.repoFullName} down`}
7274
>

src/app/stores/view.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,9 @@ export function moveLockedRepo(
372372

373373
// Remove from old position and insert adjacent to target
374374
arr.splice(idx, 1);
375-
// After removal, targetIdx shifts if source was before it
376-
const insertIdx = direction === "up"
377-
? targetIdx - (idx < targetIdx ? 1 : 0)
378-
: targetIdx - (idx < targetIdx ? 1 : 0) + 1;
375+
// After removal, target index shifts down by 1 if source preceded it
376+
const adjustedTarget = targetIdx - (idx < targetIdx ? 1 : 0);
377+
const insertIdx = direction === "up" ? adjustedTarget : adjustedTarget + 1;
379378
arr.splice(insertIdx, 0, repoFullName);
380379
}));
381380
}

0 commit comments

Comments
 (0)