Skip to content

Commit cd7a395

Browse files
committed
save3-bug-dragable-moda
1 parent 22886e1 commit cd7a395

File tree

4 files changed

+85
-49
lines changed

4 files changed

+85
-49
lines changed

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocGridContentList.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getEventCoordinates } from '@dnd-kit/utilities';
33
import { useModal } from '@gouvfr-lasuite/cunningham-react';
44
import { TreeViewMoveModeEnum } from '@gouvfr-lasuite/ui-kit';
55
import { useQueryClient } from '@tanstack/react-query';
6-
import { useMemo, useRef } from 'react';
6+
import { useEffect, useMemo, useRef, useState } from 'react';
77
import { Trans, useTranslation } from 'react-i18next';
88

99
import { AlertModal, Card, Text } from '@/components';
@@ -15,6 +15,7 @@ import {
1515
useDeleteDocInvitation,
1616
} from '@/docs/doc-share';
1717
import { useMoveDoc } from '@/docs/doc-tree';
18+
import { useResponsiveStore } from '@/stores/useResponsiveStore';
1819

1920
import { DocDragEndData, useDragAndDrop } from '../hooks/useDragAndDrop';
2021

@@ -134,7 +135,6 @@ export const DraggableDocGridContentList = ({
134135
handleDragEnd,
135136
updateCanDrop,
136137
} = useDragAndDrop(onDrag);
137-
138138
const { t } = useTranslation();
139139

140140
const overlayText = useMemo(() => {
@@ -151,10 +151,6 @@ export const DraggableDocGridContentList = ({
151151
const cannotMoveDoc =
152152
!canDrag || (canDrop !== undefined && !canDrop) || isError;
153153

154-
if (docs.length === 0) {
155-
return null;
156-
}
157-
158154
return (
159155
<>
160156
<DndContext
@@ -246,10 +242,34 @@ export const DraggableDocGridItem = ({
246242
};
247243

248244
export const DocGridContentList = ({ docs }: DocGridContentListProps) => {
245+
const { isDesktop } = useResponsiveStore();
246+
const [isDraggableDisabled, setIsDraggableDisabled] = useState(false);
247+
248+
useEffect(() => {
249+
const checkModal = () => {
250+
const modalOpen = document.querySelector('[role="dialog"]');
251+
setIsDraggableDisabled(!!modalOpen);
252+
};
253+
254+
checkModal();
255+
256+
const observer = new MutationObserver(checkModal);
257+
observer.observe(document.body, {
258+
childList: true,
259+
subtree: true,
260+
});
261+
262+
return () => observer.disconnect();
263+
}, []);
264+
249265
if (docs.length === 0) {
250266
return null;
251267
}
252268

269+
if (isDesktop && !isDraggableDisabled) {
270+
return <DraggableDocGridContentList docs={docs} />;
271+
}
272+
253273
return docs.map((doc) => (
254274
<DocsGridItem dragMode={false} doc={doc} key={doc.id} />
255275
));

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocsGrid.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { useImport } from '../../docs-import/hooks/useImport';
1414
import { useInfiniteDocsTrashbin } from '../api';
1515
import { useResponsiveDocGrid } from '../hooks/useResponsiveDocGrid';
1616

17-
import {
18-
DocGridContentList,
19-
DraggableDocGridContentList,
20-
} from './DocGridContentList';
17+
import { DocGridContentList } from './DocGridContentList';
2118
import { DocsGridLoader } from './DocsGridLoader';
2219

2320
type DocsGridProps = {
@@ -153,11 +150,7 @@ export const DocsGrid = ({
153150
</Box>
154151
</Box>
155152
<Box role="rowgroup">
156-
{isDesktop ? (
157-
<DraggableDocGridContentList docs={docs} />
158-
) : (
159-
<DocGridContentList docs={docs} />
160-
)}
153+
<DocGridContentList docs={docs} />
161154
</Box>
162155
</Box>
163156
{hasNextPage && !loading && (

src/frontend/apps/impress/src/features/docs/docs-grid/components/Draggable.tsx

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Data, useDraggable } from '@dnd-kit/core';
2-
import { useEffect, useState } from 'react';
32

43
type DraggableProps<T> = {
54
id: string;
@@ -8,44 +7,11 @@ type DraggableProps<T> = {
87
};
98

109
export const Draggable = <T,>(props: DraggableProps<T>) => {
11-
const [isDisabled, setIsDisabled] = useState(false);
12-
1310
const { attributes, listeners, setNodeRef } = useDraggable({
1411
id: props.id,
1512
data: props.data,
16-
disabled: isDisabled,
1713
});
1814

19-
useEffect(() => {
20-
const checkModal = () => {
21-
const modalOpen = document.querySelector('[role="dialog"]');
22-
setIsDisabled(!!modalOpen);
23-
};
24-
25-
checkModal();
26-
27-
const observer = new MutationObserver(checkModal);
28-
observer.observe(document.body, {
29-
childList: true,
30-
subtree: true,
31-
});
32-
33-
return () => observer.disconnect();
34-
}, []);
35-
36-
if (isDisabled) {
37-
return (
38-
<div
39-
data-testid={`draggable-doc-${props.id}`}
40-
className="--docs--grid-draggable --disabled"
41-
role="none"
42-
style={{ pointerEvents: 'none' }}
43-
>
44-
{props.children}
45-
</div>
46-
);
47-
}
48-
4915
return (
5016
<div
5117
ref={setNodeRef}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useEffect, useState } from 'react';
2+
3+
let observer: MutationObserver | null = null;
4+
let listeners = new Set<(isOpen: boolean) => void>();
5+
let currentModalState = false;
6+
7+
const checkModal = () => {
8+
const modalOpen = !!document.querySelector('[role="dialog"]');
9+
if (modalOpen !== currentModalState) {
10+
currentModalState = modalOpen;
11+
listeners.forEach((listener) => listener(modalOpen));
12+
}
13+
};
14+
15+
const initObserver = () => {
16+
if (!observer) {
17+
observer = new MutationObserver(checkModal);
18+
observer.observe(document.body, {
19+
childList: true,
20+
subtree: true,
21+
});
22+
}
23+
};
24+
25+
const cleanupObserver = () => {
26+
if (observer && listeners.size === 0) {
27+
observer.disconnect();
28+
observer = null;
29+
}
30+
};
31+
32+
export const useModalDetection = () => {
33+
const [isModalOpen, setIsModalOpen] = useState(currentModalState);
34+
35+
useEffect(() => {
36+
// Initialize observer if this is the first listener
37+
if (listeners.size === 0) {
38+
initObserver();
39+
checkModal(); // Check immediately
40+
}
41+
42+
// Add this component's listener
43+
listeners.add(setIsModalOpen);
44+
45+
// Set initial state
46+
setIsModalOpen(currentModalState);
47+
48+
return () => {
49+
// Remove this component's listener
50+
listeners.delete(setIsModalOpen);
51+
// Cleanup observer if this was the last listener
52+
cleanupObserver();
53+
};
54+
}, []);
55+
56+
return isModalOpen;
57+
};

0 commit comments

Comments
 (0)