Skip to content

Commit

Permalink
import snippet dialog initial, somewhat working
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 12, 2024
1 parent f0a2378 commit ba979af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/components/CodeAndPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useGlobalStore } from "@/hooks/use-global-store"
import { useUrlParams } from "@/hooks/use-url-params"
import { getSnippetTemplate } from "@/lib/get-snippet-template"
import "@/prettier"
import { useImportSnippetDialog } from "./dialogs/import-snippet-dialog"

interface Props {
snippet?: Snippet | null
Expand Down Expand Up @@ -70,6 +71,8 @@ export function CodeAndPreview({ snippet }: Props) {
type: snippetType,
})
const qc = useQueryClient()
const { Dialog: ImportSnippetDialog, openDialog: openImportDialog } =
useImportSnippetDialog()

const updateSnippetMutation = useMutation({
mutationFn: async () => {
Expand Down Expand Up @@ -138,7 +141,7 @@ export function CodeAndPreview({ snippet }: Props) {
<Button
size="sm"
variant="ghost"
// onClick={() => openImportDialog()}
onClick={() => openImportDialog()}
>
Import
</Button>
Expand Down Expand Up @@ -185,6 +188,7 @@ export function CodeAndPreview({ snippet }: Props) {
/>
)}
</div>
<ImportSnippetDialog />
</div>
)
}
28 changes: 28 additions & 0 deletions src/components/dialogs/create-use-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FunctionComponent, useState } from "react"

export const createUseDialog = <DialogType extends React.ComponentType<any>>(
DialogComponent: DialogType,
) => {
return () => {
const [open, setOpen] = useState(false)

return {
openDialog: () => {
setOpen(true)
},
closeDialog: () => {
setOpen(false)
},
Dialog: (
props: Omit<React.ComponentProps<DialogType>, "open" | "onOpenChange">,
) => (
<DialogComponent
{...(props as any)}
open={open}
onOpenChange={setOpen}
/>
),
open,
}
}
}
5 changes: 4 additions & 1 deletion src/components/dialogs/import-snippet-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { Button } from "../ui/button"
import { useState } from "react"
import { useQuery } from "react-query"
import { useAxios } from "@/hooks/use-axios"
import { createUseDialog } from "./create-use-dialog"

export const ImportSnippetDialog = ({
open,
onOpenChange,
onSnippetSelected,
}: {
open: boolean
onOpenChange: (open: boolean) => void
onOpenChange: (open: boolean) => any
onSnippetSelected: (snippet: Snippet) => any
}) => {
const [searchText, setSearchText] = useState("")
Expand Down Expand Up @@ -56,3 +57,5 @@ export const ImportSnippetDialog = ({
</Dialog>
)
}

export const useImportSnippetDialog = createUseDialog(ImportSnippetDialog)

0 comments on commit ba979af

Please sign in to comment.