-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add indicator for active gql requests (#1190)
- Loading branch information
Showing
10 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useHasActiveGQLTasks } from "@reearth/services/state"; | ||
import { keyframes, styled } from "@reearth/services/theme"; | ||
import { FC, useCallback, useEffect, useState } from "react"; | ||
|
||
const offsetX = 16; | ||
const offsetY = 16; | ||
|
||
const CursorStatus: FC = () => { | ||
const [mousePosition, setMousePosition] = useState({ x: -100, y: -100 }); | ||
const [inView, setInView] = useState(true); | ||
const [enabled] = useHasActiveGQLTasks(); | ||
|
||
const handleMouseMove = useCallback((event: MouseEvent) => { | ||
setMousePosition({ | ||
x: event.clientX, | ||
y: event.clientY | ||
}); | ||
}, []); | ||
|
||
const handleMouseEnter = useCallback(() => { | ||
setInView(true); | ||
}, []); | ||
|
||
const handleMouseLeave = useCallback(() => { | ||
setInView(false); | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.addEventListener("mousemove", handleMouseMove); | ||
document.addEventListener("mouseenter", handleMouseEnter); | ||
document.addEventListener("mouseleave", handleMouseLeave); | ||
return () => { | ||
document.removeEventListener("mousemove", handleMouseMove); | ||
document.removeEventListener("mouseenter", handleMouseEnter); | ||
document.removeEventListener("mouseleave", handleMouseLeave); | ||
}; | ||
}, [handleMouseMove, handleMouseEnter, handleMouseLeave]); | ||
|
||
return ( | ||
enabled && | ||
inView && ( | ||
<Wrapper left={mousePosition.x + offsetX} top={mousePosition.y + offsetY}> | ||
<Loader /> | ||
</Wrapper> | ||
) | ||
); | ||
}; | ||
|
||
export default CursorStatus; | ||
|
||
const Wrapper = styled("div")<{ left: number; top: number }>( | ||
({ left, top, theme }) => ({ | ||
position: "absolute", | ||
left: `${left}px`, | ||
top: `${top}px`, | ||
pointerEvents: "none", | ||
zIndex: theme.zIndexes.editor.loading | ||
}) | ||
); | ||
|
||
const loaderKeyframes = keyframes` | ||
100%{transform: rotate(1turn)} | ||
`; | ||
|
||
const loaderColor = "#ccc"; | ||
|
||
const Loader = styled("div")(() => ({ | ||
width: 24, | ||
aspectRatio: 1, | ||
borderRadius: "50%", | ||
background: `radial-gradient(farthest-side,${loaderColor} 100%,#0000) top/6px 6px no-repeat, conic-gradient(#0000 30%,${loaderColor})`, | ||
WebkitMask: "radial-gradient(farthest-side,#0000 calc(100% - 6px),#000 0)", | ||
animation: `${loaderKeyframes} 1s infinite linear` | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ApolloLink, Operation, NextLink, Observable } from "@apollo/client"; | ||
import { GQLTask } from "@reearth/services/state"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
export default ( | ||
addTask: (task: GQLTask) => void, | ||
removeTask: (task: GQLTask) => void | ||
): ApolloLink => | ||
new ApolloLink((operation: Operation, forward: NextLink) => { | ||
const taskId = uuidv4(); | ||
addTask({ id: taskId }); | ||
|
||
return new Observable((observer) => { | ||
const timeoutId = setTimeout(() => { | ||
observer.error(new Error("Operation timeout")); | ||
removeTask({ id: taskId }); | ||
}, 10000); | ||
|
||
const sub = forward(operation).subscribe({ | ||
next: (result) => { | ||
if (result.errors) { | ||
clearTimeout(timeoutId); | ||
removeTask({ id: taskId }); | ||
} | ||
observer.next(result); | ||
}, | ||
error: (error) => { | ||
clearTimeout(timeoutId); | ||
observer.error(error); | ||
removeTask({ id: taskId }); | ||
}, | ||
complete: () => { | ||
clearTimeout(timeoutId); | ||
observer.complete(); | ||
removeTask({ id: taskId }); | ||
} | ||
}); | ||
|
||
return () => { | ||
clearTimeout(timeoutId); | ||
sub.unsubscribe(); | ||
removeTask({ id: taskId }); | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters