Skip to content
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

add permalinks #3744

Merged
merged 12 commits into from
Mar 25, 2020
Prev Previous commit
Next Next commit
move copy to clipboard to effect
  • Loading branch information
SaraVieira committed Mar 24, 2020
commit 0b88ebcc744b3b4399bf4d5d1581c149151e8b2b
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,8 @@ import {
const getFullGitHubUrl = (url: string) =>
`${protocolAndHost()}${gitHubToSandboxUrl(url)}`;

const copyToClipboard = (str: string) => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};

export const Import = () => {
const { state, actions } = useOvermind();
const { state, actions, effects } = useOvermind();
const [error, setError] = useState(null);
const [transformedUrl, setTransformedUrl] = useState('');
const [url, setUrl] = useState('');
Expand Down Expand Up @@ -117,9 +105,7 @@ export const Import = () => {
<Button
small
style={{ fontSize: 11 }}
onClick={() => {
copyToClipboard(transformedUrl);
}}
onClick={() => effects.browser.copyToClipboard(transformedUrl)}
disabled={!transformedUrl}
>
Copy Link
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/app/overmind/effects/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ export default {
close: () => popup?.close(),
};
},
copyToClipboard: (str: string) => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
},
waitForMessage<T>(type): Promise<T> {
return new Promise(resolve => {
window.addEventListener('message', function onMessage(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,15 @@ import { Stack, Button } from '@codesandbox/components';
import CheckIcon from 'react-icons/lib/md/check';
import { useOvermind } from 'app/overmind';

const copyToClipboard = (str: string) => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};

export const ButtonActions = () => {
const [linkCopied, setLinkCopied] = React.useState(false);
const [mounted, setMounted] = React.useState(false);
const { actions } = useOvermind();
const { actions, effects } = useOvermind();
const timeout = React.useRef(null);
const copyLink = () => {
setLinkCopied(true);

copyToClipboard(document.location.href);
effects.browser.copyToClipboard(document.location.href);

if (timeout.current) {
clearTimeout(timeout.current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const Comment = React.memo<{
</Menu.Item>
<Menu.Item
onSelect={() => {
copyToClipboard(
effects.browser.copyToClipboard(
`${window.location.origin}${window.location.pathname}?comment=${comment.id}`
SaraVieira marked this conversation as resolved.
Show resolved Hide resolved
);
effects.notificationToast.success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@codesandbox/components';
import css from '@styled-system/css';
import { useOvermind } from 'app/overmind';
import { copyToClipboard } from 'app/utils/copy-to-clipboard';
import { OPTIMISTIC_COMMENT_ID } from 'app/overmind/namespaces/comments/state';
import { motion } from 'framer-motion';
import React, { useState } from 'react';
Expand Down Expand Up @@ -185,7 +184,7 @@ export const Dialog: React.FC<DialogProps> = ({ triggerRef, ...props }) => {
</Menu.Item>
<Menu.Item
onSelect={() => {
copyToClipboard(
effects.browser.copyToClipboard(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I have a suggestion here.

You can create an action in comments called like: copyPermalinkToClipboard, where you pass the commentId. Inside that action you do what you do here, run the two effects.

The great thing now is that both Dialog and Comment will just call this action and we avoid duplicate logic 😄

`${window.location.origin}${window.location.pathname}?comment=${comment.id}`
);
effects.notificationToast.success(
Expand Down
11 changes: 0 additions & 11 deletions packages/app/src/app/utils/copy-to-clipboard.ts

This file was deleted.