Skip to content

Commit

Permalink
fix: updating Javadoc link to grab the current webforJ version, or re…
Browse files Browse the repository at this point in the history
…vert to latest on failure
  • Loading branch information
MatthewHawkins committed Dec 4, 2024
1 parent 14f72b8 commit 22cb027
Showing 1 changed file with 87 additions and 43 deletions.
130 changes: 87 additions & 43 deletions src/components/DocsTools/JavadocLink.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/** @jsxImportSource @emotion/react */
import React, { useState, useEffect } from 'react'
import { css } from '@emotion/react';
import { Tooltip, Chip } from '@mui/material';
import AutoStoriesIcon from '@mui/icons-material/AutoStories';
import React, { useState, useEffect } from "react";
import { css } from "@emotion/react";
import { Tooltip, Chip } from "@mui/material";
import AutoStoriesIcon from "@mui/icons-material/AutoStories";

export default function JavadocLink( { type, location, top, children, code, suffix } ) {
const [url, setUrl] = useState('');
export default function JavadocLink({
type,
location,
top,
children,
code,
suffix,
}) {
const [url, setUrl] = useState("");
const [hasDocChips, setHasDocChips] = useState(false);

useEffect(() => {
// Check if there are any elements with the class 'doc-chip' on the page
const docChips = document.querySelectorAll('.doc-chip');
const docChips = document.querySelectorAll(".doc-chip");
if (docChips.length > 0) {
setHasDocChips(true);
} else {
Expand All @@ -19,26 +26,49 @@ export default function JavadocLink( { type, location, top, children, code, suff
}, []);

useEffect(() => {
if(!suffix){
suffix = ""
async function fetchLatestVersion() {
const resolvedSuffix = suffix || "";
try {
const response = await fetch(
"https://api.github.com/repos/webforj/webforj/releases/latest"
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}

const data = await response.json();
const latestVersion = data.tag_name;
console.log(latestVersion);

setUrl(
`https://javadoc.io/doc/com.webforj/webforj-${type}/${latestVersion}/${location}.html${resolvedSuffix}`
);
} catch (error) {
console.error("Failed to fetch the latest version:", error);
setUrl(
`https://javadoc.io/doc/com.webforj/webforj-${type}/latest/${location}.html${resolvedSuffix}`
);
}
}
setUrl("https://javadoc.io/doc/com.webforj/webforj-" + type + "/latest/" + location + ".html" + suffix)
}, []);
fetchLatestVersion();
}, [type, location, suffix]);

const mainStyles = css`
${top => top && css`
margin-bottom: 1em;
margin-left: 0.5em;
float: right;
@media (max-width: 500px){
margin-bottom: 1em;
float: none;
margin-left: -.25em;
}
`}
${(top) =>
top &&
css`
margin-bottom: 1em;
margin-left: 0.5em;
float: right;
@media (max-width: 500px) {
margin-bottom: 1em;
float: none;
margin-left: -0.25em;
}
`}
`;

// Styling for the div, if no DocChips are on the page
// Styling for the div, if no DocChips are on the page
const divStyles = css`
width: 100%;
margin-bottom: 1em;
Expand All @@ -53,41 +83,55 @@ export default function JavadocLink( { type, location, top, children, code, suff
:hover {
background-color: var(--javadoclink-hover-bg);
color: var(--javadoclink-hover-color);
}
}
`;

return (
<>
{
top === 'true' && hasDocChips && (
{top === "true" && hasDocChips && (
// If there are DocChips, don't wrap in a div
<Tooltip title="JavaDoc" arrow css={mainStyles}>
<Chip css={apiStyles} label='Java API' component="a" href={url} icon={<AutoStoriesIcon />} clickable={true} color='primary' target="_blank" />
<Chip
css={apiStyles}
label="Java API"
component="a"
href={url}
icon={<AutoStoriesIcon />}
clickable={true}
color="primary"
target="_blank"
/>
</Tooltip>
)
}
{
top === 'true' && !hasDocChips && (
)}
{top === "true" && !hasDocChips && (
// If there aren't DocChips, wrap in a div
<div css={divStyles}>
<Tooltip title="JavaDoc" arrow>
<Chip css={apiStyles} label='Java API' component="a" href={url} icon={<AutoStoriesIcon />} clickable={true} color='primary' target="_blank" />
<Chip
css={apiStyles}
label="Java API"
component="a"
href={url}
icon={<AutoStoriesIcon />}
clickable={true}
color="primary"
target="_blank"
/>
</Tooltip>
</div>
)
}
{
top !== 'true' && !code && (
<a href={url} target="_blank">{children}</a>
)
}
{
top !== 'true' && code && (
)}
{top !== "true" && !code && (
<a href={url} target="_blank">
{children}
</a>
)}
{top !== "true" && code && (
<code>
<a href={url} target="_blank">{children}</a>
<a href={url} target="_blank">
{children}
</a>
</code>
)
}
)}
</>
);
}

0 comments on commit 22cb027

Please sign in to comment.