"use client";
import { useEffect, useState, useCallback } from "react";
import { useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import { Avatar } from "@/components/ui/avatar";
import {
Loader2,
RefreshCw,
Check,
X,
Folder,
Calendar,
BookUser,
HardDrive,
Trash2,
} from "lucide-react";
import { cn } from "@/lib/utils";
import { useAuthStore } from "@/stores/auth-store";
import { useSharingStore, type SharedResourceKind, type SharedFolder } from "@/stores/sharing-store";
const ICON_CLASS = "w-4 h-4 shrink-0";
function KindIcon({ kind }: { kind: SharedResourceKind }) {
switch (kind) {
case "mailbox":
return ;
case "calendar":
return ;
case "addressBook":
return ;
case "file":
return ;
}
}
function KindLabel({ kind }: { kind: SharedResourceKind }) {
switch (kind) {
case "mailbox":
return "Mail";
case "calendar":
return "Calendar";
case "addressBook":
return "Contacts";
case "file":
return "Files";
}
}
export function SharingSettings() {
const t = useTranslations("settings");
const tSharing = useTranslations("sharing");
const client = useAuthStore((s) => s.client);
const {
sharedByMe,
sharedWithMe,
loading,
fetchShares,
revokeShare,
changeRole,
acceptShare,
declineShare,
} = useSharingStore();
const [activeTab, setActiveTab] = useState<"byMe" | "withMe">("byMe");
const handleRefresh = useCallback(() => {
if (client) fetchShares(client);
}, [client, fetchShares]);
useEffect(() => {
if (client) handleRefresh();
}, [client, handleRefresh]);
const handleRevoke = async (share: SharedFolder) => {
if (!client) return;
await revokeShare(
client,
share.resourceId,
share.resourceKind,
share.principalId,
share.accountId,
);
};
const handleChangeRole = async (share: SharedFolder, role: string) => {
if (!client) return;
await changeRole(
client,
share.resourceId,
share.resourceKind,
share.principalId,
role,
share.accountId,
);
};
const handleAccept = async (share: SharedFolder) => {
if (!client) return;
await acceptShare(client, share);
};
const handleDecline = async (share: SharedFolder) => {
if (!client) return;
await declineShare(client, share);
};
return (
{loading && (
{t("loading")}
)}
{!loading && activeTab === "byMe" && (
<>
{sharedByMe.length === 0 ? (
{tSharing("no_shares_by_me")}
) : (
{sharedByMe.map((share) => (
))}
)}
>
)}
{!loading && activeTab === "withMe" && (
<>
{sharedWithMe.length === 0 ? (
{tSharing("no_shares_with_me")}
) : (
{sharedWithMe.map((share) => (
{share.resourceName}
|
{tSharing("shared_by")}: {share.principalName}
{tSharing(`preset.${share.role}`)}
{share.pending ? (
) : (
)}
))}
)}
>
)}
);
}