-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
5,189 additions
and
228 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 6 additions & 113 deletions
119
example-web/react2/src/components/DynamicCollectionConfigData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,21 @@ | ||
import React, { | ||
lazy, | ||
Suspense, | ||
useEffect, | ||
useState, | ||
useRef, | ||
useCallback, | ||
} from "react"; | ||
import React, { lazy, Suspense } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import { | ||
PetSimulator99API, | ||
CollectionName, | ||
Collection, | ||
CollectionConfigData, | ||
} from "ps99-api"; | ||
import { CollectionName } from "ps99-api"; | ||
|
||
const DynamicCollectionConfigData: React.FC = () => { | ||
const { collectionName, configName } = useParams<{ | ||
collectionName: CollectionName; | ||
configName: string; | ||
}>(); | ||
const { collectionName, configName } = useParams<{ collectionName: CollectionName; configName: string }>(); | ||
|
||
if (!collectionName) { | ||
return <div>Invalid collection name</div>; | ||
} | ||
|
||
if (configName === "all") { | ||
return <RenderAllConfigs collectionName={collectionName} />; | ||
if (!collectionName || !configName) { | ||
return <div>Invalid collection or config name</div>; | ||
} | ||
|
||
const Component = lazy(() => import(`./${collectionName}Component`)); | ||
|
||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Component /> | ||
<Component configName={configName} /> | ||
</Suspense> | ||
); | ||
}; | ||
|
||
const RenderAllConfigs: React.FC<{ collectionName: CollectionName }> = ({ | ||
collectionName, | ||
}) => { | ||
const [configDataList, setConfigDataList] = useState< | ||
Array<Collection<CollectionName>> | ||
>([]); | ||
const [page, setPage] = useState(0); | ||
const [error, setError] = useState<string | null>(null); | ||
const observer = useRef<IntersectionObserver | null>(null); | ||
const loadMoreRef = useRef<HTMLDivElement | null>(null); | ||
|
||
const fetchData = async (page: number) => { | ||
try { | ||
const api = new PetSimulator99API(); | ||
const response = await api.getCollection(collectionName); | ||
if (response.status === "ok") { | ||
const start = page * 20; | ||
const end = start + 20; | ||
setConfigDataList((prev) => [ | ||
...prev, | ||
...response.data.slice(start, end), | ||
]); | ||
} else { | ||
setError(response.error.message); | ||
} | ||
} catch (error) { | ||
setError("Error fetching data"); | ||
console.error("Error fetching data:", error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchData(page); | ||
}, [page]); | ||
|
||
const lastElementRef = useCallback((node: HTMLDivElement | null) => { | ||
if (observer.current) observer.current.disconnect(); | ||
observer.current = new IntersectionObserver((entries) => { | ||
if (entries[0].isIntersecting) { | ||
setPage((prevPage) => prevPage + 1); | ||
} | ||
}); | ||
if (node) observer.current.observe(node); | ||
}, []); | ||
|
||
if (error) { | ||
return <div>Error: {error}</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
{configDataList.map((configData, index) => { | ||
const Component = lazy(() => import(`./${collectionName}Component`)); | ||
if (index === configDataList.length - 1) { | ||
return ( | ||
<div ref={lastElementRef} key={index}> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Component | ||
configData={ | ||
configData.configData as CollectionConfigData< | ||
typeof collectionName | ||
> | ||
} | ||
/> | ||
</Suspense> | ||
</div> | ||
); | ||
} else { | ||
return ( | ||
<div key={index}> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Component | ||
configData={ | ||
configData.configData as CollectionConfigData< | ||
typeof collectionName | ||
> | ||
} | ||
/> | ||
</Suspense> | ||
</div> | ||
); | ||
} | ||
})} | ||
<div ref={loadMoreRef}></div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DynamicCollectionConfigData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { useOnlineStatus } from "../hooks/useOnlineStatus"; | ||
|
||
const Footer: React.FC = () => { | ||
const isOnline = useOnlineStatus(); | ||
const [lastUpdate, setLastUpdate] = useState<string | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const updateLastUpdate = () => { | ||
setLastUpdate(new Date().toLocaleString()); | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
setLoading(true); | ||
// Simulate a fetch call to update lastUpdate time | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
updateLastUpdate(); | ||
setLoading(false); | ||
}; | ||
|
||
fetchData(); | ||
|
||
window.addEventListener("online", updateLastUpdate); | ||
window.addEventListener("offline", updateLastUpdate); | ||
|
||
return () => { | ||
window.removeEventListener("online", updateLastUpdate); | ||
window.removeEventListener("offline", updateLastUpdate); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<footer | ||
style={{ | ||
display: "flex", | ||
justifyContent: "space-between", | ||
padding: "1em", | ||
borderTop: "1px solid #ccc", | ||
}} | ||
> | ||
<div> | ||
{loading ? ( | ||
<span>♻️ Loading...</span> | ||
) : ( | ||
<span>Last update: {lastUpdate}</span> | ||
)} | ||
</div> | ||
<div> | ||
{isOnline ? ( | ||
<span style={{ color: "green" }}>● Online</span> | ||
) : ( | ||
<span style={{ color: "red" }}>● Offline</span> | ||
)} | ||
</div> | ||
</footer> | ||
); | ||
}; | ||
|
||
export default Footer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.