Skip to content

Commit

Permalink
Merge pull request #8778 from hicommonwealth/burton/download-markdown…
Browse files Browse the repository at this point in the history
…-for-kyve

Ability to download markdown for Kyve
  • Loading branch information
jnaviask authored Aug 7, 2024
2 parents 01a01de + 32af541 commit 29d3c73
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
23 changes: 23 additions & 0 deletions packages/commonwealth/client/scripts/utils/downloadDataAsFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Save the content locally and use the `filename` as the suggested name for the
* file.
*/
export function downloadDataAsFile(
content: string,
type: string,
filename: string,
) {
const blob = new Blob([content], { type });

const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;

document.body.appendChild(link);

link.click();

document.body.removeChild(link);

URL.revokeObjectURL(link.href);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Compass,
Copy,
DotsThreeVertical,
Download,
Export,
Eye,
Flag,
Expand Down Expand Up @@ -240,6 +241,7 @@ export const iconLookup = {
website: Icons.CWWebsite,
write: Icons.CWWrite,
members: Icons.CWMembers,
download: withPhosphorIcon(Download),
};

export const customIconLookup = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import './AdminActions.scss';

export type AdminActionsProps = {
thread: Thread;
canUpdateThread?: boolean;
onDelete?: () => any;
onSpamToggle?: (thread: Thread) => any;
onLockToggle?: (isLocked: boolean) => any;
Expand All @@ -36,6 +37,7 @@ export type AdminActionsProps = {
onEditStart?: () => any;
onEditConfirm?: () => any;
onEditCancel?: () => any;
onDownloadMarkdown?: () => void;
hasPendingEdits?: boolean;
editingDisabled?: boolean;
};
Expand All @@ -54,6 +56,8 @@ export const AdminActions = ({
onEditConfirm,
hasPendingEdits,
editingDisabled,
onDownloadMarkdown,
canUpdateThread,
}: AdminActionsProps) => {
const navigate = useCommonNavigate();
const [isEditCollaboratorsModalOpen, setIsEditCollaboratorsModalOpen] =
Expand Down Expand Up @@ -296,7 +300,8 @@ export const AdminActions = ({
<PopoverMenu
className="AdminActions compact"
menuItems={[
...(thread.archivedAt === null &&
...(canUpdateThread &&
thread.archivedAt === null &&
(hasAdminPermissions ||
isThreadAuthor ||
(isThreadCollaborator && !thread.readOnly))
Expand All @@ -319,7 +324,7 @@ export const AdminActions = ({
},
]
: []),
...(hasAdminPermissions
...(canUpdateThread && hasAdminPermissions
? [
...(thread.archivedAt === null
? [
Expand Down Expand Up @@ -357,7 +362,15 @@ export const AdminActions = ({
},
]
: []),
...(isThreadAuthor || hasAdminPermissions
...[
{
onClick: () => onDownloadMarkdown?.(),
label: 'Download as Markdown',
iconLeft: 'download' as const,
iconLeftWeight: 'bold' as const,
},
],
...(canUpdateThread && (isThreadAuthor || hasAdminPermissions)
? [
...(app.chain?.meta.snapshot.length
? [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
import { useCreateThreadSubscriptionMutation } from 'state/api/trpc/subscription/useCreateThreadSubscriptionMutation';
import { useDeleteThreadSubscriptionMutation } from 'state/api/trpc/subscription/useDeleteThreadSubscriptionMutation';
import Permissions from 'utils/Permissions';
import { downloadDataAsFile } from 'utils/downloadDataAsFile';
import { SharePopover } from 'views/components/SharePopover';
import { ViewUpvotesDrawerTrigger } from 'views/components/UpvoteDrawer';
import { CWThreadAction } from 'views/components/component_kit/new_designs/cw_thread_action';
Expand Down Expand Up @@ -93,6 +94,10 @@ export const ThreadOptions = ({
);
}, [isSubscribed, thread]);

const handleDownloadMarkdown = () => {
downloadDataAsFile(thread.plaintext, 'text/markdown', thread.title + '.md');
};

const createThreadSubscriptionMutation =
useCreateThreadSubscriptionMutation();
const deleteThreadSubscriptionMutation =
Expand Down Expand Up @@ -226,8 +231,9 @@ export const ThreadOptions = ({
/>
)}

{canUpdateThread && thread && (
{thread && (
<AdminActions
canUpdateThread={canUpdateThread}
thread={thread}
onLockToggle={onLockToggle}
onCollaboratorsEdit={onCollaboratorsEdit}
Expand All @@ -239,6 +245,7 @@ export const ThreadOptions = ({
onProposalStageChange={onProposalStageChange}
onSnapshotProposalFromThread={onSnapshotProposalFromThread}
onSpamToggle={onSpamToggle}
onDownloadMarkdown={handleDownloadMarkdown}
hasPendingEdits={hasPendingEdits}
editingDisabled={editingDisabled}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'components/proposals/json_display.scss';
import { CoinObject } from 'controllers/chain/cosmos/types';
import React from 'react';
import app from 'state';
import { downloadDataAsFile } from 'utils/downloadDataAsFile';
import { CWDivider } from '../../components/component_kit/cw_divider';
import { CWText } from '../../components/component_kit/cw_text';
import { CWButton } from '../../components/component_kit/new_designs/CWButton';
Expand All @@ -26,20 +27,7 @@ export const JSONDisplay = ({ data, title }: JSONDisplayProps) => {
const handleExport = () => {
const dataTitle = data.title || 'Proposal';
const proposalDetails = data.details || '';

const blob = new Blob([proposalDetails], { type: 'text/markdown' });

const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `${dataTitle}.md`;

document.body.appendChild(link);

link.click();

document.body.removeChild(link);

URL.revokeObjectURL(link.href);
downloadDataAsFile(proposalDetails, 'text/markdown', `${dataTitle}.md`);
};

return (
Expand Down

0 comments on commit 29d3c73

Please sign in to comment.