|
| 1 | +<template> |
| 2 | + <Dialog :open="open" @update:open="$emit('update:open', $event)"> |
| 3 | + <DialogContent> |
| 4 | + <DialogHeader> |
| 5 | + <DialogTitle>Create New File</DialogTitle> |
| 6 | + <DialogDescription> Enter a name for the new file </DialogDescription> |
| 7 | + </DialogHeader> |
| 8 | + |
| 9 | + <div class="space-y-4"> |
| 10 | + <div class="space-y-2"> |
| 11 | + <Label for="file-name">File Name</Label> |
| 12 | + <Input |
| 13 | + id="file-name" |
| 14 | + v-model="fileName" |
| 15 | + placeholder="my-file.txt" |
| 16 | + @keyup.enter="handleCreate" |
| 17 | + /> |
| 18 | + </div> |
| 19 | + |
| 20 | + <Alert v-if="localError" variant="destructive"> |
| 21 | + <AlertTriangle class="w-4 h-4" /> |
| 22 | + <AlertDescription>{{ localError }}</AlertDescription> |
| 23 | + </Alert> |
| 24 | + </div> |
| 25 | + |
| 26 | + <DialogFooter> |
| 27 | + <Button variant="outline" @click="handleCancel"> Cancel </Button> |
| 28 | + <Button @click="handleCreate" :disabled="!fileName || isCreating"> |
| 29 | + {{ isCreating ? "Creating..." : "Create" }} |
| 30 | + </Button> |
| 31 | + </DialogFooter> |
| 32 | + </DialogContent> |
| 33 | + </Dialog> |
| 34 | +</template> |
| 35 | + |
| 36 | +<script setup lang="ts"> |
| 37 | +import { ref, watch } from "vue"; |
| 38 | +import { Button } from "@/components/ui/button"; |
| 39 | +import { Input } from "@/components/ui/input"; |
| 40 | +import { Label } from "@/components/ui/label"; |
| 41 | +import { Alert, AlertDescription } from "@/components/ui/alert"; |
| 42 | +import { AlertTriangle } from "lucide-vue-next"; |
| 43 | +import { |
| 44 | + Dialog, |
| 45 | + DialogContent, |
| 46 | + DialogDescription, |
| 47 | + DialogFooter, |
| 48 | + DialogHeader, |
| 49 | + DialogTitle, |
| 50 | +} from "@/components/ui/dialog"; |
| 51 | +
|
| 52 | +const props = defineProps<{ |
| 53 | + open: boolean; |
| 54 | + parentPath?: string; |
| 55 | +}>(); |
| 56 | +
|
| 57 | +const emit = defineEmits<{ |
| 58 | + "update:open": [value: boolean]; |
| 59 | +}>(); |
| 60 | +
|
| 61 | +const store = useFileManagerStore(); |
| 62 | +const fileName = ref(""); |
| 63 | +const localError = ref<string | null>(null); |
| 64 | +const isCreating = ref(false); |
| 65 | +
|
| 66 | +watch( |
| 67 | + () => props.open, |
| 68 | + (isOpen) => { |
| 69 | + if (!isOpen) { |
| 70 | + fileName.value = ""; |
| 71 | + localError.value = null; |
| 72 | + } |
| 73 | + }, |
| 74 | +); |
| 75 | +
|
| 76 | +async function handleCreate() { |
| 77 | + if (!fileName.value) return; |
| 78 | +
|
| 79 | + isCreating.value = true; |
| 80 | + localError.value = null; |
| 81 | +
|
| 82 | + try { |
| 83 | + // Build full path using parentPath if provided |
| 84 | + const filePath = props.parentPath |
| 85 | + ? `${props.parentPath}/${fileName.value}` |
| 86 | + : fileName.value; |
| 87 | +
|
| 88 | + const success = await store.saveFile(filePath, ""); |
| 89 | + if (success) { |
| 90 | + emit("update:open", false); |
| 91 | + } else { |
| 92 | + localError.value = store.error || "Failed to create file"; |
| 93 | + } |
| 94 | + } catch (error: any) { |
| 95 | + localError.value = error.message || "Failed to create file"; |
| 96 | + } finally { |
| 97 | + isCreating.value = false; |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +function handleCancel() { |
| 102 | + emit("update:open", false); |
| 103 | +} |
| 104 | +</script> |
0 commit comments