mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
feat(settings): add media hosting settings and provider-based upload gating
This commit is contained in:
@@ -27,6 +27,11 @@ vi.mock('../../lib/utils/catbox-utils', () => ({
|
||||
uploadToCatbox: vi.fn(),
|
||||
}));
|
||||
|
||||
const selectedProviderRef = vi.hoisted(() => ({ value: 'catbox' as string }));
|
||||
vi.mock('../../stores/use-media-hosting-store', () => ({
|
||||
default: (selector: (s: { selectedProvider: string }) => unknown) => selector({ selectedProvider: selectedProviderRef.value }),
|
||||
}));
|
||||
|
||||
type HookSnapshot = ReturnType<typeof useFileUpload>;
|
||||
|
||||
let latestHook: HookSnapshot | null = null;
|
||||
@@ -68,6 +73,7 @@ const selectFileFromHiddenInput = async (file: File | null) => {
|
||||
describe('useFileUpload', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
selectedProviderRef.value = 'catbox';
|
||||
latestHook = null;
|
||||
container = document.createElement('div');
|
||||
document.body.append(container);
|
||||
@@ -177,4 +183,39 @@ describe('useFileUpload', () => {
|
||||
expect(onUploadComplete).not.toHaveBeenCalled();
|
||||
expect(hook().isUploading).toBe(false);
|
||||
});
|
||||
|
||||
it('returns early when selectedProvider is none (no upload, no alert)', async () => {
|
||||
selectedProviderRef.value = 'none';
|
||||
vi.mocked(Capacitor.getPlatform).mockReturnValue('android');
|
||||
vi.mocked(FileUploader.pickAndUploadMedia).mockResolvedValue({
|
||||
url: 'https://files.catbox.moe/skip.jpg',
|
||||
fileName: 'skip.jpg',
|
||||
});
|
||||
const { onUploadComplete, hook } = mountHook();
|
||||
|
||||
await act(async () => {
|
||||
await hook().handleUpload();
|
||||
});
|
||||
|
||||
expect(FileUploader.pickAndUploadMedia).not.toHaveBeenCalled();
|
||||
expect(window.alert).not.toHaveBeenCalled();
|
||||
expect(onUploadComplete).not.toHaveBeenCalled();
|
||||
expect(hook().isUploading).toBe(false);
|
||||
});
|
||||
|
||||
it('returns early when selectedProvider is unknown (no crash, no upload)', async () => {
|
||||
selectedProviderRef.value = 'unknown-provider';
|
||||
vi.mocked(Capacitor.getPlatform).mockReturnValue('ios');
|
||||
window.electronApi = { isElectron: true } as any;
|
||||
const { onUploadComplete, hook } = mountHook();
|
||||
|
||||
await act(async () => {
|
||||
await hook().handleUpload();
|
||||
});
|
||||
|
||||
expect(uploadToCatbox).not.toHaveBeenCalled();
|
||||
expect(window.alert).not.toHaveBeenCalled();
|
||||
expect(onUploadComplete).not.toHaveBeenCalled();
|
||||
expect(hook().isUploading).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,9 +3,15 @@ import { Capacitor } from '@capacitor/core';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import FileUploader from '../plugins/file-uploader';
|
||||
import { uploadToCatbox } from '../lib/utils/catbox-utils';
|
||||
import useMediaHostingStore from '../stores/use-media-hosting-store';
|
||||
|
||||
const FILE_SELECTION_CANCELLED_ERROR = 'File selection cancelled';
|
||||
|
||||
async function uploadByProvider(provider: string, file: File): Promise<string> {
|
||||
if (provider === 'catbox') return uploadToCatbox(file);
|
||||
throw new Error(`Unsupported media provider: ${provider}`);
|
||||
}
|
||||
|
||||
export interface UseFileUploadOptions {
|
||||
onUploadComplete: (url: string, fileName: string) => void;
|
||||
}
|
||||
@@ -54,10 +60,14 @@ function selectFileViaInput(): Promise<File | null> {
|
||||
export function useFileUpload(options: UseFileUploadOptions) {
|
||||
const { t } = useTranslation();
|
||||
const { onUploadComplete } = options;
|
||||
const selectedProvider = useMediaHostingStore((s) => s.selectedProvider);
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const [uploadedFileName, setUploadedFileName] = useState<string | null>(null);
|
||||
|
||||
const handleUpload = useCallback(async () => {
|
||||
if (selectedProvider === 'none') return;
|
||||
if (selectedProvider !== 'catbox') return;
|
||||
|
||||
try {
|
||||
setIsUploading(true);
|
||||
setUploadedFileName(null);
|
||||
@@ -79,7 +89,7 @@ export function useFileUpload(options: UseFileUploadOptions) {
|
||||
throw new Error(FILE_SELECTION_CANCELLED_ERROR);
|
||||
}
|
||||
|
||||
const url = await uploadToCatbox(file);
|
||||
const url = await uploadByProvider(selectedProvider, file);
|
||||
setUploadedFileName(file.name);
|
||||
onUploadComplete(url, file.name);
|
||||
return;
|
||||
@@ -94,7 +104,7 @@ export function useFileUpload(options: UseFileUploadOptions) {
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
}
|
||||
}, [onUploadComplete, t]);
|
||||
}, [onUploadComplete, t, selectedProvider]);
|
||||
|
||||
return {
|
||||
isUploading,
|
||||
|
||||
Reference in New Issue
Block a user