Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Generated AI description not updated automatically (#2309)

Stanimird5bfe618b90e
diff --git a/src/renderer/components/md/DescriptionMdEditor.tsx b/src/renderer/components/md/DescriptionMdEditor.tsx
index c086db494..519041ff3 100644
--- a/src/renderer/components/md/DescriptionMdEditor.tsx
+++ b/src/renderer/components/md/DescriptionMdEditor.tsx
@@ -28,6 +28,8 @@ import { useTranslation } from 'react-i18next';
import { Pro } from '-/pro';
import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { replaceAll } from '@milkdown/utils';
+import { useEditedEntryMetaContext } from '-/hooks/useEditedEntryMetaContext';
+import useFirstRender from '-/utils/useFirstRender';
interface CrepeMdEditorProps {
//onChange?: (markdown: string, prevMarkdown: string) => void;
@@ -39,6 +41,7 @@ const DescriptionMdEditor = forwardRef<CrepeRef, CrepeMdEditorProps>(
const { onFocus } = props;
const { t } = useTranslation();
const { currentDirectoryPath } = useDirectoryContentContext();
+ const { metaActions } = useEditedEntryMetaContext();
const {
saveDescription,
isEditDescriptionMode,
@@ -48,6 +51,7 @@ const DescriptionMdEditor = forwardRef<CrepeRef, CrepeMdEditorProps>(
} = useFilePropertiesContext();
const { openedEntry, openLink } = useOpenedEntryContext();
const crepeInstanceRef = useRef<Crepe>(undefined);
+ const firstRender = useFirstRender();
const { get, loading } = useEditor(
(root) => {
@@ -115,19 +119,35 @@ const DescriptionMdEditor = forwardRef<CrepeRef, CrepeMdEditorProps>(
useEffect(() => {
if (!isDescriptionChanged) {
- const editor = get();
- if (!loading && editor && editor.status === EditorStatus.Created) {
- try {
- const markdown = editor.action(getMarkdown());
- if (markdown !== openedEntry.meta?.description) {
- editor.action(replaceAll(openedEntry.meta?.description, true));
+ changeDescription(openedEntry.meta?.description);
+ }
+ }, [openedEntry]); //, isDescriptionChanged]);
+
+ useEffect(() => {
+ if (!firstRender && metaActions && metaActions.length > 0) {
+ for (const action of metaActions) {
+ if (action.entry && openedEntry.path === action.entry.path) {
+ if (action.action === 'descriptionChange') {
+ changeDescription(action.entry.meta?.description);
}
- } catch (e) {
- console.log(e);
}
}
}
- }, [openedEntry]);
+ }, [metaActions]);
+
+ function changeDescription(description: string) {
+ const editor = get();
+ if (!loading && editor && editor.status === EditorStatus.Created) {
+ try {
+ const markdown = editor.action(getMarkdown());
+ if (markdown !== description) {
+ editor.action(replaceAll(description, true));
+ }
+ } catch (e) {
+ console.log(e);
+ }
+ }
+ }
useCrepeHandler(ref, () => crepeInstanceRef.current, get, loading);
diff --git a/src/renderer/hooks/FilePropertiesContextProvider.tsx b/src/renderer/hooks/FilePropertiesContextProvider.tsx
index 02d29a08a..ce20e47ec 100644
--- a/src/renderer/hooks/FilePropertiesContextProvider.tsx
+++ b/src/renderer/hooks/FilePropertiesContextProvider.tsx
@@ -73,7 +73,7 @@ export const FilePropertiesContextProvider = ({
const { showNotification } = useNotificationContext();
const { setDescriptionChange } = useIOActionsContext();
- const lastOpenedFile = useRef<TS.OpenedEntry>(openedEntry);
+ const lastOpenedFile = useRef<TS.OpenedEntry>({ ...openedEntry });
const isDescriptionChanged = useRef<boolean>(false);
const [isEditMode, setIsEditMode] = useState<boolean>(false);
@@ -99,10 +99,13 @@ export const FilePropertiesContextProvider = ({
if (isDescriptionChanged.current) {
// handle not saved changes
saveDescriptionConfirmOpened(true);
- } else {
+ } else if (
+ JSON.stringify(lastOpenedFile.current) !== JSON.stringify(openedEntry)
+ ) {
if (lastOpenedFile.current?.path !== openedEntry.path) {
setIsEditMode(false);
}
+
lastOpenedFile.current = { ...openedEntry };
forceUpdate();
}