Skip to content
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
16 changes: 15 additions & 1 deletion components/admin/admin-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import { DownloadIcon } from '@/components/ui/animated-icons/download';
import { FilePenLineIcon } from '@/components/ui/animated-icons/file-pen-line';
import { UploadIcon } from '@/components/ui/animated-icons/upload';
import { importConfig } from '@/lib/actions';
import { DEFAULT_APP_TITLE, type Category, type Service, type Preferences, type IconConfig } from '@/lib/types';
import { createServiceDuplicateDraft } from '@/lib/service-duplicate';
import { DEFAULT_APP_TITLE, type Category, type Service, type Preferences, type IconConfig, type ServiceFormData } from '@/lib/types';
import { AppSettingsCard } from '@/components/admin/app-settings/app-settings-card';
import { ConfigEditorDialog } from '@/components/admin/config-editor/config-editor-dialog';
import { PageFooter } from '@/components/layout/footer/page-footer';
Expand Down Expand Up @@ -72,6 +73,7 @@ export function AdminClient({ appTitle, appLogo, categories: initialCategories,
const [serviceModalOpen, setServiceModalOpen] = useState(false);
const [editingCategory, setEditingCategory] = useState<Category | undefined>();
const [editingService, setEditingService] = useState<Service | undefined>();
const [serviceDraft, setServiceDraft] = useState<ServiceFormData | undefined>();
const [searchQuery, setSearchQuery] = useState('');
const [refreshKey, setRefreshKey] = useState(0);
const searchInputRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -159,11 +161,19 @@ export function AdminClient({ appTitle, appLogo, categories: initialCategories,

const handleAddService = () => {
setEditingService(undefined);
setServiceDraft(undefined);
setServiceModalOpen(true);
};

const handleEditService = (service: Service) => {
setEditingService(service);
setServiceDraft(undefined);
setServiceModalOpen(true);
};

const handleDuplicateService = (service: Service) => {
setEditingService(undefined);
setServiceDraft(createServiceDuplicateDraft(service, services));
setServiceModalOpen(true);
};

Expand All @@ -178,6 +188,7 @@ export function AdminClient({ appTitle, appLogo, categories: initialCategories,
setServiceModalOpen(open);
if (!open) {
setEditingService(undefined);
setServiceDraft(undefined);
}
};

Expand Down Expand Up @@ -422,6 +433,7 @@ export function AdminClient({ appTitle, appLogo, categories: initialCategories,
services={filteredData.services}
categories={categories}
onEdit={handleEditService}
onDuplicate={handleDuplicateService}
onDeleted={handleRefresh}
cacheKey={refreshKey}
/>
Expand All @@ -442,6 +454,8 @@ export function AdminClient({ appTitle, appLogo, categories: initialCategories,
open={serviceModalOpen}
onOpenChange={handleServiceModalClose}
service={editingService}
initialValues={serviceDraft}
mode={editingService ? 'edit' : serviceDraft ? 'duplicate' : 'create'}
categories={categories}
onSuccess={handleRefresh}
cacheKey={refreshKey}
Expand Down
21 changes: 19 additions & 2 deletions components/admin/services/service-form-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
DialogTitle,
} from '@/components/ui/dialog';
import { ServiceForm } from './service-form';
import type { Category, Service } from '@/lib/types';
import type { Category, Service, ServiceFormData } from '@/lib/types';

interface ServiceFormModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
service?: Service;
initialValues?: ServiceFormData;
mode?: 'create' | 'duplicate' | 'edit';
categories: Category[];
onSuccess: () => void;
cacheKey?: number;
Expand All @@ -22,6 +24,8 @@ export function ServiceFormModal({
open,
onOpenChange,
service,
initialValues,
mode,
categories,
onSuccess,
cacheKey,
Expand All @@ -30,15 +34,28 @@ export function ServiceFormModal({
onSuccess();
onOpenChange(false);
};
const resolvedMode = mode ?? (service ? 'edit' : 'create');
const formKey = service
? `edit:${service.id}`
: initialValues
? `draft:${initialValues.name}:${initialValues.url}:${initialValues.categoryId}`
: 'create';
const title = resolvedMode === 'edit'
? 'Edit Service'
: resolvedMode === 'duplicate'
? 'Duplicate Service'
: 'Add Service';

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[500px]" aria-describedby={undefined}>
<DialogHeader>
<DialogTitle>{service ? 'Edit Service' : 'Add Service'}</DialogTitle>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>
<ServiceForm
key={formKey}
service={service}
initialValues={initialValues}
categories={categories}
onSuccess={handleSuccess}
onCancel={() => onOpenChange(false)}
Expand Down
27 changes: 16 additions & 11 deletions components/admin/services/service-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
type ServiceMetadataApplyMode,
} from '@/lib/service-metadata-apply';
import { cn, slugify } from '@/lib/utils';
import { ICON_TYPES, type Category, type IconConfig, type Service } from '@/lib/types';
import { ICON_TYPES, type Category, type IconConfig, type Service, type ServiceFormData } from '@/lib/types';

interface ServiceFormProps {
service?: Service;
initialValues?: ServiceFormData;
categories: Category[];
onSuccess?: () => void;
onCancel?: () => void;
Expand Down Expand Up @@ -73,25 +74,29 @@ function getIconFetchId(serviceId: string, serviceUrl: string): string {
}
}

export function ServiceForm({ service, categories, onSuccess, onCancel, cacheKey }: ServiceFormProps) {
const [name, setName] = useState(service?.name || '');
const [description, setDescription] = useState(service?.description || '');
const [url, setUrl] = useState(service?.url || '');
const [categoryId, setCategoryId] = useState(service?.categoryId || '');
const [icon, setIcon] = useState<IconConfig | undefined>(service?.icon);
export function ServiceForm({ service, initialValues, categories, onSuccess, onCancel, cacheKey }: ServiceFormProps) {
const formValues = service ?? initialValues;
const hasSeededValues = !!formValues;
const [name, setName] = useState(formValues?.name || '');
const [description, setDescription] = useState(formValues?.description || '');
const [url, setUrl] = useState(formValues?.url || '');
const [categoryId, setCategoryId] = useState(formValues?.categoryId || '');
const [icon, setIcon] = useState<IconConfig | undefined>(formValues?.icon);
const [pendingIconFile, setPendingIconFile] = useState<File | null>(null);
const [active, setActive] = useState(service?.active ?? true);
const [active, setActive] = useState(formValues?.active ?? true);
const [errors, setErrors] = useState<Record<string, string>>({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [isFetchingIcon, setIsFetchingIcon] = useState(false);
const [isFetchingMetadata, setIsFetchingMetadata] = useState(false);
const [iconVersion, setIconVersion] = useState(0);
const [iconControl, setIconControl] = useState<'auto' | 'manual'>(service ? 'manual' : 'auto');
const [iconControl, setIconControl] = useState<'auto' | 'manual'>(
hasSeededValues ? 'manual' : 'auto'
);
Comment thread
austin-smith marked this conversation as resolved.
const urlRef = useRef(url);
const nameRef = useRef(name);
const descriptionRef = useRef(description);
const hasUserEditedNameRef = useRef(!!service);
const hasUserEditedDescriptionRef = useRef(!!service);
const hasUserEditedNameRef = useRef(hasSeededValues);
const hasUserEditedDescriptionRef = useRef(hasSeededValues);
const lastAutoFetchKeyRef = useRef<string | null>(null);
const lastAutoMetadataFetchKeyRef = useRef<string | null>(null);
const autoFetchRequestRef = useRef(0);
Expand Down
6 changes: 4 additions & 2 deletions components/admin/services/service-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Button } from '@/components/ui/button';
import { CornerRibbon } from '@/components/ui/corner-ribbon';
import { CategoryIcon } from '@/components/common/icons/category-icon';
import { SortableList, SortableItem } from '@/components/ui/sortable';
import { Pencil, Trash2, ExternalLink } from 'lucide-react';
import { ExternalLink, Pencil, Trash2 } from 'lucide-react';
import { ServiceIcon } from '@/components/common/icons/service-icon';
import { DeleteConfirmDialog } from '../delete-confirm-dialog';
import { deleteService, reorderServices } from '@/lib/actions';
Expand All @@ -20,11 +20,12 @@ interface ServiceListProps {
services: Service[];
categories: Category[];
onEdit: (service: Service) => void;
onDuplicate: (service: Service) => void;
onDeleted: () => void;
cacheKey?: number;
}

export function ServiceList({ services, categories, onEdit, onDeleted, cacheKey }: ServiceListProps) {
export function ServiceList({ services, categories, onEdit, onDuplicate, onDeleted, cacheKey }: ServiceListProps) {
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [serviceToDelete, setServiceToDelete] = useState<Service | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
Expand Down Expand Up @@ -218,6 +219,7 @@ export function ServiceList({ services, categories, onEdit, onDeleted, cacheKey
<ServiceCardContext
service={service}
onEdit={onEdit}
onDuplicate={onDuplicate}
onDelete={handleDeleteClick}
index={index}
>
Expand Down
12 changes: 10 additions & 2 deletions components/common/context-menus/service-card-context.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import React from 'react';
import { Copy, ExternalLink, Pencil, Trash2 } from 'lucide-react';
import { Clipboard, CopyPlus, ExternalLink, Pencil, Trash2 } from 'lucide-react';
import { toast } from 'sonner';
import {
ContextMenu,
Expand All @@ -16,6 +16,7 @@ interface ServiceCardContextProps {
service: Service;
children: React.ReactNode;
onEdit: (service: Service) => void;
onDuplicate?: (service: Service) => void;
onDelete: (service: Service) => void;
index: number;
}
Expand All @@ -28,6 +29,7 @@ export function ServiceCardContext({
service,
children,
onEdit,
onDuplicate,
onDelete,
index,
}: ServiceCardContextProps) {
Expand Down Expand Up @@ -75,9 +77,15 @@ export function ServiceCardContext({
<Pencil />
Edit
</ContextMenuItem>
{onDuplicate && (
<ContextMenuItem onClick={() => onDuplicate(service)}>
<CopyPlus />
Duplicate
</ContextMenuItem>
)}
<ContextMenuSeparator />
<ContextMenuItem onClick={handleCopyUrl}>
<Copy />
<Clipboard />
Copy URL
</ContextMenuItem>
<ContextMenuItem onClick={handleOpenInNewTab}>
Expand Down
Loading