Skip to content

Commit

Permalink
fix: delete shared link cache update
Browse files Browse the repository at this point in the history
  • Loading branch information
berry-13 committed Jan 12, 2025
1 parent fee1a83 commit 52f4ded
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 993 deletions.
14 changes: 11 additions & 3 deletions api/models/Share.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,23 @@ async function updateSharedLink(user, shareId) {
}
}

async function deleteSharedLink(user, { shareId }) {
async function deleteSharedLink(user, shareId) {
if (!user || !shareId) {
throw new ShareServiceError('Missing required parameters', 'INVALID_PARAMS');
}

try {
const result = await SharedLink.findOneAndDelete({ shareId, user }).exec();
const result = await SharedLink.findOneAndDelete({ shareId, user }).lean().exec();

return result ? { message: 'Share deleted successfully' } : { message: 'Share not found' };
if (!result) {
return null;
}

return {
success: true,
shareId,
message: 'Share deleted successfully',
};
} catch (error) {
logger.error('[deleteSharedLink] Error deleting shared link', {
error: error.message,
Expand Down
13 changes: 7 additions & 6 deletions api/server/routes/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ router.patch('/:shareId', requireJwtAuth, async (req, res) => {

router.delete('/:shareId', requireJwtAuth, async (req, res) => {
try {
const deleted = await deleteSharedLink(req.user.id, req.params.shareId);
if (deleted) {
res.status(200).json(deleted);
} else {
res.status(404).end();
const result = await deleteSharedLink(req.user.id, req.params.shareId);

if (!result) {
return res.status(404).json({ message: 'Share not found' });
}

return res.status(200).json(result);
} catch (error) {
res.status(500).json({ message: 'Error deleting shared link' });
return res.status(400).json({ message: error.message });
}
});

Expand Down
57 changes: 39 additions & 18 deletions client/src/components/Nav/SettingsTabs/Data/SharedLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState, useMemo } from 'react';
import { Link } from 'react-router-dom';
import { TrashIcon } from 'lucide-react';
import type { SharedLinkItem, SharedLinksListParams } from 'librechat-data-provider';
import { OGDialog, OGDialogTrigger, Checkbox, Button, TooltipAnchor, Label } from '~/components/ui';
import { OGDialog, OGDialogTrigger, Button, TooltipAnchor, Label } from '~/components/ui';
import { useDeleteSharedLinkMutation, useSharedLinksQuery } from '~/data-provider';
import OGDialogTemplate from '~/components/ui/OGDialogTemplate';
import { useLocalize, useMediaQuery } from '~/hooks';
Expand All @@ -11,11 +11,7 @@ import { NotificationSeverity } from '~/common';
import { useToastContext } from '~/Providers';
import { formatDate } from '~/utils';

interface TableRow extends SharedLinkItem {
id?: string;
}

const PAGE_SIZE = 250;
const PAGE_SIZE = 25;

const DEFAULT_PARAMS: SharedLinksListParams = {
pageSize: PAGE_SIZE,
Expand All @@ -35,20 +31,29 @@ export default function SharedLinks() {
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, refetch, isLoading } =
useSharedLinksQuery(DEFAULT_PARAMS, {
enabled: isOpen,
staleTime: 0,
cacheTime: 5 * 60 * 1000,
refetchOnWindowFocus: false,
refetchOnMount: false,
});

const allLinks = useMemo(() => {
if (!data?.pages) {
return [];
}
return data.pages.flatMap((page) => page.links);

return data.pages.flatMap((page) => page.links.filter(Boolean));
}, [data?.pages]);

const deleteMutation = useDeleteSharedLinkMutation({
onSuccess: () => {
onSuccess: async () => {
refetch();

setIsDeleteOpen(false);
setDeleteRow(null);
},
onError: () => {
onError: (error) => {
console.error('Delete error:', error);
showToast({
message: localize('com_ui_share_delete_error'),
severity: NotificationSeverity.ERROR,
Expand All @@ -57,15 +62,30 @@ export default function SharedLinks() {
});

const handleDelete = useCallback(
async (selectedRows: TableRow[]) => {
async (selectedRows: SharedLinkItem[]) => {
const validRows = selectedRows.filter(
(row) => typeof row.id === 'string' && row.id.length > 0,
(row) => typeof row.shareId === 'string' && row.shareId.length > 0,
);

if (validRows.length === 0) {
showToast({
message: localize('com_ui_no_valid_items'),
severity: NotificationSeverity.WARNING,
});
return;
}

try {
await Promise.all(
validRows.map((row) => deleteMutation.mutateAsync({ shareId: row.id as string })),
);
for (const row of validRows) {
await deleteMutation.mutateAsync({ shareId: row.shareId });
}

showToast({
message: localize(
validRows.length === 1 ? 'com_ui_delete_success' : 'com_ui_bulk_delete_success',
),
severity: NotificationSeverity.SUCCESS,
});
} catch (error) {
console.error('Failed to delete shared links:', error);
showToast({
Expand All @@ -84,7 +104,7 @@ export default function SharedLinks() {
await fetchNextPage();
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);

const [deleteRow, setDeleteRow] = useState<TableRow | null>(null);
const [deleteRow, setDeleteRow] = useState<SharedLinkItem | null>(null);

const confirmDelete = useCallback(() => {
if (deleteRow) {
Expand All @@ -98,7 +118,7 @@ export default function SharedLinks() {
{
accessorKey: 'title',
header: 'Name',
cell: ({ row }) => (
cell: ({ row }) => (
<Link
to={`/share/${row.original.shareId}`}
target="_blank"
Expand Down Expand Up @@ -129,7 +149,7 @@ export default function SharedLinks() {
size: '15%',
mobileSize: '15%',
},
cell: ({ row }) => (
cell: ({ row }) => (
<TooltipAnchor
description={localize('com_ui_delete')}
render={
Expand All @@ -149,7 +169,7 @@ export default function SharedLinks() {
),
},
],
[isSmallScreen, handleDelete, localize],
[isSmallScreen, localize],
);

return (
Expand All @@ -175,6 +195,7 @@ export default function SharedLinks() {
hasNextPage={hasNextPage}
isFetchingNextPage={isFetchingNextPage}
fetchNextPage={handleFetchNextPage}
showCheckboxes={false}
/>
}
/>
Expand Down
Loading

0 comments on commit 52f4ded

Please sign in to comment.