Skip to content

Commit fe832ce

Browse files
SaerisMichaelDeBoey
authored andcommitted
🔨 Add useInterval Hook, Refactor Live to use useInterval
Added a useInterval Hook (demo here: https://codesandbox.io/s/sessiontimer-useinterval-hook-demo-0l6jq) Refactored Live workspace item and it's dependents to use hooks.
1 parent 1c7df61 commit fe832ce

File tree

20 files changed

+802
-783
lines changed

20 files changed

+802
-783
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { useInterval } from './useInterval';
12
export { useScript } from './useScript';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Based on https://overreacted.io/making-setinterval-declarative-with-react-hooks/
2+
import { useEffect, useRef } from 'react';
3+
4+
export const useInterval = (callback = () => {}, delay: number) => {
5+
const savedCallback = useRef(null);
6+
7+
useEffect(() => {
8+
savedCallback.current = callback;
9+
}, [callback]);
10+
11+
useEffect(() => {
12+
function tick() {
13+
savedCallback.current();
14+
}
15+
16+
if (delay !== null) {
17+
const id = setInterval(tick, delay);
18+
19+
return () => clearInterval(id);
20+
}
21+
22+
return undefined;
23+
}, [delay]);
24+
};

‎packages/app/src/app/pages/Sandbox/Editor/Workspace/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Files from './items/Files';
1010
import ProjectInfo from './items/ProjectInfo';
1111
import GitHub from './items/GitHub';
1212
import Server from './items/Server';
13-
import Live from './items/Live';
13+
import { Live } from './items/Live';
1414
import More from './items/More';
1515
import Deployment from './items/Deployment';
1616
import ConfigurationFiles from './items/ConfigurationFiles';

‎packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Live/Countdown.js‎

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import { observer } from 'mobx-react-lite';
3+
import { useSignals, useStore } from 'app/store';
4+
import { LiveInfo } from './LiveInfo';
5+
import { LiveButton } from './LiveButton';
6+
import {
7+
Description,
8+
ErrorDescription,
9+
WorkspaceInputContainer,
10+
WorkspaceSubtitle,
11+
} from '../../elements';
12+
13+
export const Live = observer(() => {
14+
const signals = useSignals();
15+
const store = useStore();
16+
const hasUnsyncedModules = !store.editor.isAllModulesSynced;
17+
18+
return (
19+
<div>
20+
{store.live.isLive ? (
21+
<LiveInfo />
22+
) : (
23+
<>
24+
<Description style={{ marginBottom: '1rem' }}>
25+
Invite others to live edit this sandbox with you. We
26+
{"'"}
27+
re doing it live!
28+
</Description>
29+
30+
<>
31+
<WorkspaceSubtitle>Create live room</WorkspaceSubtitle>
32+
33+
<Description>
34+
To invite others you need to generate a URL that others can join.
35+
</Description>
36+
37+
{hasUnsyncedModules && (
38+
<ErrorDescription>
39+
Save all your files before going live
40+
</ErrorDescription>
41+
)}
42+
43+
<WorkspaceInputContainer>
44+
<LiveButton
45+
onClick={() => {
46+
signals.live.createLiveClicked({
47+
sandboxId: store.editor.currentId,
48+
});
49+
}}
50+
isLoading={store.live.isLoading}
51+
disable={hasUnsyncedModules}
52+
/>
53+
</WorkspaceInputContainer>
54+
</>
55+
</>
56+
)}
57+
</div>
58+
);
59+
});

‎packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Live/LiveButton.js‎

Lines changed: 0 additions & 120 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { useState } from 'react';
2+
3+
import { useInterval } from 'app/hooks';
4+
5+
import { AnimatedRecordIcon, Button, LoadingDiv } from './elements';
6+
7+
export const LiveButton = ({
8+
disable,
9+
icon = true,
10+
isLoading,
11+
message = 'Go Live',
12+
onClick,
13+
}) => {
14+
const [hovering, setHovering] = useState(false);
15+
const [showIcon, setShowIcon] = useState(icon);
16+
17+
useInterval(
18+
() => {
19+
if (hovering) {
20+
setShowIcon(!showIcon);
21+
}
22+
},
23+
hovering ? 1000 : null
24+
);
25+
26+
if (!hovering && !showIcon) {
27+
setShowIcon(true);
28+
}
29+
30+
const startHovering = () => setHovering(true);
31+
const stopHovering = () => setHovering(false);
32+
33+
if (isLoading) {
34+
return <LoadingDiv>Creating Session</LoadingDiv>;
35+
}
36+
37+
return (
38+
<Button
39+
disable={disable}
40+
onMouseEnter={startHovering}
41+
onMouseLeave={stopHovering}
42+
onClick={onClick}
43+
>
44+
{/*
45+
// @ts-ignore */}
46+
{icon && <AnimatedRecordIcon opacity={Number(showIcon)} />} {message}
47+
</Button>
48+
);
49+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import styled, { css } from 'styled-components';
2+
import RecordIcon from 'react-icons/lib/md/fiber-manual-record';
3+
4+
const styles = css`
5+
display: flex;
6+
align-items: center;
7+
justify-content: center;
8+
outline: none;
9+
padding: 0.5rem;
10+
background-color: #fd2439b8;
11+
width: 100%;
12+
color: white;
13+
border-radius: 4px;
14+
font-weight: 800;
15+
border: 2px solid #fd2439b8;
16+
`;
17+
18+
interface ButtonProps extends HTMLButtonElement {
19+
disable?: boolean;
20+
}
21+
export const Button = styled.button<ButtonProps>`
22+
${({ disable }) => css`
23+
transition: 0.3s ease all;
24+
${styles};
25+
cursor: pointer;
26+
27+
svg {
28+
margin-right: 0.25rem;
29+
}
30+
31+
${disable
32+
? css`
33+
pointer-events: none;
34+
background-color: rgba(0, 0, 0, 0.3);
35+
border-color: rgba(0, 0, 0, 0.2);
36+
color: rgba(255, 255, 255, 0.7);
37+
`
38+
: css`
39+
&:hover {
40+
background-color: #fd2439fa;
41+
}
42+
`};
43+
`}
44+
`;
45+
46+
export const LoadingDiv = styled.div`
47+
${styles};
48+
`;
49+
50+
export const AnimatedRecordIcon = styled(RecordIcon)`
51+
${({ opacity = 1 }) => css`
52+
opacity: ${opacity};
53+
transition: 0.3s ease opacity;
54+
`}
55+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { LiveButton } from './LiveButton';

0 commit comments

Comments
 (0)