Continue on final review and Finish without inviting no longer open a login wall. The header stays Exit until Save your progress is done, and Send invitations is the only invite-path login.
132 lines
3.8 KiB
TypeScript
132 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
useCallback,
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
type ChangeEvent,
|
|
type RefObject,
|
|
} from "react";
|
|
import { COMMUNITY_AVATAR_ACCEPT } from "../../../../lib/create/createFlowUploadValidation";
|
|
import {
|
|
createFlowUploadFailureMessageKey,
|
|
uploadCreateFlowFile,
|
|
} from "../../../../lib/create/uploadToServer";
|
|
import { useMessages, useTranslation } from "../../../contexts/MessagesContext";
|
|
import { useCreateFlow } from "../context/CreateFlowContext";
|
|
|
|
function hasCommunityAvatarUrl(url: string | undefined): boolean {
|
|
return typeof url === "string" && url.trim().length > 0;
|
|
}
|
|
|
|
/**
|
|
* Pick and store a community logo for the create flow. Guests and signed-in
|
|
* users upload immediately (`POST /api/uploads` accepts `communityAvatar`
|
|
* without a session).
|
|
*
|
|
* @example
|
|
* const avatar = useCommunityAvatarUpload();
|
|
* <input ref={avatar.fileInputRef} type="file" onChange={avatar.handleFileChange} />
|
|
*/
|
|
export function useCommunityAvatarUpload(): {
|
|
fileInputRef: RefObject<HTMLInputElement | null>;
|
|
accept: string;
|
|
busy: boolean;
|
|
errorMessage: string | null;
|
|
displaySrc: string | null;
|
|
fileName: string | undefined;
|
|
handlePickFile: () => void;
|
|
handleFileChange: (_event: ChangeEvent<HTMLInputElement>) => Promise<void>;
|
|
} {
|
|
const m = useMessages();
|
|
const u = m.create.community.communityUpload;
|
|
const tUpload = useTranslation("create.upload");
|
|
const { markCreateFlowInteraction, state, updateState } = useCreateFlow();
|
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
|
const [localPreviewUrl, setLocalPreviewUrl] = useState<string | null>(null);
|
|
const [localFileName, setLocalFileName] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
if (localPreviewUrl) URL.revokeObjectURL(localPreviewUrl);
|
|
},
|
|
[localPreviewUrl],
|
|
);
|
|
|
|
const serverAvatarUrl = hasCommunityAvatarUrl(state.communityAvatarUrl)
|
|
? state.communityAvatarUrl!.trim()
|
|
: null;
|
|
|
|
useEffect(() => {
|
|
if (serverAvatarUrl) {
|
|
setLocalPreviewUrl((prev) => {
|
|
if (prev) URL.revokeObjectURL(prev);
|
|
return null;
|
|
});
|
|
}
|
|
}, [serverAvatarUrl]);
|
|
|
|
const resolveUploadError = useCallback(
|
|
(err: unknown) => {
|
|
const key = createFlowUploadFailureMessageKey(err);
|
|
if (key === "errors.tooLarge") return u.fileTooLarge;
|
|
return tUpload(key);
|
|
},
|
|
[tUpload, u.fileTooLarge],
|
|
);
|
|
|
|
const handleFileChange = useCallback(
|
|
async (e: ChangeEvent<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0];
|
|
e.target.value = "";
|
|
if (!file) return;
|
|
markCreateFlowInteraction();
|
|
setErrorMessage(null);
|
|
setBusy(true);
|
|
try {
|
|
const { url } = await uploadCreateFlowFile(file, "communityAvatar");
|
|
setLocalPreviewUrl((prev) => {
|
|
if (prev) URL.revokeObjectURL(prev);
|
|
return null;
|
|
});
|
|
setLocalFileName(file.name);
|
|
updateState({
|
|
communityAvatarUrl: url,
|
|
communityAvatarFileName: file.name,
|
|
});
|
|
} catch (err) {
|
|
setErrorMessage(resolveUploadError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
},
|
|
[markCreateFlowInteraction, resolveUploadError, updateState],
|
|
);
|
|
|
|
const handlePickFile = useCallback(() => {
|
|
if (!busy) fileInputRef.current?.click();
|
|
}, [busy]);
|
|
|
|
const displaySrc = serverAvatarUrl ?? localPreviewUrl;
|
|
const fileName =
|
|
localFileName ??
|
|
(typeof state.communityAvatarFileName === "string" &&
|
|
state.communityAvatarFileName.trim().length > 0
|
|
? state.communityAvatarFileName.trim()
|
|
: undefined);
|
|
|
|
return {
|
|
fileInputRef,
|
|
accept: COMMUNITY_AVATAR_ACCEPT,
|
|
busy,
|
|
errorMessage,
|
|
displaySrc,
|
|
fileName,
|
|
handlePickFile,
|
|
handleFileChange,
|
|
};
|
|
}
|