-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add useInterval Hook, Refactor Live to use useInterval #1964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9552625
0374d00
0d0120d
897bd85
eeca4ee
a0b832a
b8e157d
0e685fd
a39e637
58eb615
c7ec652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { useInterval } from './useInterval'; | ||
| export { useScript } from './useScript'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Based on https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| const noop = () => undefined; | ||
| export const useInterval = (callback: () => void = noop, delay: number) => { | ||
| const savedCallback = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| savedCallback.current = callback; | ||
| }, [callback]); | ||
|
|
||
| useEffect(() => { | ||
| const id = | ||
| delay !== null ? setInterval(savedCallback.current, delay) : null; | ||
|
|
||
| return () => clearInterval(id); | ||
| }, [delay]); | ||
| }; |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import React from 'react'; | ||
| import { observer } from 'mobx-react-lite'; | ||
|
|
||
| import { useSignals, useStore } from 'app/store'; | ||
|
|
||
| import { | ||
| ErrorDescription, | ||
| WorkspaceInputContainer, | ||
| WorkspaceSubtitle, | ||
| } from '../../elements'; | ||
|
|
||
| import { More } from '../More'; | ||
|
|
||
| import { Description } from './elements'; | ||
| import { LiveButton } from './LiveButton'; | ||
| import { LiveInfo } from './LiveInfo'; | ||
|
|
||
| export const Live = observer(() => { | ||
| const { | ||
| live: { createLiveClicked }, | ||
| } = useSignals(); | ||
| const { | ||
| editor: { currentId, currentSandbox, isAllModulesSynced }, | ||
| isLoggedIn, | ||
| live: { isLive, isLoading }, | ||
| } = useStore(); | ||
|
|
||
| const showPlaceHolder = !(isLoggedIn && (isLive || currentSandbox.owned)); | ||
| if (showPlaceHolder) { | ||
| const message = isLoggedIn ? ( | ||
| <> | ||
| You need to own this sandbox to open a live session to collaborate with | ||
| others in real time.{' '} | ||
| <p>Fork this sandbox to live share it with others!</p> | ||
| </> | ||
| ) : ( | ||
| `You need to be signed in to open a live session to collaborate with others in real time. Sign in to live share this sandbox!` | ||
| ); | ||
|
|
||
| return <More message={message} id="live" />; | ||
| } | ||
|
|
||
| const hasUnsyncedModules = !isAllModulesSynced; | ||
| return ( | ||
| <div> | ||
| {isLive ? ( | ||
| <LiveInfo /> | ||
| ) : ( | ||
| <> | ||
| <Description> | ||
| {`Invite others to live edit this sandbox with you. We're doing it live!`} | ||
| </Description> | ||
|
|
||
| <WorkspaceSubtitle>Create live room</WorkspaceSubtitle> | ||
|
|
||
| <Description> | ||
| To invite others you need to generate a URL that others can join. | ||
| </Description> | ||
|
|
||
| {hasUnsyncedModules && ( | ||
| <ErrorDescription> | ||
| Save all your files before going live | ||
| </ErrorDescription> | ||
| )} | ||
|
|
||
| <WorkspaceInputContainer> | ||
| <LiveButton | ||
| disable={hasUnsyncedModules} | ||
| isLoading={isLoading} | ||
| onClick={() => { | ||
| createLiveClicked({ sandboxId: currentId }); | ||
| }} | ||
| /> | ||
| </WorkspaceInputContainer> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| }); | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import noop from 'lodash/noop'; | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { useInterval } from 'app/hooks'; | ||
|
|
||
| import { AnimatedRecordIcon, Button, LoadingDiv } from './elements'; | ||
|
|
||
| export const LiveButton = ({ | ||
| disable = false, | ||
| icon = true, | ||
| isLoading = false, | ||
| message = 'Go Live', | ||
| onClick = noop, | ||
| }) => { | ||
| const [hovering, setHovering] = useState(false); | ||
| const [showIcon, setShowIcon] = useState(icon); | ||
|
|
||
| useInterval( | ||
| () => { | ||
| if (hovering) { | ||
| setShowIcon(!showIcon); | ||
| } | ||
| }, | ||
| hovering ? 1000 : null | ||
| ); | ||
|
|
||
| if (!hovering && !showIcon) { | ||
| setShowIcon(true); | ||
| } | ||
|
|
||
| if (isLoading) { | ||
| return <LoadingDiv>Creating Session</LoadingDiv>; | ||
| } | ||
|
|
||
| return ( | ||
| <Button | ||
| disable={disable} | ||
| onMouseEnter={() => setHovering(true)} | ||
| onMouseLeave={() => setHovering(false)} | ||
| onClick={onClick} | ||
| > | ||
| {/* | ||
| // @ts-ignore */} | ||
SaraVieira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {icon && <AnimatedRecordIcon opacity={Number(showIcon)} />} {message} | ||
| </Button> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import styled, { css } from 'styled-components'; | ||
| import { HTMLAttributes } from 'react'; | ||
| import RecordIcon from 'react-icons/lib/md/fiber-manual-record'; | ||
|
|
||
| const styles = css` | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| outline: none; | ||
| padding: 0.5rem; | ||
| background-color: #fd2439b8; | ||
| width: 100%; | ||
| color: white; | ||
| border-radius: 4px; | ||
| font-weight: 800; | ||
| border: 2px solid #fd2439b8; | ||
| `; | ||
|
|
||
| interface ButtonProps extends HTMLAttributes<HTMLButtonElement> { | ||
| disable?: boolean; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this disable and not disabled?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's what we had on the button props previously. Feel free to change. |
||
| } | ||
| export const Button = styled.button<ButtonProps>` | ||
| ${({ disable }) => css` | ||
| transition: 0.3s ease all; | ||
| ${styles}; | ||
| cursor: pointer; | ||
|
|
||
| svg { | ||
| margin-right: 0.25rem; | ||
| } | ||
|
|
||
| ${disable | ||
| ? css` | ||
| pointer-events: none; | ||
| background-color: rgba(0, 0, 0, 0.3); | ||
| border-color: rgba(0, 0, 0, 0.2); | ||
| color: rgba(255, 255, 255, 0.7); | ||
| ` | ||
| : css` | ||
| &:hover { | ||
| background-color: #fd2439fa; | ||
| } | ||
| `}; | ||
| `} | ||
| `; | ||
|
|
||
| export const LoadingDiv = styled.div` | ||
| ${styles}; | ||
| `; | ||
|
|
||
| export const AnimatedRecordIcon = styled(RecordIcon)` | ||
| ${({ opacity = 1 }) => css` | ||
| opacity: ${opacity}; | ||
| transition: 0.3s ease opacity; | ||
| `} | ||
| `; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { LiveButton } from './LiveButton'; |
Uh oh!
There was an error while loading. Please reload this page.