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

feat: add share functionality #618

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/components/Editor/EditorSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import React from 'react';
import { EditorDropdown } from './EditorDropdown';

import { useFilesState } from '../../state';
import { ShareButton } from './ShareButton';

interface EditorSidebarProps {}

export const EditorSidebar: React.FunctionComponent<EditorSidebarProps> = () => {
const { source, from } = useFilesState(state => state.files['asyncapi']);
export const EditorSidebar: React.FunctionComponent<
EditorSidebarProps
> = () => {
const { source, from } = useFilesState((state) => state.files['asyncapi']);

let documentFromText = '';
if (from === 'storage') {
Expand All @@ -23,7 +26,10 @@ export const EditorSidebar: React.FunctionComponent<EditorSidebarProps> = () =>
className="flex flex-row items justify-between bg-gray-800 border-b border-gray-700 text-sm"
style={{ height: '30px', lineHeight: '30px' }}
>
<div className="ml-2 text-gray-500 text-xs italic" style={{ height: '30px', lineHeight: '30px' }}>
<div
className="ml-2 text-gray-500 text-xs italic"
style={{ height: '30px', lineHeight: '30px' }}
>
{documentFromText}
</div>
<div
Expand All @@ -32,6 +38,9 @@ export const EditorSidebar: React.FunctionComponent<EditorSidebarProps> = () =>
>
<div>
<ul className="flex">
<li>
<ShareButton />
</li>
<li>
<EditorDropdown />
</li>
Expand Down
36 changes: 36 additions & 0 deletions src/components/Editor/ShareButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { FaShareAlt } from 'react-icons/fa';
import { useServices } from '../../services';
import { toast } from 'react-hot-toast';
import { Tooltip } from '../common';

interface ShareButtonProps {}

export const ShareButton: React.FunctionComponent<ShareButtonProps> = () => {
const { editorSvc } = useServices();

const handleShare = () => {
toast.promise(
(async function () {
const base64 = await editorSvc.exportAsBase64();
const url = `${window.location.origin}/?base64=${encodeURIComponent(
base64
)}`;
await navigator.clipboard.writeText(url);
}()),
{
loading: 'Copying URL to clipboard...',
success: 'URL copied to clipboard!',
error: 'Failed to copy URL to clipboard.',
}
);
};

return (
<Tooltip content={'Share link'} placement="top" hideOnClick={true}>
<button className="bg-inherit" onClick={handleShare}>
<FaShareAlt className="text-gray-500 hover:text-white" />
</button>
</Tooltip>
);
};
10 changes: 10 additions & 0 deletions src/services/editor.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ export class EditorService extends AbstractService {
}
}

async exportAsBase64() {
try {
const file = filesState.getState().files['asyncapi'];
return this.svcs.formatSvc.encodeBase64(file.content);
} catch (err) {
console.error(err);
throw err;
}
}

async convertToYaml() {
try {
const yamlContent = this.svcs.formatSvc.convertToYaml(this.value);
Expand Down