Skip to content

Commit 66be7f9

Browse files
committed
fix
1 parent 02ff49e commit 66be7f9

2 files changed

Lines changed: 48 additions & 76 deletions

File tree

packages/modal/src/components/MagicModalPortal/MagicModalPortal.tsx

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import { MagicModal } from "../MagicModal";
2222
import { MagicModalProvider } from "../MagicModalProvider";
2323

2424
const generatePseudoRandomID = () =>
25-
Math.random().toString(36).substring(7).toUpperCase() + Date.now();
25+
Math.random().toString(36).substring(7).toUpperCase() + String(Date.now());
2626

27-
type ModalStackItem = {
27+
interface ModalStackItem {
2828
id: string;
2929
component: ModalChildren;
3030
config: ModalProps;
3131
hideCallback: (value: unknown) => void;
3232
hideFunction: (props: unknown) => void;
33-
};
33+
}
3434
/**
3535
* @description A magic portal that should stay on the top of the app component hierarchy for the modal to be displayed.
3636
* @example
@@ -60,47 +60,53 @@ export const MagicModalPortal: React.FC = memo(() => {
6060
setFullWindowOverlayEnabled(true);
6161
}, []);
6262

63-
const _hide = useCallback<GlobalHideFunction>(
64-
async (props, { modalID } = {}) => {
65-
setModals((prevModals) => {
66-
const currentModal = prevModals.find((modal) => modal.id === modalID);
67-
68-
if (!modalID) {
69-
// eslint-disable-next-line no-console
70-
console.warn(
71-
"[DEPRECATED] react-native-magic-modal deprecated 'hide' usage:\nCalling magicModal.hide without a modal ID is deprecated and will be removed in future versions.\nPlease provide a modal id to hide or use the preferred `useMagicModal` hook inside the modal to hide itself.\nDefaulting to hiding the last modal in the stack.",
72-
);
73-
} else if (!currentModal) {
74-
// eslint-disable-next-line no-console
75-
console.log(
76-
`[HIDE EVENT IGNORED] No modal found with id: ${modalID}. It might have already been hidden.`,
77-
);
78-
return prevModals;
79-
}
80-
81-
if (prevModals.length === 0) {
82-
// eslint-disable-next-line no-console
83-
console.log(
84-
`[HIDE EVENT IGNORED] No modals found in the stack to hide. It might have already been hidden.`,
85-
);
86-
return prevModals;
87-
}
88-
89-
const safeModal = currentModal || prevModals[prevModals.length - 1]!;
90-
91-
safeModal.hideCallback(props);
92-
93-
return prevModals.filter((modal) => modal.id !== safeModal.id);
94-
});
95-
},
96-
[],
97-
);
63+
const _hide = useCallback<GlobalHideFunction>((props, { modalID } = {}) => {
64+
setModals((prevModals) => {
65+
const currentModal = prevModals.find((modal) => modal.id === modalID);
66+
67+
if (!modalID) {
68+
// We need to keep this console.warn for deprecation notice
69+
// eslint-disable-next-line no-console
70+
console.warn(
71+
"[DEPRECATED] react-native-magic-modal deprecated 'hide' usage:\nCalling magicModal.hide without a modal ID is deprecated and will be removed in future versions.\nPlease provide a modal id to hide or use the preferred `useMagicModal` hook inside the modal to hide itself.\nDefaulting to hiding the last modal in the stack.",
72+
);
73+
} else if (!currentModal) {
74+
// eslint-disable-next-line no-console
75+
console.log(
76+
`[HIDE EVENT IGNORED] No modal found with id: ${modalID}. It might have already been hidden.`,
77+
);
78+
return prevModals;
79+
}
80+
81+
if (prevModals.length === 0) {
82+
// eslint-disable-next-line no-console
83+
console.log(
84+
`[HIDE EVENT IGNORED] No modals found in the stack to hide. It might have already been hidden.`,
85+
);
86+
return prevModals;
87+
}
88+
89+
const safeModal = currentModal ?? prevModals[prevModals.length - 1];
90+
91+
if (!safeModal) {
92+
// eslint-disable-next-line no-console
93+
console.warn("[HIDE EVENT IGNORED] No modal found to hide.");
94+
return prevModals;
95+
}
96+
97+
safeModal.hideCallback(props);
98+
99+
return prevModals.filter((modal) => modal.id !== safeModal.id);
100+
});
101+
}, []);
98102

99103
const show = useCallback<GlobalShowFunction>(
100104
(newComponent, newConfig) => {
101105
const modalID = generatePseudoRandomID();
102106

103-
let hideCallback: (value: unknown) => void = () => {};
107+
let hideCallback: (value: unknown) => void = (_value) => {
108+
// This is intentionally empty as it will be replaced by the Promise resolver
109+
};
104110
const hidePromise = new Promise((resolve) => {
105111
hideCallback = resolve;
106112
});
@@ -119,7 +125,7 @@ export const MagicModalPortal: React.FC = memo(() => {
119125

120126
return {
121127
// This is already typed by the GlobalShowFunction type Generic
122-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
128+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
123129
promise: hidePromise as any,
124130
modalID,
125131
} as const;

tools/eslint/base.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const baseConfig = tseslint.config(
5959
"@shopify/no-namespace-imports": [
6060
"error",
6161
{
62-
allow: ["@radix-ui", "react"],
62+
allow: ["react"],
6363
},
6464
],
6565
"@shopify/no-ancestor-directory-import": ["error"],
@@ -100,39 +100,6 @@ const baseConfig = tseslint.config(
100100
],
101101
"no-console": "error",
102102
"no-void": 0,
103-
"no-restricted-imports": [
104-
"error",
105-
{
106-
paths: [
107-
{
108-
name: "sonner",
109-
message:
110-
"Import toast from @/lib/toast instead of using sonner directly.",
111-
},
112-
{
113-
name: "zod",
114-
message:
115-
"Import Zod only in @/schemas/{file-name}.ts directory. Create a schema file and import the types from there instead.",
116-
},
117-
{
118-
name: "react-i18next",
119-
importNames: ["Trans", "useTranslation"],
120-
message:
121-
"Import Trans and useTranslation from @/i18n/client instead of react-i18next directly.",
122-
},
123-
{
124-
name: "@ui/dialog",
125-
message:
126-
"Import Credenza from @ui/credenza instead. Credenza is mobile-friendly and automatically switches between Dialog and Drawer based on screen size.",
127-
},
128-
{
129-
name: "@ui/drawer",
130-
message:
131-
"Import Credenza from @ui/credenza instead. Credenza is mobile-friendly and automatically switches between Dialog and Drawer based on screen size.",
132-
},
133-
],
134-
},
135-
],
136103
"import/no-duplicates": "error",
137104
"import/no-anonymous-default-export": "off",
138105
"import/no-cycle": "error",
@@ -141,8 +108,7 @@ const baseConfig = tseslint.config(
141108
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
142109
"line-comment-position": "off",
143110
"unused-imports/no-unused-imports": "error",
144-
"@typescript-eslint/array-type": "off",
145-
"@typescript-eslint/consistent-type-definitions": "off",
111+
"@typescript-eslint/no-unnecessary-type-parameters": "off", // Broken rule, breaks our generics.
146112
"prefer-arrow-functions/prefer-arrow-functions": [
147113
"error",
148114
{

0 commit comments

Comments
 (0)