ui/hooks: add overloaded useClanUri
This commit is contained in:
@@ -28,18 +28,30 @@ export const clanURIParam = (params: Params) => {
|
||||
return window.atob(params.clanURI);
|
||||
};
|
||||
|
||||
export const useMaybeClanURI = () => {
|
||||
export function useClanURI(opts: { force: true }): string;
|
||||
export function useClanURI(opts: { force: boolean }): string | null;
|
||||
export function useClanURI(
|
||||
opts: { force: boolean } = { force: false },
|
||||
): string | null {
|
||||
const maybe = () => {
|
||||
const params = useParams();
|
||||
|
||||
if (!params.clanURI) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const clanURI = clanURIParam(params);
|
||||
|
||||
if (!clanURI) {
|
||||
throw new Error("Could not decode clan URI from params: " + params.clanURI);
|
||||
throw new Error(
|
||||
"Could not decode clan URI from params: " + params.clanURI,
|
||||
);
|
||||
}
|
||||
|
||||
return clanURI;
|
||||
};
|
||||
|
||||
const uri = maybe();
|
||||
if (!uri && opts.force) {
|
||||
throw new Error(
|
||||
"ClanURI is not set. Use this function only within contexts, where clanURI is guaranteed to have been set.",
|
||||
);
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user