refactor: integrate current user data across components
- Replaced instances of `authStore` with `currentUser` to streamline user authentication handling. - Updated permission checks and user-related data retrieval to utilize the new `useQuery` for better performance and clarity. - Cleaned up component structures and improved formatting for consistency and readability. - Enhanced error handling and user feedback mechanisms in various components to improve user experience.
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
<script lang="ts">
|
||||
import { useConvexClient } from "convex-svelte";
|
||||
import { ExternalLink, FileText, File, Upload, Trash2, Eye, RefreshCw } from "lucide-svelte";
|
||||
|
||||
import {
|
||||
ExternalLink,
|
||||
FileText,
|
||||
File,
|
||||
Upload,
|
||||
Trash2,
|
||||
Eye,
|
||||
RefreshCw,
|
||||
} from "lucide-svelte";
|
||||
|
||||
interface Props {
|
||||
label: string;
|
||||
helpUrl?: string;
|
||||
@@ -21,9 +29,9 @@
|
||||
onUpload,
|
||||
onRemove,
|
||||
}: Props = $props();
|
||||
|
||||
|
||||
const client = useConvexClient();
|
||||
|
||||
|
||||
let fileInput: HTMLInputElement;
|
||||
let uploading = $state(false);
|
||||
let error = $state<string | null>(null);
|
||||
@@ -31,7 +39,7 @@
|
||||
let fileType = $state<string>("");
|
||||
let previewUrl = $state<string | null>(null);
|
||||
let fileUrl = $state<string | null>(null);
|
||||
|
||||
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
||||
const ALLOWED_TYPES = [
|
||||
"application/pdf",
|
||||
@@ -39,7 +47,7 @@
|
||||
"image/jpg",
|
||||
"image/png",
|
||||
];
|
||||
|
||||
|
||||
// Buscar URL do arquivo quando houver um storageId
|
||||
$effect(() => {
|
||||
if (value && !fileName) {
|
||||
@@ -47,7 +55,7 @@
|
||||
loadExistingFile(value);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async function loadExistingFile(storageId: string) {
|
||||
try {
|
||||
const url = await client.storage.getUrl(storageId as any);
|
||||
@@ -66,34 +74,34 @@
|
||||
console.error("Erro ao carregar arquivo existente:", err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleFileSelect(event: Event) {
|
||||
const target = event.target as HTMLInputElement;
|
||||
const file = target.files?.[0];
|
||||
|
||||
|
||||
if (!file) return;
|
||||
|
||||
|
||||
error = null;
|
||||
|
||||
|
||||
// Validate file size
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
error = "Arquivo muito grande. Tamanho máximo: 10MB";
|
||||
target.value = "";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Validate file type
|
||||
if (!ALLOWED_TYPES.includes(file.type)) {
|
||||
error = "Tipo de arquivo não permitido. Use PDF ou imagens (JPG, PNG)";
|
||||
target.value = "";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
uploading = true;
|
||||
fileName = file.name;
|
||||
fileType = file.type;
|
||||
|
||||
|
||||
// Create preview for images
|
||||
if (file.type.startsWith("image/")) {
|
||||
const reader = new FileReader();
|
||||
@@ -102,9 +110,8 @@
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
|
||||
|
||||
await onUpload(file);
|
||||
|
||||
} catch (err: any) {
|
||||
error = err?.message || "Erro ao fazer upload do arquivo";
|
||||
previewUrl = null;
|
||||
@@ -113,12 +120,12 @@
|
||||
target.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleRemove() {
|
||||
if (!confirm("Tem certeza que deseja remover este arquivo?")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
uploading = true;
|
||||
await onRemove();
|
||||
@@ -132,13 +139,13 @@
|
||||
uploading = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function handleView() {
|
||||
if (fileUrl) {
|
||||
window.open(fileUrl, '_blank');
|
||||
window.open(fileUrl, "_blank");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function openFileDialog() {
|
||||
fileInput?.click();
|
||||
}
|
||||
@@ -152,10 +159,13 @@
|
||||
<span class="text-error">*</span>
|
||||
{/if}
|
||||
{#if helpUrl}
|
||||
<div class="tooltip tooltip-right" data-tip="Clique para acessar o link">
|
||||
<a
|
||||
href={helpUrl}
|
||||
target="_blank"
|
||||
<div
|
||||
class="tooltip tooltip-right"
|
||||
data-tip="Clique para acessar o link"
|
||||
>
|
||||
<a
|
||||
href={helpUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="text-primary hover:text-primary-focus transition-colors"
|
||||
aria-label="Acessar link"
|
||||
@@ -166,7 +176,7 @@
|
||||
{/if}
|
||||
</span>
|
||||
</label>
|
||||
|
||||
|
||||
<input
|
||||
id="file-upload-input"
|
||||
type="file"
|
||||
@@ -176,27 +186,39 @@
|
||||
class="hidden"
|
||||
{disabled}
|
||||
/>
|
||||
|
||||
|
||||
{#if value || fileName}
|
||||
<div class="flex items-center gap-2 p-3 border border-base-300 rounded-lg bg-base-100">
|
||||
<div
|
||||
class="flex items-center gap-2 p-3 border border-base-300 rounded-lg bg-base-100"
|
||||
>
|
||||
<!-- Preview -->
|
||||
<div class="flex-shrink-0">
|
||||
<div class="shrink-0">
|
||||
{#if previewUrl}
|
||||
<img src={previewUrl} alt="Preview" class="w-12 h-12 object-cover rounded" />
|
||||
<img
|
||||
src={previewUrl}
|
||||
alt="Preview"
|
||||
class="w-12 h-12 object-cover rounded"
|
||||
/>
|
||||
{:else if fileType === "application/pdf" || fileName.endsWith(".pdf")}
|
||||
<div class="w-12 h-12 bg-error/10 rounded flex items-center justify-center">
|
||||
<div
|
||||
class="w-12 h-12 bg-error/10 rounded flex items-center justify-center"
|
||||
>
|
||||
<FileText class="h-6 w-6 text-error" strokeWidth={2} />
|
||||
</div>
|
||||
{:else}
|
||||
<div class="w-12 h-12 bg-success/10 rounded flex items-center justify-center">
|
||||
<div
|
||||
class="w-12 h-12 bg-success/10 rounded flex items-center justify-center"
|
||||
>
|
||||
<File class="h-6 w-6 text-success" strokeWidth={2} />
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
|
||||
<!-- File info -->
|
||||
<div class="flex-1 min-w-0">
|
||||
<p class="text-sm font-medium truncate">{fileName || "Arquivo anexado"}</p>
|
||||
<p class="text-sm font-medium truncate">
|
||||
{fileName || "Arquivo anexado"}
|
||||
</p>
|
||||
<p class="text-xs text-base-content/60">
|
||||
{#if uploading}
|
||||
Carregando...
|
||||
@@ -205,7 +227,7 @@
|
||||
{/if}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Actions -->
|
||||
<div class="flex gap-2">
|
||||
{#if fileUrl}
|
||||
@@ -255,11 +277,10 @@
|
||||
{/if}
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
|
||||
{#if error}
|
||||
<div class="label">
|
||||
<span class="label-text-alt text-error">{error}</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user