mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-03 18:16:07 +00:00
f48dc847ff
Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
104 lines
2.9 KiB
TypeScript
104 lines
2.9 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { httpClient } from '@/app/infra/http/HttpClient';
|
|
import { useCurrentWorkspace } from '@/app/infra/http';
|
|
|
|
type AuthenticatedResourceState = {
|
|
key: string;
|
|
url: string;
|
|
error: boolean;
|
|
};
|
|
|
|
const EMPTY_RESOURCE: AuthenticatedResourceState = {
|
|
key: '',
|
|
url: '',
|
|
error: false,
|
|
};
|
|
|
|
export function useAuthenticatedPluginIcon(
|
|
author: string,
|
|
name: string,
|
|
enabled = true,
|
|
): { url: string; error: boolean } {
|
|
const [resource, setResource] =
|
|
useState<AuthenticatedResourceState>(EMPTY_RESOURCE);
|
|
const currentWorkspace = useCurrentWorkspace();
|
|
const workspaceUuid = currentWorkspace?.workspace.uuid;
|
|
const resourceKey = `${workspaceUuid ?? ''}:${author}/${name}`;
|
|
|
|
useEffect(() => {
|
|
if (!enabled) {
|
|
setResource({ key: resourceKey, url: '', error: false });
|
|
return;
|
|
}
|
|
let active = true;
|
|
let objectURL = '';
|
|
setResource({ key: resourceKey, url: '', error: false });
|
|
httpClient
|
|
.getAuthenticatedPluginIconURL(author, name)
|
|
.then((nextURL) => {
|
|
objectURL = nextURL;
|
|
if (active) {
|
|
setResource({ key: resourceKey, url: nextURL, error: false });
|
|
} else {
|
|
URL.revokeObjectURL(nextURL);
|
|
}
|
|
})
|
|
.catch(() => {
|
|
if (active) {
|
|
setResource({ key: resourceKey, url: '', error: true });
|
|
}
|
|
});
|
|
return () => {
|
|
active = false;
|
|
if (objectURL) URL.revokeObjectURL(objectURL);
|
|
};
|
|
}, [author, enabled, name, resourceKey]);
|
|
|
|
return {
|
|
url: resource.key === resourceKey ? resource.url : '',
|
|
error: resource.key === resourceKey && resource.error,
|
|
};
|
|
}
|
|
|
|
export function useAuthenticatedPluginAsset(
|
|
author: string,
|
|
name: string,
|
|
filepath: string,
|
|
): { url: string; error: boolean } {
|
|
const [resource, setResource] =
|
|
useState<AuthenticatedResourceState>(EMPTY_RESOURCE);
|
|
const currentWorkspace = useCurrentWorkspace();
|
|
const workspaceUuid = currentWorkspace?.workspace.uuid;
|
|
const resourceKey = `${workspaceUuid ?? ''}:${author}/${name}/${filepath}`;
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
let objectURL = '';
|
|
setResource({ key: resourceKey, url: '', error: false });
|
|
httpClient
|
|
.getAuthenticatedPluginAssetURL(author, name, filepath)
|
|
.then((nextURL) => {
|
|
objectURL = nextURL;
|
|
if (active) {
|
|
setResource({ key: resourceKey, url: nextURL, error: false });
|
|
} else {
|
|
URL.revokeObjectURL(nextURL);
|
|
}
|
|
})
|
|
.catch(() => {
|
|
if (active) {
|
|
setResource({ key: resourceKey, url: '', error: true });
|
|
}
|
|
});
|
|
return () => {
|
|
active = false;
|
|
if (objectURL) URL.revokeObjectURL(objectURL);
|
|
};
|
|
}, [author, name, filepath, resourceKey]);
|
|
|
|
return {
|
|
url: resource.key === resourceKey ? resource.url : '',
|
|
error: resource.key === resourceKey && resource.error,
|
|
};
|
|
}
|