@@ -15,8 +15,6 @@ import { v4 as uuidv4 } from 'uuid';
1515
1616import vars from 'src/domains/styling/utils/vars' ;
1717
18- import Modal from './Modal' ;
19-
2018type Modal = {
2119 id : string ,
2220 modal : ReactElement ,
@@ -26,8 +24,6 @@ type Modal = {
2624type ModalContextType = {
2725 mount : ( modal : Modal , options ?: { checkDuplicateBy ?: string [ ] } ) => void ,
2826 unmount : ( id : string ) => void ,
29- updateId : ( oldId : string , newId : string ) => Promise < Modal | null > ,
30- getModals : ( ) => Modal [ ] ,
3127 modals : Modal [ ] ,
3228} ;
3329
@@ -59,31 +55,8 @@ export const ModalProvider = ({ children }: { children: ReactNode }) => {
5955 setModals ( prev => prev . filter ( m => m . id !== id ) ) ;
6056 } , [ ] ) ;
6157
62- const updateId = useCallback ( async ( oldId : string , newId : string ) : Promise < Modal | null > => {
63- return new Promise ( resolve => {
64- setModals ( prev => {
65- const modalIndex = prev . findIndex ( modal => modal . id === oldId ) ;
66-
67- if ( modalIndex === - 1 ) {
68- resolve ( null ) ;
69- return prev ;
70- }
71-
72- const updatedModal = { ...prev [ modalIndex ] , id : newId } ;
73- const updatedModals = prev . map ( ( modal , index ) =>
74- index === modalIndex ? updatedModal : modal
75- ) ;
76-
77- resolve ( updatedModal ) ;
78- return updatedModals ;
79- } ) ;
80- } ) ;
81- } , [ ] ) ;
82-
83- const getModals = ( ) => modals ;
84-
8558 return (
86- < ModalContext . Provider value = { { mount, unmount, updateId , modals, getModals } } >
59+ < ModalContext . Provider value = { { mount, unmount, modals } } >
8760 { children }
8861 { createPortal (
8962 < AnimatePresence >
@@ -123,9 +96,9 @@ export const useModals = () => {
12396} ;
12497
12598export const useModal = ( ) => {
126- const { mount, unmount, modals, updateId : _updateId } = useModals ( ) ;
127- const defaultId = useRef ( uuidv4 ( ) ) . current ;
128- const currentIdRef = useRef < string > ( defaultId ) ;
99+ const { mount, unmount, modals } = useModals ( ) ;
100+ const [ defaultId ] = useState ( uuidv4 ( ) ) ;
101+ const currentIdRef = useRef ( defaultId ) ;
129102
130103 const open = useCallback ( ( modalElement : ReactElement , options ?: { idOverride ?: string } ) => {
131104 const modalId = options ?. idOverride ?? defaultId ;
@@ -137,23 +110,18 @@ export const useModal = () => {
137110 unmount ( currentIdRef . current ) ;
138111 } , [ unmount ] ) ;
139112
140- const updateId = async ( newId : string ) => {
141- return _updateId ( currentIdRef . current , newId ) ;
142- } ;
143-
144113 const modal = modals . find ( m => m . id === currentIdRef . current ) ;
145114
146115 return {
147116 open,
148117 close,
149118 isOpen : ! ! modal ,
150- updateId,
151119 } ;
152120} ;
153121
154122export const useModalControls = ( ) => {
155123 const ctx = useContext ( ModalControlsContext ) ;
156- if ( ! ctx ) throw new Error ( 'useModal must be used within ModalControlsProvider' ) ;
124+ if ( ! ctx ) throw new Error ( 'useModalControls must be used within ModalControlsProvider' ) ;
157125
158126 return ctx ;
159127} ;
0 commit comments