refactor: modal interface

This commit is contained in:
kawamataryo 2024-11-24 20:04:39 +09:00
parent 0428a6f93c
commit 1640928837
4 changed files with 19 additions and 22 deletions

View File

@ -23,7 +23,6 @@ export const getStyle = () => {
const App = () => { const App = () => {
const { const {
initialize, initialize,
modalRef,
users, users,
loading, loading,
stopRetrieveLoop, stopRetrieveLoop,
@ -32,6 +31,12 @@ const App = () => {
errorMessage, errorMessage,
} = useRetrieveBskyUsers(); } = useRetrieveBskyUsers();
const [isModalOpen, setIsModalOpen] = React.useState(false);
const closeModal = () => {
setIsModalOpen(false);
stopRetrieveLoop();
};
React.useEffect(() => { React.useEffect(() => {
const messageHandler = ( const messageHandler = (
message: { message: {
@ -43,6 +48,7 @@ const App = () => {
if (Object.values(MESSAGE_NAMES).includes(message.name)) { if (Object.values(MESSAGE_NAMES).includes(message.name)) {
initialize() initialize()
.then(() => { .then(() => {
setIsModalOpen(true);
sendResponse({ hasError: false }); sendResponse({ hasError: false });
}) })
.catch((e) => { .catch((e) => {
@ -72,7 +78,7 @@ const App = () => {
return ( return (
<> <>
<Modal anchorRef={modalRef} onClose={stopRetrieveLoop}> <Modal open={isModalOpen} onClose={closeModal}>
<div className="flex flex-col gap-2 items-center"> <div className="flex flex-col gap-2 items-center">
{loading && ( {loading && (
<p className="text-lg font-bold"> <p className="text-lg font-bold">

View File

@ -1,8 +1,6 @@
import type { Meta, StoryObj } from "@storybook/react"; import type { Meta, StoryObj } from "@storybook/react";
import { useState } from "react";
import { useRef } from "react";
import Modal from "./Modal"; import Modal from "./Modal";
import type { Props as UserCardProps } from "./UserCard";
const meta: Meta<typeof Modal> = { const meta: Meta<typeof Modal> = {
title: "Components/Modal", title: "Components/Modal",
@ -10,22 +8,22 @@ const meta: Meta<typeof Modal> = {
}; };
export default meta; export default meta;
type Story = StoryObj<{ items: UserCardProps["user"][] }>; type Story = StoryObj<typeof Modal>;
const DefaultTemplate: Story = { const DefaultTemplate: Story = {
render: () => { render: () => {
const modalRef = useRef<HTMLDialogElement>(null); const [isModalOpen, setIsModalOpen] = useState(false);
return ( return (
<> <>
<button <button
type="button" type="button"
className="btn btn-primary" className="btn btn-primary"
onClick={() => modalRef.current?.showModal()} onClick={() => setIsModalOpen(true)}
> >
open open
</button> </button>
<Modal anchorRef={modalRef}> <Modal open={isModalOpen} onClose={() => setIsModalOpen(false)}>
<p>Modal content</p> <p>Modal content</p>
</Modal> </Modal>
</> </>

View File

@ -1,14 +1,15 @@
import type React from "react"; import type React from "react";
import { useEffect } from "react"; import { useEffect, useRef } from "react";
export type Props = { export type Props = {
children: React.ReactNode; children: React.ReactNode;
anchorRef: React.RefObject<HTMLDialogElement>; open: boolean;
open?: boolean;
onClose?: () => void; onClose?: () => void;
}; };
const Modal = ({ children, anchorRef, open = false, onClose }: Props) => { const Modal = ({ children, open = false, onClose }: Props) => {
const anchorRef = useRef<HTMLDialogElement>(null);
useEffect(() => { useEffect(() => {
if (anchorRef.current) { if (anchorRef.current) {
anchorRef.current.addEventListener("close", onClose); anchorRef.current.addEventListener("close", onClose);
@ -19,7 +20,7 @@ const Modal = ({ children, anchorRef, open = false, onClose }: Props) => {
anchorRef.current.removeEventListener("close", onClose); anchorRef.current.removeEventListener("close", onClose);
} }
}; };
}, [anchorRef, onClose]); }, [onClose]);
return ( return (
<> <>

View File

@ -45,11 +45,6 @@ export const useRetrieveBskyUsers = () => {
messageName: (typeof MESSAGE_NAMES)[keyof typeof MESSAGE_NAMES]; messageName: (typeof MESSAGE_NAMES)[keyof typeof MESSAGE_NAMES];
}>(null); }>(null);
const modalRef = React.useRef<HTMLDialogElement>(null);
const showModal = () => {
modalRef.current?.showModal();
};
const retrieveBskyUsers = React.useCallback( const retrieveBskyUsers = React.useCallback(
async (usersData: CrawledUserInfo[]) => { async (usersData: CrawledUserInfo[]) => {
for (const userData of usersData) { for (const userData of usersData) {
@ -151,7 +146,6 @@ export const useRetrieveBskyUsers = () => {
}); });
setLoading(true); setLoading(true);
await setUsers([]); await setUsers([]);
showModal();
}, []); }, []);
const restart = React.useCallback(() => { const restart = React.useCallback(() => {
@ -173,8 +167,6 @@ export const useRetrieveBskyUsers = () => {
); );
return { return {
modalRef,
showModal,
initialize, initialize,
users, users,
loading, loading,