Adding additional possible field types.
This commit is contained in:
parent
e48b7c48b0
commit
04d047653e
@ -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",
|
||||||
|
|||||||
@ -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"];
|
||||||
|
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 (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;
|
return acc;
|
||||||
|
}, { repos: 0, stars: 0, forks: 0 });
|
||||||
|
|
||||||
|
data = { ...data, ...repoStats };
|
||||||
}
|
}
|
||||||
, {
|
|
||||||
"Issue": [],
|
if (hasField(fields, notificationFields)) {
|
||||||
"Pull": [],
|
|
||||||
"Commit": [],
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||||
"Repository": []
|
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
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (giteaError) {
|
data = { ...data, ...notificationTypes };
|
||||||
return <Container service={service} error={giteaError} />;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!notifications) return (
|
|
||||||
<Container service={service}>
|
|
||||||
<Block label="gitea.notifications" />
|
|
||||||
</Container>
|
|
||||||
);
|
|
||||||
|
|
||||||
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>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -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
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user