Adding additional possible field types.

This commit is contained in:
Brice Johnson 2023-05-11 10:36:33 -06:00
parent e48b7c48b0
commit 04d047653e
3 changed files with 73 additions and 33 deletions

View File

@ -596,10 +596,15 @@
}, },
"gitea": { "gitea": {
"notifications": "Notifications", "notifications": "Notifications",
"Issue": "Issues", "issue": "Issues",
"Pull": "Pull Requests", "pull": "Pull Requests",
"Commit": "Commits", "commit": "Commits",
"Repository": "Repositories" "repository": "Repositories",
"followers": "Followers",
"following": "Following",
"repos": "Repositories",
"forks": "Stars",
"stars": "Forks"
}, },
"audiobookshelf": { "audiobookshelf": {
"podcasts": "Podcasts", "podcasts": "Podcasts",

View File

@ -5,41 +5,73 @@ import Container from "components/services/widget/container";
import useWidgetAPI from "utils/proxy/use-widget-api"; import useWidgetAPI from "utils/proxy/use-widget-api";
function hasField(fields, fieldTypes) {
return fields.some(field => fieldTypes.includes(field));
}
export default function Component({ service }) {
export default function Component ({ service }) {
const { t } = useTranslation(); const { t } = useTranslation();
let data = {};
const { widget } = service; const { widget } = service;
const fields = widget.fields ?? ["repos", "followers", "notifications"];
const { data: notifications, error: giteaError } = useWidgetAPI(widget, "allNotifications"); // Different fields require different API calls
const notificationTypes = (notifications ?? []).reduce((acc, notification) => { const notificationFields = ["notifications", "issue", "pull", "commit", "repository"];
acc[notification.subject.type].push(notification); const userFields = ["followers", "following"];
return acc; const repoFields = ["repos", "stars", "forks"];
}
, { if (hasField(fields, userFields)) {
"Issue": [], // eslint-disable-next-line react-hooks/rules-of-hooks
"Pull": [], const { data: user, error: userError } = useWidgetAPI(widget, "user");
"Commit": [], if (userError) {
"Repository": [] return <Container service={service} error={userError} />;
} }
); data = { ...data, followers: user?.followers_count, following: user?.following_count };
if (giteaError) {
return <Container service={service} error={giteaError} />;
} }
if (!notifications) return ( if (hasField(fields, repoFields)) {
<Container service={service}> // eslint-disable-next-line react-hooks/rules-of-hooks
<Block label="gitea.notifications" /> const { data: repos, error: repoError } = useWidgetAPI(widget, "repos");
</Container> if (repoError) {
); return <Container service={service} error={repoError} />;
}
const repoStats = (repos ?? []).reduce((acc, repo) => {
acc.repos += 1;
acc.stars += repo.stars_count;
acc.forks += repo.forks_count;
return acc;
}, { repos: 0, stars: 0, forks: 0 });
data = { ...data, ...repoStats };
}
if (hasField(fields, notificationFields)) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: notifications, error: notificationError } = useWidgetAPI(widget, "notifications");
if (notificationError) {
return <Container service={service} error={notificationError} />;
}
const notificationTypes = (notifications ?? []).reduce((acc, notification) => {
acc[notification.subject.type.toLowerCase()] += 1;
acc.notifications += 1;
return acc;
}, {
"notifications": 0, "issue": 0, "pull": 0, "commit": 0, "repository": 0
}
);
data = { ...data, ...notificationTypes };
}
return ( return (
<Container service={service}> <Container service={service}>
{Object.keys(notificationTypes).map((type) => {fields.map((field) =>
<Block key={type} label={`gitea.${type}`} <Block key={field} label={`gitea.${field}`} value={data[field] ? t("common.number", { value: data[field] }) : '-'} />
value={t("common.number", { value: notificationTypes[type].length })} />
)} )}
</Container> </Container>
); );

View File

@ -1,16 +1,19 @@
import genericProxyHandler from "utils/proxy/handlers/generic"; import genericProxyHandler from "utils/proxy/handlers/generic";
const widget = { const widget = {
api: "{url}/api/v1/{endpoint}?access_token={key}", api: "{url}/api/{endpoint}?access_token={key}",
proxyHandler: genericProxyHandler, proxyHandler: genericProxyHandler,
mappings: { mappings: {
"allNotifications": { "notifications": {
endpoint: "notifications", endpoint: "v1/notifications", // required scope: notification
}, },
"newNotifications": { "user": {
endpoint: "notifications/new", endpoint: "v1/user",
}, },
"repos": {
endpoint: "v1/user/repos", // required scope: repo
}
}, },
}; };