mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 15:03:22 +00:00
54 lines
2 KiB
TypeScript
54 lines
2 KiB
TypeScript
import type { MediaFile } from "@/domain/work-orders/types/work-order-media";
|
|
|
|
const COMPLETION_PURPOSES = new Set(["Before", "After", "Completion"]);
|
|
|
|
export type MediaSurfaceOwnership = {
|
|
completionOwnedIds: ReadonlySet<string>;
|
|
extraOwnedIds: ReadonlySet<string>;
|
|
};
|
|
|
|
const EMPTY_OWNERSHIP: MediaSurfaceOwnership = {
|
|
completionOwnedIds: new Set(),
|
|
extraOwnedIds: new Set(),
|
|
};
|
|
|
|
/** Only Reactive and Emergency work orders render a media gallery on the Completion tab. */
|
|
export function hasCompletionMediaGallery(type: string | null | undefined): boolean {
|
|
return type === "Emergency" || type === "Reactive";
|
|
}
|
|
|
|
function isPhotoOrVideo(item: MediaFile): boolean {
|
|
return item.fileType === "image" || item.fileType === "video";
|
|
}
|
|
|
|
function isInFlight(item: MediaFile): boolean {
|
|
return item.uploadStatus === "uploading" || item.uploadStatus === "failed";
|
|
}
|
|
|
|
/** Extra photo/video from Completion Doc POST (no category) until Extra is chosen. */
|
|
export function isCompletionSurfaceMedia(
|
|
item: MediaFile,
|
|
ownership: MediaSurfaceOwnership = EMPTY_OWNERSHIP,
|
|
completionHasGallery = true,
|
|
): boolean {
|
|
if (isInFlight(item)) return true;
|
|
if (COMPLETION_PURPOSES.has(item.purpose ?? "")) return true;
|
|
if (!completionHasGallery && item.purpose === "Extra") return false;
|
|
if (ownership.completionOwnedIds.has(item.id)) return true;
|
|
if (ownership.extraOwnedIds.has(item.id)) return false;
|
|
return item.purpose === "Extra" && isPhotoOrVideo(item);
|
|
}
|
|
|
|
/** Without a Completion gallery, every Extra item (including photos/videos) belongs to Extras. */
|
|
export function isExtrasSurfaceMedia(
|
|
item: MediaFile,
|
|
ownership: MediaSurfaceOwnership = EMPTY_OWNERSHIP,
|
|
completionHasGallery = true,
|
|
): boolean {
|
|
if (item.purpose === "Aveta") return true;
|
|
if (item.purpose !== "Extra") return false;
|
|
if (!completionHasGallery) return true;
|
|
if (ownership.completionOwnedIds.has(item.id)) return false;
|
|
if (ownership.extraOwnedIds.has(item.id)) return true;
|
|
return !isPhotoOrVideo(item);
|
|
}
|