fix(files): allow uploading to root when another user has same name (#15)
CD — Deployment / deploy (push) Successful in 43s
CD — Deployment / deploy (push) Successful in 43s
Root is a personal namespace per user, so duplicate checks must scope to the current owner when uploading at root level. Subfolders keep their existing uniqueness rules. Also fixed folder creation at root with the same owner scope. --------- Co-authored-by: xEsiah <[email protected]> Reviewed-on: #15 Reviewed-by: Esiah <[email protected]> Co-authored-by: Gitea Actions <-[email protected]>
This commit was merged in pull request #15.
This commit is contained in:
@@ -8,7 +8,14 @@ import {
|
||||
type KeyboardEvent as ReactKeyboardEvent,
|
||||
} from "react";
|
||||
import { Button, Spinner, cn } from "../ui/index.js";
|
||||
import { ApiError, api, formatDate, type ChatMember, type ChatMessage } from "../lib/api.js";
|
||||
import {
|
||||
ApiError,
|
||||
api,
|
||||
formatDate,
|
||||
formatDateTimeSeconds,
|
||||
type ChatMember,
|
||||
type ChatMessage,
|
||||
} from "../lib/api.js";
|
||||
import { useI18n } from "../lib/i18n.js";
|
||||
import { getLocale } from "../lib/locale.js";
|
||||
import { useSession } from "../lib/session.js";
|
||||
@@ -16,7 +23,16 @@ import { useConfirm } from "./confirm.js";
|
||||
import { Avatar } from "./Avatar.js";
|
||||
import { IconAction } from "./IconAction.js";
|
||||
import { Alert, Badge, EmptyState } from "./feedback.js";
|
||||
import { ChatIcon, CloseIcon, SendIcon, TrashIcon, UsersIcon } from "./icons.js";
|
||||
import {
|
||||
ArrowRightIcon,
|
||||
ChatIcon,
|
||||
CloseIcon,
|
||||
PenIcon,
|
||||
PinIcon,
|
||||
SendIcon,
|
||||
TrashIcon,
|
||||
UsersIcon,
|
||||
} from "./icons.js";
|
||||
|
||||
const MAX_LENGTH = 2000;
|
||||
const POLL_MESSAGES_MS = 4000;
|
||||
@@ -63,15 +79,25 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
|
||||
const [members, setMembers] = useState<ChatMember[]>([]);
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [pins, setPins] = useState<ChatMessage[]>([]);
|
||||
const [pinsOpen, setPinsOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [draft, setDraft] = useState("");
|
||||
const [sending, setSending] = useState(false);
|
||||
const [deletingId, setDeletingId] = useState<number | null>(null);
|
||||
const [pinningId, setPinningId] = useState<number | null>(null);
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
const [editDraft, setEditDraft] = useState("");
|
||||
const [savingEdit, setSavingEdit] = useState(false);
|
||||
const [membersOpen, setMembersOpen] = useState(false);
|
||||
|
||||
const scrollRef = useRef<HTMLDivElement | null>(null);
|
||||
const lastIdRef = useRef(0);
|
||||
// Curseur d'edition : horodatage delivre par le serveur, renvoye tel quel.
|
||||
// Le curseur d'identifiant ne voit que les messages plus recents, ce second
|
||||
// curseur rattrape les reecritures d'anciens messages.
|
||||
const editedSinceRef = useRef<string | null>(null);
|
||||
// Tant que l'utilisateur reste au bas de la conversation, chaque nouveau
|
||||
// message le ramene en bas ; s'il remonte lire l'historique, on ne le force
|
||||
// pas a redescendre.
|
||||
@@ -86,9 +112,27 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
(incoming: ChatMessage[]) => {
|
||||
if (incoming.length === 0) return;
|
||||
setMessages((previous) => {
|
||||
const known = new Set(previous.map((message) => message.id));
|
||||
const fresh = incoming.filter((message) => !known.has(message.id));
|
||||
return fresh.length > 0 ? [...previous, ...fresh] : previous;
|
||||
const index = new Map(previous.map((message) => [message.id, message]));
|
||||
let changed = false;
|
||||
for (const message of incoming) {
|
||||
const known = index.get(message.id);
|
||||
if (!known) {
|
||||
index.set(message.id, message);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
// Un message deja connu peut avoir ete reecrit ou change d'etat
|
||||
// d'epinglage : on ne remplace que si la version recue est plus
|
||||
// recente, pour ne pas ecraser une edition locale toute recente.
|
||||
const knownEdited = known.edited_at ? Date.parse(known.edited_at) : 0;
|
||||
const incomingEdited = message.edited_at ? Date.parse(message.edited_at) : 0;
|
||||
if (incomingEdited > knownEdited || message.is_pinned !== known.is_pinned) {
|
||||
index.set(message.id, { ...known, ...message });
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (!changed) return previous;
|
||||
return [...index.values()].sort((a, b) => a.id - b.id);
|
||||
});
|
||||
lastIdRef.current = Math.max(lastIdRef.current, MAX_ID(incoming));
|
||||
if (stickToBottomRef.current) queueMicrotask(scrollToBottom);
|
||||
@@ -105,17 +149,22 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
let active = true;
|
||||
setLoading(true);
|
||||
setMessages([]);
|
||||
setPins([]);
|
||||
lastIdRef.current = 0;
|
||||
editedSinceRef.current = null;
|
||||
void (async () => {
|
||||
try {
|
||||
const [memberResult, messageResult] = await Promise.all([
|
||||
const [memberResult, messageResult, pinResult] = await Promise.all([
|
||||
api.chatMembers(folderId),
|
||||
api.chatMessages(folderId, 0, 50),
|
||||
api.chatPins(folderId),
|
||||
]);
|
||||
if (!active) return;
|
||||
setMembers(memberResult.members);
|
||||
setMessages(messageResult.messages);
|
||||
setPins(pinResult.messages);
|
||||
lastIdRef.current = MAX_ID(messageResult.messages);
|
||||
editedSinceRef.current = messageResult.cursor;
|
||||
setError(null);
|
||||
} catch (caught) {
|
||||
if (!active) return;
|
||||
@@ -138,8 +187,15 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
const poll = async () => {
|
||||
if (document.hidden) return;
|
||||
try {
|
||||
const { messages: fresh } = await api.chatMessages(folderId, lastIdRef.current, 100);
|
||||
mergeMessages(fresh);
|
||||
const [result, pinResult] = await Promise.all([
|
||||
api.chatMessages(folderId, lastIdRef.current, 100, editedSinceRef.current),
|
||||
api.chatPins(folderId),
|
||||
]);
|
||||
// Le curseur avance avec la reponse : le serveur le calcule avant la
|
||||
// requete, une edition survenue entre les deux sera vue au tour suivant.
|
||||
editedSinceRef.current = result.cursor;
|
||||
mergeMessages(result.messages);
|
||||
setPins(pinResult.messages);
|
||||
} catch {
|
||||
/* le prochain cycle retentera */
|
||||
}
|
||||
@@ -194,7 +250,7 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
mergeMessages([message]);
|
||||
setDraft("");
|
||||
} catch (caught) {
|
||||
setError(caught instanceof ApiError ? caught.message : t("Envoi impossible."));
|
||||
setError(caught instanceof ApiError ? caught.message : t("Envoi du message impossible."));
|
||||
} finally {
|
||||
setSending(false);
|
||||
}
|
||||
@@ -214,6 +270,8 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
try {
|
||||
await api.chatDeleteMessage(folderId, message.id);
|
||||
setMessages((previous) => previous.filter((item) => item.id !== message.id));
|
||||
// Un message retire disparait aussi des epingles.
|
||||
setPins((previous) => previous.filter((item) => item.id !== message.id));
|
||||
} catch (caught) {
|
||||
setError(caught instanceof ApiError ? caught.message : t("Suppression impossible."));
|
||||
} finally {
|
||||
@@ -221,6 +279,58 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
}
|
||||
};
|
||||
|
||||
const startEdit = (message: ChatMessage) => {
|
||||
setEditingId(message.id);
|
||||
setEditDraft(message.body);
|
||||
};
|
||||
|
||||
const cancelEdit = () => {
|
||||
setEditingId(null);
|
||||
setEditDraft("");
|
||||
};
|
||||
|
||||
const saveEdit = async (message: ChatMessage) => {
|
||||
const body = editDraft.trim();
|
||||
if (!body || savingEdit) return;
|
||||
setSavingEdit(true);
|
||||
setError(null);
|
||||
try {
|
||||
const { message: updated } = await api.chatEditMessage(folderId, message.id, body);
|
||||
mergeMessages([updated]);
|
||||
setPins((previous) =>
|
||||
previous.map((item) => (item.id === updated.id ? { ...item, ...updated } : item)),
|
||||
);
|
||||
cancelEdit();
|
||||
} catch (caught) {
|
||||
setError(caught instanceof ApiError ? caught.message : t("Modification impossible."));
|
||||
} finally {
|
||||
setSavingEdit(false);
|
||||
}
|
||||
};
|
||||
|
||||
const togglePin = async (message: ChatMessage) => {
|
||||
if (pinningId !== null) return;
|
||||
setPinningId(message.id);
|
||||
setError(null);
|
||||
try {
|
||||
const { pinned } = await api.chatTogglePin(folderId, message.id);
|
||||
const updated = { ...message, is_pinned: pinned };
|
||||
mergeMessages([updated]);
|
||||
setPins((previous) =>
|
||||
pinned
|
||||
? [updated, ...previous.filter((item) => item.id !== updated.id)]
|
||||
: previous.filter((item) => item.id !== updated.id),
|
||||
);
|
||||
// Le bandeau s'ouvre quand on epingle : c'est le moment ou l'utilisateur
|
||||
// veut voir ce qu'il vient de mettre de cote.
|
||||
if (pinned) setPinsOpen(true);
|
||||
} catch (caught) {
|
||||
setError(caught instanceof ApiError ? caught.message : t("Épinglage impossible."));
|
||||
} finally {
|
||||
setPinningId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const onComposerKeyDown = (event: ReactKeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
@@ -241,7 +351,9 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
GROUP_WINDOW_MS;
|
||||
const own = message.user_id === user?.id;
|
||||
const canDelete = own;
|
||||
return { message, newDay, continuation, own, canDelete };
|
||||
// Seul l'auteur peut reecrire son message, sans limite de temps.
|
||||
const canEdit = own;
|
||||
return { message, newDay, continuation, own, canDelete, canEdit };
|
||||
}),
|
||||
[messages, user?.id],
|
||||
);
|
||||
@@ -337,6 +449,49 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{pins.length > 0 ? (
|
||||
<div className="shrink-0 border-b border-border bg-surface px-4 py-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setPinsOpen((value) => !value)}
|
||||
aria-expanded={pinsOpen}
|
||||
className="flex w-full items-center gap-2 text-left text-xs font-medium text-foreground"
|
||||
>
|
||||
<PinIcon className="h-3.5 w-3.5 shrink-0 text-accent" />
|
||||
<span>{t("Messages épinglés")}</span>
|
||||
<span className="text-subtle-foreground">{pins.length}</span>
|
||||
<ArrowRightIcon
|
||||
className={cn(
|
||||
"ml-auto h-3.5 w-3.5 shrink-0 text-subtle-foreground transition-transform",
|
||||
pinsOpen && "rotate-90",
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
{pinsOpen ? (
|
||||
<ul className="mt-2 flex max-h-48 flex-col gap-2 overflow-y-auto">
|
||||
{pins.map((pin) => (
|
||||
<li
|
||||
key={pin.id}
|
||||
className="rounded-md border border-border bg-background px-3 py-2"
|
||||
>
|
||||
<p className="flex items-center gap-2 text-xs">
|
||||
<span className="font-medium text-foreground">
|
||||
{fullName(pin, t("Compte supprimé"))}
|
||||
</span>
|
||||
<span className="text-[0.6875rem] text-subtle-foreground">
|
||||
{timeOf(pin.created_at)}
|
||||
</span>
|
||||
</p>
|
||||
<p className="mt-1 whitespace-pre-wrap break-words text-sm leading-relaxed text-foreground">
|
||||
{pin.body}
|
||||
</p>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div
|
||||
ref={scrollRef}
|
||||
onScroll={onScroll}
|
||||
@@ -356,7 +511,8 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
/>
|
||||
) : (
|
||||
<ol className="flex flex-col gap-1">
|
||||
{rendered.map(({ message, newDay, continuation, own, canDelete }) => (
|
||||
{rendered.map(
|
||||
({ message, newDay, continuation, own, canDelete, canEdit }) => (
|
||||
<li key={message.id}>
|
||||
{newDay ? (
|
||||
<div className="my-3 flex items-center gap-3" role="separator">
|
||||
@@ -389,7 +545,10 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
{continuation ? null : (
|
||||
<p className="flex items-center gap-2 text-xs">
|
||||
<span
|
||||
className={cn("font-medium", own ? "text-accent" : "text-foreground")}
|
||||
className={cn(
|
||||
"font-medium",
|
||||
own ? "text-accent" : "text-foreground",
|
||||
)}
|
||||
>
|
||||
{fullName(message, t("Compte supprimé"))}
|
||||
</span>
|
||||
@@ -398,12 +557,88 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
</span>
|
||||
</p>
|
||||
)}
|
||||
<p className="whitespace-pre-wrap break-words text-sm leading-relaxed text-foreground">
|
||||
{message.body}
|
||||
</p>
|
||||
{editingId === message.id ? (
|
||||
<div className="mt-1 flex flex-col gap-2">
|
||||
<textarea
|
||||
value={editDraft}
|
||||
autoFocus
|
||||
onChange={(event) => setEditDraft(event.target.value.slice(0, MAX_LENGTH))}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
void saveEdit(message);
|
||||
}
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancelEdit();
|
||||
}
|
||||
}}
|
||||
rows={2}
|
||||
maxLength={MAX_LENGTH}
|
||||
aria-label={t("Modifier le message")}
|
||||
className="max-h-40 min-h-[2.5rem] resize-y rounded-sm border border-border-control bg-surface px-3 py-2 text-sm text-foreground focus-visible:border-border-strong focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background focus-visible:outline-none"
|
||||
/>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
isLoading={savingEdit}
|
||||
disabled={editDraft.trim().length === 0}
|
||||
onClick={() => void saveEdit(message)}
|
||||
>
|
||||
{t("Enregistrer")}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={cancelEdit}
|
||||
>
|
||||
{t("Annuler")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<p className="whitespace-pre-wrap break-words text-sm leading-relaxed text-foreground">
|
||||
{message.body}
|
||||
</p>
|
||||
{message.edited_at ? (
|
||||
<p
|
||||
className="mt-0.5 text-[0.6875rem] text-subtle-foreground"
|
||||
title={formatDateTimeSeconds(message.edited_at)}
|
||||
>
|
||||
{t("(modifié)")}
|
||||
</p>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{canDelete ? (
|
||||
<span className="opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100">
|
||||
{/* L'epinglage est ouvert a tous les participants ; l'edition
|
||||
et la suppression restent reservees a l'auteur. */}
|
||||
<span className="flex shrink-0 items-center opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100">
|
||||
<IconAction
|
||||
label={
|
||||
message.is_pinned
|
||||
? t("Désépingler le message")
|
||||
: t("Épingler le message")
|
||||
}
|
||||
isLoading={pinningId === message.id}
|
||||
icon={
|
||||
<PinIcon
|
||||
className={cn("h-3.5 w-3.5", message.is_pinned && "text-accent")}
|
||||
/>
|
||||
}
|
||||
onClick={() => void togglePin(message)}
|
||||
/>
|
||||
{canEdit ? (
|
||||
<IconAction
|
||||
label={t("Modifier le message")}
|
||||
icon={<PenIcon className="h-3.5 w-3.5" />}
|
||||
onClick={() => startEdit(message)}
|
||||
/>
|
||||
) : null}
|
||||
{canDelete ? (
|
||||
<IconAction
|
||||
label={t("Supprimer le message")}
|
||||
tone="danger"
|
||||
@@ -411,11 +646,12 @@ export const FolderChat = ({ folder, onClose }: { folder: ChatFolder; onClose: (
|
||||
icon={<TrashIcon className="h-3.5 w-3.5" />}
|
||||
onClick={() => void remove(message)}
|
||||
/>
|
||||
</span>
|
||||
) : null}
|
||||
) : null}
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
),
|
||||
)}
|
||||
</ol>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -185,6 +185,14 @@ export const PenIcon = (props: IconProps) => (
|
||||
</svg>
|
||||
);
|
||||
|
||||
/** Epingle : tete du pushpin en haut, pointe en bas. */
|
||||
export const PinIcon = (props: IconProps) => (
|
||||
<svg {...base(props)}>
|
||||
<path d="M9 3.5h6l-.8 5.2 3.3 3.1H6.5l3.3-3.1Z" />
|
||||
<path d="M12 11.8V20.5" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const MoveIcon = (props: IconProps) => (
|
||||
<svg {...base(props)}>
|
||||
<path d="M12 4v16M4 12h16" />
|
||||
|
||||
Reference in New Issue
Block a user