Skip to content

Commit 85ccfd2

Browse files
authored
add the fork functionality (#789)
* add the fork functionality * format
1 parent 3db9410 commit 85ccfd2

4 files changed

Lines changed: 32 additions & 13 deletions

File tree

fake-snippets-api/routes/api/package_files/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default withRouteSpec(routeSpec)(async (req, ctx) => {
4141
.filter((file) => file.package_release_id === packageReleaseId)
4242
.map((file) => ({
4343
...file,
44-
content_text: undefined,
44+
content_text: file.content_text ?? undefined,
4545
}))
4646

4747
return ctx.json({

src/components/ViewPackagePage/components/package-header.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { TypeBadge } from "@/components/TypeBadge"
22
import { Button } from "@/components/ui/button"
33
import { Skeleton } from "@/components/ui/skeleton"
4+
import { useForkPackageMutation } from "@/hooks/use-fork-package-mutation"
45
import {
56
usePackageStarMutationByName,
67
usePackageStarsByName,
78
} from "@/hooks/use-package-stars"
89
import { LockClosedIcon } from "@radix-ui/react-icons"
910
import { GitFork, Star } from "lucide-react"
11+
import { useToast } from "@/hooks/use-toast"
1012
import { Link } from "wouter"
1113

1214
interface PackageInfo {
@@ -18,42 +20,53 @@ interface PackageInfo {
1820
ai_description: string
1921
creator_account_id?: string
2022
owner_org_id?: string
23+
package_id: string
2124
}
2225

2326
interface PackageHeaderProps {
2427
packageInfo?: PackageInfo
2528
isPrivate?: boolean
26-
onForkClick?: () => void
2729
isCurrentUserAuthor?: boolean
2830
}
2931

3032
export default function PackageHeader({
3133
packageInfo,
3234
isPrivate = false,
33-
onForkClick,
3435
isCurrentUserAuthor = false,
3536
}: PackageHeaderProps) {
3637
const author = packageInfo?.owner_github_username
3738
const packageName = packageInfo?.unscoped_name
3839

39-
// Use the star hooks
40+
const { toast } = useToast()
41+
4042
const { data: starData, isLoading: isStarDataLoading } =
4143
usePackageStarsByName(packageInfo?.name ?? null)
4244
const { addStar, removeStar } = usePackageStarMutationByName(
4345
packageInfo?.name ?? "",
4446
)
4547

48+
const { mutateAsync: forkPackage, isLoading: isForkLoading } =
49+
useForkPackageMutation()
50+
4651
const handleStarClick = async () => {
4752
if (!packageInfo?.name) return
4853

49-
console.log("starData", starData)
5054
if (starData?.is_starred) {
5155
await removeStar.mutateAsync()
5256
} else {
5357
await addStar.mutateAsync()
5458
}
5559
}
5660

61+
const handleForkClick = async () => {
62+
if (!packageInfo?.package_id) return
63+
await forkPackage(packageInfo.package_id)
64+
toast({
65+
title: "Forked package",
66+
description: "Package forked successfully",
67+
})
68+
}
69+
5770
const isStarLoading =
5871
isStarDataLoading || addStar.isLoading || removeStar.isLoading
5972

@@ -111,11 +124,13 @@ export default function PackageHeader({
111124
<Button
112125
variant="outline"
113126
size="sm"
114-
onClick={onForkClick}
115-
disabled={!onForkClick}
127+
onClick={handleForkClick}
128+
disabled={
129+
isCurrentUserAuthor || isForkLoading || !packageInfo?.package_id
130+
}
116131
>
117132
<GitFork className="w-4 h-4 mr-2" />
118-
{isCurrentUserAuthor ? "Save" : "Fork"}
133+
Fork
119134
</Button>
120135
</div>
121136
</div>

src/components/ViewPackagePage/components/repo-page-content.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Header from "@/components/Header"
1818
import Footer from "@/components/Footer"
1919
import ViewSnippetHeader from "@/components/ViewSnippetHeader"
2020
import PackageHeader from "./package-header"
21+
import { useGlobalStore } from "@/hooks/use-global-store"
2122

2223
interface PackageFile {
2324
package_file_id: string
@@ -59,6 +60,7 @@ export default function RepoPageContent({
5960
onEditClicked,
6061
}: RepoPageContentProps) {
6162
const [activeView, setActiveView] = useState("files")
63+
const session = useGlobalStore((s) => s.session)
6264

6365
const importantFilePaths = packageFiles
6466
?.filter((pf) => isPackageFileImportant(pf.file_path))
@@ -163,7 +165,12 @@ export default function RepoPageContent({
163165
return (
164166
<div className="min-h-screen bg-white dark:bg-[#0d1117] text-gray-900 dark:text-[#c9d1d9] font-sans">
165167
<Header />
166-
<PackageHeader packageInfo={packageInfo} />
168+
<PackageHeader
169+
packageInfo={packageInfo}
170+
isCurrentUserAuthor={
171+
packageInfo?.creator_account_id === session?.github_username
172+
}
173+
/>
167174

168175
{/* Mobile Sidebar */}
169176
<div className="max-w-[1200px] mx-auto">

src/hooks/use-fork-package-mutation.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export const useForkPackageMutation = ({
3636
const sourcePackage: Package = packageData.package
3737
if (!sourcePackage) throw new Error("Source package not found")
3838

39-
console.log("sourcePackage", sourcePackage)
4039
// Step 2: Fetch latest release
4140
const { data: releaseData } = await axios.post("/package_releases/get", {
4241
package_release_id: sourcePackage.latest_package_release_id,
@@ -53,10 +52,8 @@ export const useForkPackageMutation = ({
5352

5453
// Step 4: Create new package
5554
const newPackage = await createPackage({
56-
name: `@${session.github_username}/${sourcePackage.unscoped_name}`,
55+
name: sourcePackage.unscoped_name,
5756
description: `Fork of ${sourcePackage.name}`,
58-
is_private: false,
59-
is_unlisted: false,
6057
})
6158

6259
// Step 5: Create new release

0 commit comments

Comments
 (0)