Skip to content

Commit

Permalink
Merge branch 'master' into char0n/dependabot-timing
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed May 27, 2024
2 parents 0f2501a + df03a8f commit e5e87db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
26 changes: 22 additions & 4 deletions flavors/swagger-ui-react/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
*/
"use client"

import React, { useEffect, useState } from "react"
import React, { useEffect, useRef, useState } from "react"
import PropTypes from "prop-types"
import SwaggerUIConstructor from "#swagger-ui"

const { config } = SwaggerUIConstructor

const usePrevious = (value) => {
const ref = useRef()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}

const SwaggerUI = ({
spec = config.defaults.spec,
url = config.defaults.url,
Expand Down Expand Up @@ -40,6 +48,8 @@ const SwaggerUI = ({
}) => {
const [system, setSystem] = useState(null)
const SwaggerUIComponent = system?.getComponent("App", "root")
const prevSpec = usePrevious(spec)
const prevUrl = usePrevious(url)

useEffect(() => {
const systemInstance = SwaggerUIConstructor({
Expand Down Expand Up @@ -84,22 +94,30 @@ const SwaggerUI = ({
useEffect(() => {
if (system) {
const prevStateUrl = system.specSelectors.url()
if (url !== prevStateUrl) {
if (url !== prevStateUrl || url !== prevUrl) {
system.specActions.updateSpec("")
if (url) {
system.specActions.updateUrl(url)
system.specActions.download(url)
}
}
}
}, [system, url])

useEffect(() => {
if (system) {
const prevStateSpec = system.specSelectors.specStr()
if (spec && spec !== prevStateSpec) {
if (
spec &&
spec !== SwaggerUIConstructor.config.defaults.spec &&
(spec !== prevStateSpec || spec !== prevSpec)
) {
const updatedSpec =
typeof spec === "object" ? JSON.stringify(spec) : spec
system.specActions.updateSpec(updatedSpec)
}
}
}, [system, url, spec])
}, [system, spec])

return SwaggerUIComponent ? <SwaggerUIComponent /> : null
}
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"open": "^10.1.0",
"postcss": "^8.4.38",
"postcss-loader": "^8.1.1",
"postcss-preset-env": "^9.5.13",
"postcss-preset-env": "^9.5.14",
"prettier": "^3.2.5",
"process": "^0.11.10",
"react-refresh": "^0.14.2",
Expand Down

0 comments on commit e5e87db

Please sign in to comment.