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": {
"notifications": "Notifications",
"Issue": "Issues",
"Pull": "Pull Requests",
"Commit": "Commits",
"Repository": "Repositories"
"issue": "Issues",
"pull": "Pull Requests",
"commit": "Commits",
"repository": "Repositories",
"followers": "Followers",
"following": "Following",
"repos": "Repositories",
"forks": "Stars",
"stars": "Forks"
},
"audiobookshelf": {
"podcasts": "Podcasts",

View File

@ -5,41 +5,73 @@ import Container from "components/services/widget/container";
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();
let data = {};
const { widget } = service;
const fields = widget.fields ?? ["repos", "followers", "notifications"];
const { data: notifications, error: giteaError } = useWidgetAPI(widget, "allNotifications");
const notificationTypes = (notifications ?? []).reduce((acc, notification) => {
acc[notification.subject.type].push(notification);
return acc;
}
, {
"Issue": [],
"Pull": [],
"Commit": [],
"Repository": []
}
);
// Different fields require different API calls
const notificationFields = ["notifications", "issue", "pull", "commit", "repository"];
const userFields = ["followers", "following"];
const repoFields = ["repos", "stars", "forks"];
if (hasField(fields, userFields)) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: user, error: userError } = useWidgetAPI(widget, "user");
if (userError) {
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 (
<Container service={service}>
<Block label="gitea.notifications" />
</Container>
);
if (hasField(fields, repoFields)) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data: repos, error: repoError } = useWidgetAPI(widget, "repos");
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 (
<Container service={service}>
{Object.keys(notificationTypes).map((type) =>
<Block key={type} label={`gitea.${type}`}
value={t("common.number", { value: notificationTypes[type].length })} />
{fields.map((field) =>
<Block key={field} label={`gitea.${field}`} value={data[field] ? t("common.number", { value: data[field] }) : '-'} />
)}
</Container>
);

View File

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