Skip to content

[UI] Support repo snapshots #462

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "YDocSnapshot" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"message" TEXT,
"yDocBlob" BYTEA NOT NULL,
"repoId" TEXT NOT NULL,

CONSTRAINT "YDocSnapshot_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "YDocSnapshot" ADD CONSTRAINT "YDocSnapshot_repoId_fkey" FOREIGN KEY ("repoId") REFERENCES "Repo"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
11 changes: 11 additions & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ model UserRepoData {
@@id([userId, repoId])
}

model YDocSnapshot {
id String @id
createdAt DateTime @default(now())
message String?
yDocBlob Bytes
repo Repo @relation(fields: [repoId], references: [id])
repoId String
}

model Repo {
id String @id
name String?
Expand All @@ -83,6 +92,8 @@ model Repo {
UserRepoData UserRepoData[]
stargazers User[] @relation("STAR")
yDocBlob Bytes?
yDocSnapshots YDocSnapshot[]

}

enum PodType {
Expand Down
41 changes: 41 additions & 0 deletions api/src/resolver_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,50 @@ async function copyRepo(_, { repoId }, { userId }) {
return id;
}

/**
* Create yDoc snapshot upon request.
*/
async function addRepoSnapshot(_, { repoId, message }) {
const repo = await prisma.repo.findFirst({
where: { id: repoId },
include: {
owner: true,
collaborators: true,
},
});
if (!repo) throw Error("Repo not exists.");
if (!repo.yDocBlob) throw Error(`yDocBlob on ${repoId} not found`);
const snapshot = await prisma.yDocSnapshot.create({
data: {
id: await nanoid(),
yDocBlob: repo.yDocBlob,
message: message,
repo: { connect: { id: repoId } },
},
});
return snapshot.id;
}

/**
* Fetch yDoc snapshots for a repo.
*/
async function getRepoSnapshots(_, { repoId }) {
const snapshots = await prisma.yDocSnapshot.findMany({
where: { repo: { id: repoId } },
});
if (!snapshots) throw Error(`No snapshot exists for repo ${repoId}.`);

return snapshots.map((snapshot) => ({
...snapshot,
yDocBlob: JSON.stringify(snapshot.yDocBlob),
}));
}

export default {
Query: {
repo,
getDashboardRepos,
getRepoSnapshots,
},
Mutation: {
createRepo,
Expand All @@ -531,6 +571,7 @@ export default {
addCollaborator,
updateVisibility,
deleteCollaborator,
addRepoSnapshot,
star,
unstar,
},
Expand Down
8 changes: 8 additions & 0 deletions api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export const typeDefs = gql`
ttl: Int
}

type YDocSnapshot {
id: String
createdAt: String
message: String
}

input RunSpecInput {
code: String
podId: String
Expand All @@ -107,6 +113,7 @@ export const typeDefs = gql`
repo(id: String!): Repo
pod(id: ID!): Pod
getDashboardRepos: [Repo]
getRepoSnapshots(repoId: String!): [YDocSnapshot]
activeSessions: [String]
listAllRuntimes: [RuntimeInfo]
infoRuntime(sessionId: String!): RuntimeInfo
Expand Down Expand Up @@ -142,6 +149,7 @@ export const typeDefs = gql`

exportJSON(repoId: String!): String!
exportFile(repoId: String!): String!
addRepoSnapshot(repoId: String!, message: String!): String!
updateCodeiumAPIKey(apiKey: String!): Boolean

connectRuntime(runtimeId: String, repoId: String): Boolean
Expand Down
103 changes: 103 additions & 0 deletions ui/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemText from "@mui/material/ListItemText";
import ListItemButton from "@mui/material/ListItemButton";
import AddIcon from "@mui/icons-material/Add";
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import RestartAltIcon from "@mui/icons-material/RestartAlt";
Expand Down Expand Up @@ -1337,6 +1338,105 @@ function TableofPods() {
);
}

function SnapshotItem({ id, message }) {
const [anchorEl, setAnchorEl] = useState(null);

const open = Boolean(anchorEl);

const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};
return (
<ListItem disablePadding key={id}>
<ListItemText primary={message} />
<IconButton aria-label="More options" onClick={handleClick}>
<MoreVertIcon />
</IconButton>
<Popover open={open} anchorEl={anchorEl} onClose={handleClose}>
<MenuList>
<MenuItem>Restore</MenuItem>
<MenuItem>Delete</MenuItem>
</MenuList>
</Popover>
</ListItem>
);
}

function RepoSnapshots() {
const { id: repoId } = useParams();
const { error: queryError, data: queryResult } = useQuery(gql`
query GetRepoSnapshots {
getRepoSnapshots(repoId: "${repoId}") {
id
createdAt
message
}
}
`);
const [addRepoSnapshot, { error: mutationError, data: mutationResult }] =
useMutation(
gql`
mutation addRepoSnapshot($repoId: String!, $message: String!) {
addRepoSnapshot(repoId: $repoId, message: $message)
}
`,
{
refetchQueries: ["GetRepoSnapshots"],
}
);

if (queryError) {
return <Box>ERROR: {queryError.message}</Box>;
}

const snapshots = queryResult?.getRepoSnapshots.slice();
return (
<Box>
<Box
style={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
alignItems: "stretch",
}}
>
<Box
style={{
display: "flex",
alignItems: "center",
}}
>
<Typography variant="h6">Snapshots</Typography>
</Box>
<IconButton
onClick={() => {
addRepoSnapshot({
variables: { repoId: repoId, message: "placeholder" },
});
}}
>
<AddIcon />
</IconButton>
</Box>
<List>
{snapshots &&
snapshots.length > 0 &&
snapshots.map((snapshot) => (
<SnapshotItem
key={snapshot.id}
id={snapshot.id}
message={snapshot.createdAt}
/>
))}
</List>
</Box>
);
}

export const Sidebar: React.FC<SidebarProps> = ({
width,
open,
Expand Down Expand Up @@ -1422,6 +1522,9 @@ export const Sidebar: React.FC<SidebarProps> = ({
<Divider />
<Typography variant="h6">Table of Pods</Typography>
<TableofPods />

<Divider />
<RepoSnapshots />
</Stack>
</Box>
</Drawer>
Expand Down