* Bump deps * Add openai object generation with zod * Streamline AI query instructions * Use single AI request when editing uploads * Clean up AI text from object requests * Fix AI text formatting logic * Reorganize upload network requests
122 lines
2.9 KiB
TypeScript
122 lines
2.9 KiB
TypeScript
import { useCallback, useEffect, useRef } from 'react';
|
|
import useAiImageQuery from './useAiImageQuery';
|
|
import useTitleCaptionAiImageQuery from './useTitleCaptionAiImageQuery';
|
|
import { AI_AUTO_GENERATED_FIELDS_ALL, AiAutoGeneratedField } from '.';
|
|
|
|
export type AiContent = ReturnType<typeof useAiImageQueries>;
|
|
|
|
export default function useAiImageQueries(
|
|
imageBase64?: string,
|
|
textFieldsToAutoGenerate: AiAutoGeneratedField[] = [],
|
|
) {
|
|
const [
|
|
requestTitleCaption,
|
|
_title,
|
|
_caption,
|
|
_isLoadingTitle,
|
|
_isLoadingCaption,
|
|
resetTitle,
|
|
resetCaption,
|
|
] = useTitleCaptionAiImageQuery(imageBase64);
|
|
|
|
const [
|
|
requestTitle,
|
|
titleSolo,
|
|
isLoadingTitleSolo,
|
|
resetTitleSolo,
|
|
] = useAiImageQuery(imageBase64, 'title');
|
|
|
|
const [
|
|
requestCaption,
|
|
captionSolo,
|
|
isLoadingCaptionSolo,
|
|
resetCaptionSolo,
|
|
] = useAiImageQuery(imageBase64, 'caption');
|
|
|
|
const [
|
|
requestTags,
|
|
tags,
|
|
isLoadingTags,
|
|
] = useAiImageQuery(imageBase64, 'tags');
|
|
|
|
const [
|
|
requestSemantic,
|
|
semanticDescription,
|
|
isLoadingSemantic,
|
|
] = useAiImageQuery(imageBase64, 'semantic');
|
|
|
|
const title = _title || titleSolo;
|
|
const caption = _caption || captionSolo;
|
|
const isLoadingTitle = _isLoadingTitle || isLoadingTitleSolo;
|
|
const isLoadingCaption = _isLoadingCaption || isLoadingCaptionSolo;
|
|
|
|
const isLoading =
|
|
isLoadingTitle ||
|
|
isLoadingCaption ||
|
|
isLoadingTags ||
|
|
isLoadingSemantic;
|
|
|
|
const hasRunAllQueriesOnce = useRef(false);
|
|
|
|
const request = useCallback(async (
|
|
fields = AI_AUTO_GENERATED_FIELDS_ALL,
|
|
) => {
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
console.log('RUNNING AI QUERIES', fields);
|
|
}
|
|
hasRunAllQueriesOnce.current = true;
|
|
if (fields.includes('title') && fields.includes('caption')) {
|
|
// Unmask individual title + caption
|
|
resetTitleSolo();
|
|
resetCaptionSolo();
|
|
requestTitleCaption();
|
|
} else {
|
|
if (fields.includes('title')) {
|
|
// Unmask combined title
|
|
resetTitle();
|
|
resetTitleSolo();
|
|
requestTitle();
|
|
}
|
|
if (fields.includes('caption')) {
|
|
// Unmask combined caption
|
|
resetCaption();
|
|
resetCaptionSolo();
|
|
requestCaption();
|
|
}
|
|
}
|
|
if (fields.includes('tags')) { requestTags(); }
|
|
if (fields.includes('semantic')) { requestSemantic(); }
|
|
}, [
|
|
requestTitleCaption,
|
|
requestTitle,
|
|
requestCaption,
|
|
requestTags,
|
|
requestSemantic,
|
|
resetTitle,
|
|
resetTitleSolo,
|
|
resetCaption,
|
|
resetCaptionSolo,
|
|
]);
|
|
|
|
useEffect(() => {
|
|
if (imageBase64 && !hasRunAllQueriesOnce.current) {
|
|
if (textFieldsToAutoGenerate.length > 0) {
|
|
request(textFieldsToAutoGenerate);
|
|
}
|
|
}
|
|
}, [textFieldsToAutoGenerate, imageBase64, request]);
|
|
|
|
return {
|
|
request,
|
|
title,
|
|
caption,
|
|
tags,
|
|
semanticDescription,
|
|
isLoading,
|
|
isLoadingTitle,
|
|
isLoadingCaption,
|
|
isLoadingTags,
|
|
isLoadingSemantic,
|
|
};
|
|
}
|