Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: dashboard #422

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions api/src/resolver_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ async function ensurePodEditAccess({ id, userId }) {
}
}

async function myRepos(_, __, { userId }) {
async function getDashboardRepos(_, __, { userId }) {
if (!userId) throw Error("Unauthenticated");
const repos = await prisma.repo.findMany({
where: {
owner: {
id: userId,
},
OR: [
{
owner: {
id: userId,
},
},
{
collaborators: {
some: { id: userId },
},
},
],
},
include: {
UserRepoData: {
Expand All @@ -58,46 +67,17 @@ async function myRepos(_, __, { userId }) {
stargazers: true,
},
});
// Sort by last access time.
repos.sort((a, b) => {
if (a.UserRepoData.length > 0) {
if (b.UserRepoData.length > 0) {
return (
b.UserRepoData[0].accessedAt.valueOf() -
a.UserRepoData[0].accessedAt.valueOf()
);
}
return -1;
}
return a.updatedAt.valueOf() - b.updatedAt.valueOf();
});
// Re-use updatedAt field (this is actually the lastviewed field).
return repos.map((repo) => {
return {
...repo,
updatedAt:
accessedAt:
repo.UserRepoData.length > 0
? repo.UserRepoData[0].accessedAt
: repo.updatedAt,
};
});
}

async function myCollabRepos(_, __, { userId }) {
if (!userId) throw Error("Unauthenticated");
const repos = await prisma.repo.findMany({
where: {
collaborators: {
some: { id: userId },
},
},
include: {
stargazers: true,
},
});
return repos;
}

async function updateUserRepoData({ userId, repoId }) {
// FIXME I should probably rename this from query to mutation?
//
Expand Down Expand Up @@ -609,9 +589,8 @@ async function copyRepo(_, { repoId }, { userId }) {

export default {
Query: {
myRepos,
repo,
myCollabRepos,
getDashboardRepos,
getVisibility,
},
Mutation: {
Expand Down
4 changes: 2 additions & 2 deletions api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const typeDefs = gql`
public: Boolean
createdAt: String
updatedAt: String
accessedAt: String
}

type Edge {
Expand Down Expand Up @@ -105,11 +106,10 @@ export const typeDefs = gql`
repos: [Repo]
repo(id: String!): Repo
pod(id: ID!): Pod
myRepos: [Repo]
getDashboardRepos: [Repo]
activeSessions: [String]
getVisibility(repoId: String!): Visibility
listAllRuntimes: [RuntimeInfo]
myCollabRepos: [Repo]
infoRuntime(sessionId: String!): RuntimeInfo
}

Expand Down
Loading