mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
* chore: Prune redundant comments and normalize lint directives Removes comments that restate the following line and stale TODOs, drops the passive event listener feature check now that all supported browsers support it, and renames eslint directives to oxlint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * fix: Evaluate restricted team domains at validation time The restricted email provider list was captured when the model loaded, so it could not follow the hosting environment and the tests that covered it never reached the model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * chore: Replace type suppressions with accurate types Adds a Concrete helper type for registries of abstract classes, declares missing Sequelize and refractor typings, uses public ProseMirror APIs, a typed mock context helper in middleware tests, and a StaticLink component for sidebar rows being edited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH --------- Co-authored-by: Claude <noreply@anthropic.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { useState, useCallback, useEffect } from "react";
|
|
|
|
type RequestResponse<T> = {
|
|
/** The return value of the request function. */
|
|
data: T | undefined;
|
|
/** The request error, if any. */
|
|
error: unknown;
|
|
/** Whether the request is currently in progress. */
|
|
loading: boolean;
|
|
/** Whether the request has completed - useful to check if the request has completed at least once. */
|
|
loaded: boolean;
|
|
/** Function to start the request. */
|
|
request: () => Promise<T | undefined>;
|
|
};
|
|
|
|
/**
|
|
* A hook to make an API request and track its state within a component.
|
|
*
|
|
* @param requestFn The function to call to make the request, it should return a promise.
|
|
* @param makeRequestOnMount Whether to make the request when the component mounts.
|
|
* @returns An object containing the request state and a function to start the request.
|
|
*/
|
|
export default function useRequest<T = unknown>(
|
|
requestFn: () => Promise<T>,
|
|
makeRequestOnMount = false
|
|
): RequestResponse<T> {
|
|
const [data, setData] = useState<T>();
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
const [loaded, setLoaded] = useState<boolean>(false);
|
|
const [error, setError] = useState<unknown>();
|
|
|
|
const request = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const response = await requestFn();
|
|
setData(response);
|
|
setError(undefined);
|
|
setLoaded(true);
|
|
return response;
|
|
} catch (err) {
|
|
setError(err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
|
|
return undefined;
|
|
}, [requestFn]);
|
|
|
|
useEffect(() => {
|
|
if (makeRequestOnMount) {
|
|
void request();
|
|
}
|
|
// Only ever request on mount, later changes to the request function are
|
|
// surfaced through the returned `request` for the caller to invoke.
|
|
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return { data, loading, loaded, error, request };
|
|
}
|