Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stale use effect #13

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Changelog = () => {
<div>
<h1>{changelog.title}</h1>
<ul>
{changelog.entries.map(entry => (
{changelog.entries.map((entry) => (
<li key={entry.id}>{entry.version}</li>
))}
</ul>
Expand All @@ -53,7 +53,10 @@ const Changelog = () => {
### Component

```tsx
import { ChangelogContainer, MinimalChangelogList, } from '@wertarbyte/updatehive-react';
import {
ChangelogContainer,
MinimalChangelogList,
} from '@wertarbyte/updatehive-react';

return (
<ChangelogContainer
Expand Down Expand Up @@ -101,11 +104,9 @@ The library can be easily testet in dev mode via the provided 'dev' script and A
```
# npm
npm run dev

# yarn
yarn dev
```

### ChangelogLists
### ChangelogLists

ChangelogLists are split into public (API) classes and their internal representation, to
separate concerns and allow for easier reusage.
separate concerns and allow for easier reusage.
79 changes: 44 additions & 35 deletions lib/changelog.hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import {
Changelog,
UpdateHiveConfig,
Expand Down Expand Up @@ -27,47 +27,56 @@ export function useChangelogs(config: UpdateHiveConfig): UpdateHiveHookResult {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | undefined>();

const fetchData = async () => {
setIsLoading(true);
const requestURL = useMemo(() => buildRequestURL(config), [config]);
const apiKey = config.connection.API_KEY;

const requestURL = buildRequestURL(config);

try {
const result = await fetch(requestURL, {
headers: {
Authorization: `Bearer ${config.connection.API_KEY}`,
Accept: 'application/vnd.wertarbyte.changelog.v1+json',
},
});
useEffect(() => {
const ac = new AbortController();

if (!result.ok) {
const error = await result.json();
throw new Error(error.message);
}
void (async () => {
setIsLoading(true);

const resultData: Changelog[] | undefined = await result.json();
try {
const result = await fetch(requestURL, {
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: 'application/vnd.wertarbyte.changelog.v1+json',
},
signal: ac.signal,
});

if (resultData) {
setData(
resultData.sort((a, b) => Date.parse(b.releaseDate) - Date.parse(a.releaseDate)),
);
if (!result.ok) {
const error = await result.json();
throw new Error(error.message);
}
const resultData: Changelog[] | undefined = await result.json();
if (resultData) {
setData(
resultData.sort(
(a, b) => Date.parse(b.releaseDate) - Date.parse(a.releaseDate),
),
);
} else {
setError('Did not receive a changelog.');
}
} catch (error) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
return; // fetch aborted, ie. unmounted component or config changed
}
setError(error.message);
} else {
setError('An unknown error occurred.');
}
}
} catch (error) {
if (error instanceof Error) {
setError(error.message);
} else {
setError('An unknown error occurred.');
}
}

setIsLoading(false);
};
setIsLoading(false);
})();

useEffect(() => {
void fetchData();
// Explicitly set to empty array to avoid multiple requests.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return () => {
ac.abort();
};
}, [apiKey, requestURL]);

return {
loading: isLoading,
Expand Down
20 changes: 12 additions & 8 deletions lib/changelog.types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
/**
* Configuration to retrieve changelogs from UpdateHive.
*
* @param connection
* API_KEY: API_KEY to access UpdateHive public REST API.
* url: Override the default URL to UpdateHive API.
*
* @param changelogs
* product: Product ID to retrieve changelogs for.
* onlyLast: Retrieve only the last changelog.
*/
export type UpdateHiveConfig = {
connection: {
/**
* API_KEY to access UpdateHive public REST API.
*/
API_KEY: string;
/**
* Override the default URL to UpdateHive API.
*/
url?: string;
};
changelogs: {
/**
* Product ID to retrieve changelogs for.
*/
product: string;
/**
* Retrieve only the last changelog.
*/
onlyLast?: boolean;
};
};
Expand Down
Loading
Loading