buster/web/src/components/features/ShareMenu/ShareMenuContent.tsx

115 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-03-18 05:45:47 +08:00
import { type BusterShare, ShareAssetType, ShareRole } from '@/api/asset_interfaces';
2025-03-18 06:38:25 +08:00
import React, { useState } from 'react';
2025-03-18 05:45:47 +08:00
import { ShareMenuTopBar, ShareMenuTopBarOptions } from './ShareMenuTopBar';
2025-01-31 07:29:06 +08:00
import { ShareMenuContentBody } from './ShareMenuContentBody';
2025-03-18 05:45:47 +08:00
import { useMemoizedFn } from '@/hooks';
import { BusterRoutes, createBusterRoute } from '@/routes';
import { useBusterNotifications } from '@/context/BusterNotifications';
import { ShareMenuContentEmbedFooter } from './ShareMenuContentEmbed';
2025-03-20 04:47:15 +08:00
import { getIsEffectiveOwner } from '@/lib/share';
2025-01-31 07:29:06 +08:00
export const ShareMenuContent: React.FC<{
shareAssetConfig: BusterShare;
assetId: string;
2025-02-01 02:04:49 +08:00
assetType: ShareAssetType;
2025-03-18 05:45:47 +08:00
}> = React.memo(({ assetId, assetType, shareAssetConfig }) => {
const { openSuccessMessage } = useBusterNotifications();
const [selectedOptions, setSelectedOptions] = useState<ShareMenuTopBarOptions>(
ShareMenuTopBarOptions.Share
);
const permission = shareAssetConfig?.permission;
const publicly_accessible = shareAssetConfig?.publicly_accessible;
2025-03-20 04:47:15 +08:00
const isOwner = getIsEffectiveOwner(permission);
2025-03-18 05:45:47 +08:00
const onCopyLink = useMemoizedFn(() => {
let url = '';
if (assetType === ShareAssetType.METRIC && assetId) {
2025-04-09 00:29:15 +08:00
url = createBusterRoute({ route: BusterRoutes.APP_METRIC_ID_CHART, metricId: assetId });
2025-03-18 05:45:47 +08:00
} else if (assetType === ShareAssetType.DASHBOARD && assetId) {
url = createBusterRoute({
route: BusterRoutes.APP_DASHBOARD_ID,
dashboardId: assetId
});
} else if (assetType === ShareAssetType.COLLECTION && assetId) {
url = createBusterRoute({
route: BusterRoutes.APP_COLLECTIONS_ID,
collectionId: assetId
});
}
const urlWithDomain = window.location.origin + url;
navigator.clipboard.writeText(urlWithDomain);
openSuccessMessage('Link copied to clipboard');
});
return (
<div className="max-w-[340px] min-w-[340px]">
{isOwner && (
<ShareMenuContentHeader
assetType={assetType}
selectedOptions={selectedOptions}
setSelectedOptions={setSelectedOptions}
onCopyLink={onCopyLink}
isOwner={isOwner}
/>
)}
2025-03-20 07:07:18 +08:00
<ShareMenuContentBody
shareAssetConfig={shareAssetConfig}
assetType={assetType}
assetId={assetId}
selectedOptions={selectedOptions}
onCopyLink={onCopyLink}
isOwner={isOwner}
className="px-3 py-2.5"
/>
2025-03-18 05:45:47 +08:00
<ShareMenuContentFooter
selectedOptions={selectedOptions}
publicly_accessible={publicly_accessible!}
assetId={assetId}
assetType={assetType}
/>
</div>
);
});
ShareMenuContent.displayName = 'ShareMenuContent';
const ShareMenuContentHeader = React.memo<{
assetType: ShareAssetType;
selectedOptions: ShareMenuTopBarOptions;
setSelectedOptions: (options: ShareMenuTopBarOptions) => void;
onCopyLink: () => void;
isOwner: boolean;
}>(({ assetType, selectedOptions, setSelectedOptions, onCopyLink, isOwner }) => {
return (
<div className="border-b px-3 py-2">
<ShareMenuTopBar
assetType={assetType}
selectedOptions={selectedOptions}
onChangeSelectedOption={setSelectedOptions}
onCopyLink={onCopyLink}
isOwner={isOwner}
/>
</div>
);
});
ShareMenuContentHeader.displayName = 'ShareMenuContentHeader';
const ShareMenuContentFooter = React.memo<{
selectedOptions: ShareMenuTopBarOptions;
publicly_accessible: boolean;
assetId: string;
assetType: ShareAssetType;
}>(({ selectedOptions, publicly_accessible, assetId, assetType }) => {
if (selectedOptions === ShareMenuTopBarOptions.Embed && !publicly_accessible) {
return (
2025-03-20 07:07:18 +08:00
<div className="border-t">
2025-03-18 05:45:47 +08:00
<ShareMenuContentEmbedFooter assetId={assetId} assetType={assetType} />
</div>
2025-03-04 13:10:36 +08:00
);
}
2025-03-18 05:45:47 +08:00
return <></>;
});