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

Fixed errors viewing public view when unauthenticated, duplicate only… #106

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion client/src/components/snippets/list/SnippetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface SnippetCardProps {
expandCategories: boolean;
showLineNumbers: boolean;
isPublicView?: boolean;
isAuthenticated: boolean;
}

export const SnippetCard: React.FC<SnippetCardProps> = ({
Expand All @@ -43,7 +44,8 @@ export const SnippetCard: React.FC<SnippetCardProps> = ({
showCategories,
expandCategories,
showLineNumbers,
isPublicView = false
isPublicView = false,
isAuthenticated
}) => {
const [currentFragmentIndex, setCurrentFragmentIndex] = useState(0);
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
Expand Down Expand Up @@ -166,6 +168,7 @@ export const SnippetCard: React.FC<SnippetCardProps> = ({
onOpenInNewTab={handleOpenInNewTab}
onDuplicate={handleDuplicate}
isPublicView={isPublicView}
isAuthenticated={isAuthenticated}
/>
</div>
</div>
Expand Down
26 changes: 15 additions & 11 deletions client/src/components/snippets/list/SnippetCardMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface SnippetCardMenuProps {
onOpenInNewTab: () => void;
onDuplicate: (e: React.MouseEvent) => void;
isPublicView: boolean;
isAuthenticated: boolean;
}

const SnippetCardMenu: React.FC<SnippetCardMenuProps> = ({
Expand All @@ -18,7 +19,8 @@ const SnippetCardMenu: React.FC<SnippetCardMenuProps> = ({
onShare,
onOpenInNewTab,
onDuplicate,
isPublicView
isPublicView,
isAuthenticated
}) => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
Expand All @@ -29,16 +31,18 @@ const SnippetCardMenu: React.FC<SnippetCardMenuProps> = ({
if (isPublicView) {
return (
<div className="top-4 right-4 flex items-center gap-1">
<IconButton
icon={<Copy size={16} />}
onClick={(e: React.MouseEvent) => {
e.stopPropagation();
onDuplicate(e);
}}
variant="custom"
size="sm"
className="bg-gray-700 hover:bg-gray-600"
/>
{isAuthenticated && (
<IconButton
icon={<Copy size={16} />}
onClick={(e: React.MouseEvent) => {
e.stopPropagation();
onDuplicate(e);
}}
variant="custom"
size="sm"
className="bg-gray-700 hover:bg-gray-600"
/>
)}
<IconButton
icon={<ExternalLink size={16} />}
onClick={(e: React.MouseEvent) => {
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/snippets/list/SnippetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface SnippetListProps {
expandCategories: boolean;
showLineNumbers: boolean;
isPublicView: boolean;
isAuthenticated: boolean;
}

const SnippetList: React.FC<SnippetListProps> = ({
Expand All @@ -35,7 +36,8 @@ const SnippetList: React.FC<SnippetListProps> = ({
showCategories,
expandCategories,
showLineNumbers,
isPublicView
isPublicView,
isAuthenticated
}) => {
if (snippets.length === 0) {
return (
Expand Down Expand Up @@ -68,6 +70,7 @@ const SnippetList: React.FC<SnippetListProps> = ({
expandCategories={expandCategories}
showLineNumbers={showLineNumbers}
isPublicView={isPublicView}
isAuthenticated={isAuthenticated}
/>
))}
</div>
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/snippets/view/SnippetStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ShareMenu } from '../share/ShareMenu';
import { UserDropdown } from '../../auth/UserDropdown';
import BaseSnippetStorage from './common/BaseSnippetStorage';
import { Snippet } from '../../../types/snippets';
import { useAuth } from '../../../hooks/useAuth';

const SnippetStorage: React.FC = () => {
const {
Expand All @@ -27,6 +28,7 @@ const SnippetStorage: React.FC = () => {
} = useSettings();

const { addToast } = useToast();
const { isAuthenticated } = useAuth();

const [isEditSnippetModalOpen, setIsEditSnippetModalOpen] = useState(false);
const [isSettingsModalOpen, setIsSettingsModalOpen] = useState(false);
Expand Down Expand Up @@ -112,6 +114,7 @@ const SnippetStorage: React.FC = () => {
onDuplicate={handleDuplicate}
headerRight={<UserDropdown />}
isPublicView={false}
isAuthenticated={isAuthenticated}
/>

<EditSnippetModal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface BaseSnippetStorageProps {
onDuplicate?: (snippet: Snippet) => void;
headerRight: React.ReactNode;
isPublicView: boolean;
isAuthenticated: boolean;
}

const BaseSnippetStorage: React.FC<BaseSnippetStorageProps> = ({
Expand All @@ -49,7 +50,8 @@ const BaseSnippetStorage: React.FC<BaseSnippetStorageProps> = ({
onShare,
onDuplicate,
headerRight,
isPublicView
isPublicView,
isAuthenticated
}) => {
const [searchTerm, setSearchTerm] = useState('');
const [selectedLanguage, setSelectedLanguage] = useState('');
Expand Down Expand Up @@ -199,6 +201,7 @@ const BaseSnippetStorage: React.FC<BaseSnippetStorageProps> = ({
expandCategories={expandCategories}
showLineNumbers={showLineNumbers}
isPublicView={isPublicView}
isAuthenticated={isAuthenticated}
/>

<SnippetModal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useSettings } from '../../../../hooks/useSettings';
import { useSnippets } from '../../../../hooks/useSnippets';
import { useToast } from '../../../../hooks/useToast';
import { useAuth } from '../../../../hooks/useAuth';
import { initializeMonaco } from '../../../../utils/language/languageUtils';
import SettingsModal from '../../../settings/SettingsModal';
import BaseSnippetStorage from '../common/BaseSnippetStorage';
import { fetchPublicSnippets } from '../../../../utils/api/snippets';
import { Snippet } from '../../../../types/snippets';
import { UserDropdown } from '../../../auth/UserDropdown';
import { ROUTES } from '../../../../constants/routes';

const PublicSnippetStorage: React.FC = () => {
const {
Expand All @@ -16,8 +18,9 @@ const PublicSnippetStorage: React.FC = () => {
showCategories, expandCategories, showLineNumbers
} = useSettings();

const { addSnippet } = useSnippets();
const { isAuthenticated } = useAuth();
const { addToast } = useToast();
const navigate = useNavigate();
const [snippets, setSnippets] = useState<Snippet[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [isSettingsModalOpen, setIsSettingsModalOpen] = useState(false);
Expand All @@ -33,12 +36,19 @@ const PublicSnippetStorage: React.FC = () => {
setSnippets(fetchedSnippets);
} catch (error) {
console.error('Failed to load public snippets:', error);
addToast('Failed to load public snippets', 'error');
} finally {
setIsLoading(false);
}
};

const handleDuplicate = async (snippet: Snippet) => {
if (!isAuthenticated) {
addToast('Please sign in to add this snippet to your collection', 'info');
navigate(ROUTES.LOGIN);
return;
}

try {
const duplicatedSnippet: Omit<Snippet, 'id' | 'updated_at' | 'share_count' | 'username'> = {
title: `${snippet.title}`,
Expand All @@ -48,10 +58,12 @@ const PublicSnippetStorage: React.FC = () => {
is_public: 0
};

await addSnippet(duplicatedSnippet);
const { createSnippet } = await import('../../../../utils/api/snippets');
await createSnippet(duplicatedSnippet);
addToast('Snippet added to your collection', 'success');
} catch (error) {
console.error('Failed to duplicate snippet:', error);
addToast('Failed to add snippet to your stash', 'error');
addToast('Failed to add snippet to your collection', 'error');
}
};

Expand All @@ -74,6 +86,7 @@ const PublicSnippetStorage: React.FC = () => {
onDuplicate={handleDuplicate}
headerRight={<UserDropdown />}
isPublicView={true}
isAuthenticated={isAuthenticated}
/>

<SettingsModal
Expand Down