From 0a5970a2137e577922406236892c9470258b653d Mon Sep 17 00:00:00 2001
From: Ansh Grover <168731971+Anshgrover23@users.noreply.github.com>
Date: Sun, 29 Dec 2024 04:05:37 +0530
Subject: [PATCH] add comments (#434)
---
src/components/EditorNav.tsx | 34 ++++++++++++++++++---
src/components/ViewSnippetHeader.tsx | 7 +++--
src/hooks/useForkSnippetMutation.ts | 45 ++++++++++++++++++++++++++++
3 files changed, 80 insertions(+), 6 deletions(-)
create mode 100644 src/hooks/useForkSnippetMutation.ts
diff --git a/src/components/EditorNav.tsx b/src/components/EditorNav.tsx
index 9c1b7289..94cbdd28 100644
--- a/src/components/EditorNav.tsx
+++ b/src/components/EditorNav.tsx
@@ -1,4 +1,5 @@
import { Button } from "@/components/ui/button"
+import { GitFork } from "lucide-react"
import {
DropdownMenu,
DropdownMenuContent,
@@ -33,7 +34,7 @@ import {
Trash2,
} from "lucide-react"
import { useEffect, useState } from "react"
-import { useQueryClient } from "react-query"
+import { useMutation, useQueryClient } from "react-query"
import { Link, useLocation } from "wouter"
import { useAxios } from "../hooks/use-axios"
import { useToast } from "../hooks/use-toast"
@@ -46,6 +47,7 @@ import { DownloadButtonAndMenu } from "./DownloadButtonAndMenu"
import { SnippetLink } from "./SnippetLink"
import { TypeBadge } from "./TypeBadge"
import { useUpdateDescriptionDialog } from "./dialogs/edit-description-dialog"
+import { useForkSnippetMutation } from "@/hooks/useForkSnippetMutation"
export default function EditorNav({
circuitJson,
@@ -72,6 +74,7 @@ export default function EditorNav({
}) {
const [, navigate] = useLocation()
const isLoggedIn = useGlobalStore((s) => Boolean(s.session))
+ const session = useGlobalStore((s) => s.session)
const { Dialog: RenameDialog, openDialog: openRenameDialog } =
useRenameSnippetDialog()
const {
@@ -94,6 +97,16 @@ export default function EditorNav({
const { toast } = useToast()
const qc = useQueryClient()
+ const { mutate: forkSnippet, isLoading: isForking } = useForkSnippetMutation({
+ snippet: snippet!,
+ onSuccess: (forkedSnippet) => {
+ navigate("/editor?snippet_id=" + forkedSnippet.snippet_id)
+ setTimeout(() => {
+ window.location.reload() //reload the page
+ }, 2000)
+ },
+ })
+
// Update currentType when snippet or snippetType changes
useEffect(() => {
setCurrentType(snippetType ?? snippet?.snippet_type)
@@ -178,10 +191,23 @@ export default function EditorNav({
size="sm"
className={"h-6 px-2 text-xs save-button"}
disabled={!isLoggedIn || !canSave}
- onClick={onSave}
+ onClick={
+ snippet?.owner_name === session?.github_username
+ ? onSave
+ : () => forkSnippet()
+ }
>
-