Skip to content

Commit 2f86ee8

Browse files
committed
feat(admin): move workflow assistant to top-center and make it freely draggable
1 parent 9cb4820 commit 2f86ee8

1 file changed

Lines changed: 229 additions & 29 deletions

File tree

‎apps/admin/src/app/keystatic/workflow-assistant.tsx‎

Lines changed: 229 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useCallback, useEffect, useMemo, useState } from "react";
3+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
44

55
const REPO_OWNER = "PreferredAI";
66
const REPO_NAME = "PreferredAI.github.io";
@@ -154,6 +154,111 @@ export default function KeystaticWorkflowAssistant() {
154154
return false;
155155
});
156156

157+
const [customPosition, setCustomPosition] = useState<{
158+
x: number;
159+
y: number;
160+
} | null>(() => {
161+
if (typeof window !== "undefined") {
162+
try {
163+
const saved = localStorage.getItem("ks-assistant-pos");
164+
if (saved) {
165+
const parsed = JSON.parse(saved) as { x?: number; y?: number };
166+
if (typeof parsed.x === "number" && typeof parsed.y === "number") {
167+
return { x: parsed.x, y: parsed.y };
168+
}
169+
}
170+
} catch {
171+
// ignore localStorage errors
172+
}
173+
}
174+
return null;
175+
});
176+
177+
const isDraggingRef = useRef(false);
178+
const dragStartRef = useRef<{
179+
mouseX: number;
180+
mouseY: number;
181+
elemX: number;
182+
elemY: number;
183+
}>({ mouseX: 0, mouseY: 0, elemX: 0, elemY: 0 });
184+
const containerRef = useRef<HTMLDivElement | null>(null);
185+
186+
const onPointerDown = useCallback((e: React.PointerEvent) => {
187+
const target = e.target as HTMLElement;
188+
if (
189+
target.closest("button") ||
190+
target.closest("a") ||
191+
target.closest("input")
192+
) {
193+
return;
194+
}
195+
if (!containerRef.current) return;
196+
const rect = containerRef.current.getBoundingClientRect();
197+
isDraggingRef.current = true;
198+
dragStartRef.current = {
199+
mouseX: e.clientX,
200+
mouseY: e.clientY,
201+
elemX: rect.left,
202+
elemY: rect.top,
203+
};
204+
try {
205+
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
206+
} catch {
207+
// ignore
208+
}
209+
}, []);
210+
211+
const onPointerMove = useCallback((e: React.PointerEvent) => {
212+
if (!isDraggingRef.current || !containerRef.current) return;
213+
const dx = e.clientX - dragStartRef.current.mouseX;
214+
const dy = e.clientY - dragStartRef.current.mouseY;
215+
const width = containerRef.current.offsetWidth;
216+
const height = containerRef.current.offsetHeight;
217+
const newX = Math.max(
218+
8,
219+
Math.min(window.innerWidth - width - 8, dragStartRef.current.elemX + dx),
220+
);
221+
const newY = Math.max(
222+
8,
223+
Math.min(
224+
window.innerHeight - height - 8,
225+
dragStartRef.current.elemY + dy,
226+
),
227+
);
228+
setCustomPosition({ x: Math.round(newX), y: Math.round(newY) });
229+
}, []);
230+
231+
const onPointerUp = useCallback((e: React.PointerEvent) => {
232+
if (isDraggingRef.current) {
233+
isDraggingRef.current = false;
234+
try {
235+
(e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
236+
} catch {
237+
// ignore
238+
}
239+
if (containerRef.current) {
240+
const rect = containerRef.current.getBoundingClientRect();
241+
const pos = { x: Math.round(rect.left), y: Math.round(rect.top) };
242+
setCustomPosition(pos);
243+
try {
244+
localStorage.setItem("ks-assistant-pos", JSON.stringify(pos));
245+
} catch {
246+
// ignore
247+
}
248+
}
249+
}
250+
}, []);
251+
252+
const resetPosition = useCallback((e?: React.MouseEvent) => {
253+
e?.stopPropagation();
254+
setCustomPosition(null);
255+
try {
256+
localStorage.removeItem("ks-assistant-pos");
257+
} catch {
258+
// ignore
259+
}
260+
}, []);
261+
157262
const toggleCollapsed = useCallback((nextCollapsed: boolean) => {
158263
setCollapsed(nextCollapsed);
159264
if (typeof window !== "undefined") {
@@ -444,52 +549,114 @@ export default function KeystaticWorkflowAssistant() {
444549
return null;
445550
}
446551

552+
const isMain = currentBranch === DEFAULT_BRANCH;
553+
const peopleButtonLabel =
554+
route.collection === "people" && route.itemSlug
555+
? `+ Edit "${route.itemSlug}"`
556+
: "+ Edit People Info";
557+
const actionsHref = `https://github.com/${REPO_OWNER}/${REPO_NAME}/actions?query=${encodeURIComponent(`branch:${currentBranch}`)}`;
558+
559+
const rootStyle: React.CSSProperties = {
560+
position: "fixed",
561+
zIndex: 9999,
562+
fontFamily:
563+
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
564+
...(customPosition
565+
? { left: customPosition.x, top: customPosition.y }
566+
: { top: 8, left: "50%", transform: "translateX(-50%)" }),
567+
};
568+
447569
if (collapsed) {
448570
return (
449571
<div
572+
ref={containerRef}
573+
onPointerDown={onPointerDown}
574+
onPointerMove={onPointerMove}
575+
onPointerUp={onPointerUp}
450576
style={{
451-
position: "fixed",
452-
left: 16,
453-
bottom: 16,
454-
zIndex: 9999,
455-
fontFamily:
456-
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
577+
...rootStyle,
578+
userSelect: "none",
457579
}}
458580
>
459-
<button
460-
onClick={() => toggleCollapsed(false)}
581+
<div
461582
style={{
583+
display: "flex",
584+
alignItems: "center",
585+
gap: 7,
462586
background: "#18181b",
463587
color: "#fafafa",
464588
border: "1px solid #3f3f46",
465589
borderRadius: 999,
466-
padding: "7px 14px",
590+
padding: "4px 8px 4px 6px",
467591
fontSize: 12,
468-
fontWeight: 600,
469-
cursor: "pointer",
470-
boxShadow: "0 4px 14px rgba(0, 0, 0, 0.2)",
592+
boxShadow: "0 4px 14px rgba(0, 0, 0, 0.35)",
593+
cursor: "grab",
471594
}}
472595
>
473-
Quick Publish ▴
474-
</button>
596+
<span
597+
title="Drag anywhere (double-click to center)"
598+
onDoubleClick={resetPosition}
599+
style={{
600+
color: "#71717a",
601+
cursor: "grab",
602+
fontSize: 12,
603+
padding: "0 2px",
604+
}}
605+
>
606+
⠿
607+
</span>
608+
<span
609+
style={{
610+
display: "inline-block",
611+
width: 7,
612+
height: 7,
613+
borderRadius: "50%",
614+
background: isMain ? "#f59e0b" : "#10b981",
615+
}}
616+
/>
617+
<strong style={{ fontSize: 11, color: "#f4f4f5" }}>
618+
{isMain ? "main" : currentBranch}
619+
</strong>
620+
{status?.prNumber && (
621+
<span
622+
style={{
623+
background: "#2563eb",
624+
color: "#fff",
625+
fontSize: 10,
626+
fontWeight: 600,
627+
padding: "1px 5px",
628+
borderRadius: 4,
629+
}}
630+
>
631+
PR #{status.prNumber}
632+
</span>
633+
)}
634+
<button
635+
onClick={() => toggleCollapsed(false)}
636+
style={{
637+
background: "#27272a",
638+
color: "#e4e4e7",
639+
border: "1px solid #52525b",
640+
borderRadius: 999,
641+
padding: "2px 7px",
642+
fontSize: 11,
643+
fontWeight: 600,
644+
cursor: "pointer",
645+
}}
646+
>
647+
Quick Publish ▾
648+
</button>
649+
</div>
475650
</div>
476651
);
477652
}
478653

479-
const isMain = currentBranch === DEFAULT_BRANCH;
480-
const peopleButtonLabel =
481-
route.collection === "people" && route.itemSlug
482-
? `+ Edit "${route.itemSlug}"`
483-
: "+ Edit People Info";
484-
const actionsHref = `https://github.com/${REPO_OWNER}/${REPO_NAME}/actions?query=${encodeURIComponent(`branch:${currentBranch}`)}`;
485-
486654
return (
487655
<div
656+
ref={containerRef}
488657
style={{
489-
position: "fixed",
490-
left: 16,
491-
bottom: 16,
492-
zIndex: 9999,
658+
...rootStyle,
659+
width: "max-content",
493660
maxWidth: 440,
494661
background: "#18181b",
495662
color: "#f4f4f5",
@@ -498,21 +665,38 @@ export default function KeystaticWorkflowAssistant() {
498665
padding: "10px 12px",
499666
fontSize: 12,
500667
lineHeight: 1.4,
501-
fontFamily:
502-
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
503-
boxShadow: "0 8px 24px rgba(0, 0, 0, 0.28)",
668+
boxShadow: "0 8px 24px rgba(0, 0, 0, 0.35)",
504669
}}
505670
>
506671
<div
672+
onPointerDown={onPointerDown}
673+
onPointerMove={onPointerMove}
674+
onPointerUp={onPointerUp}
507675
style={{
508676
display: "flex",
509677
alignItems: "center",
510678
justifyContent: "space-between",
511679
gap: 8,
512680
marginBottom: 8,
681+
cursor: "grab",
682+
userSelect: "none",
683+
paddingBottom: 4,
684+
borderBottom: "1px solid #27272a",
513685
}}
514686
>
515687
<div style={{ display: "flex", alignItems: "center", gap: 6 }}>
688+
<span
689+
title="Drag anywhere (double-click to center)"
690+
onDoubleClick={resetPosition}
691+
style={{
692+
color: "#71717a",
693+
cursor: "grab",
694+
fontSize: 12,
695+
paddingRight: 2,
696+
}}
697+
>
698+
⠿
699+
</span>
516700
<span
517701
style={{
518702
display: "inline-block",
@@ -527,6 +711,22 @@ export default function KeystaticWorkflowAssistant() {
527711
</strong>
528712
</div>
529713
<div style={{ display: "flex", alignItems: "center", gap: 4 }}>
714+
{customPosition && (
715+
<button
716+
onClick={resetPosition}
717+
title="Reset position to top center"
718+
style={{
719+
background: "transparent",
720+
color: "#a1a1aa",
721+
border: "none",
722+
cursor: "pointer",
723+
padding: "2px 5px",
724+
fontSize: 11,
725+
}}
726+
>
727+
Reset ↺
728+
</button>
729+
)}
530730
{isKeystaticBranch && (
531731
<button
532732
onClick={() => {

0 commit comments

Comments
 (0)