diff --git a/packages/frontend/src/features/ReportActions.tsx b/packages/frontend/src/features/ReportActions.tsx index 09d5b87e..0b03f886 100644 --- a/packages/frontend/src/features/ReportActions.tsx +++ b/packages/frontend/src/features/ReportActions.tsx @@ -11,11 +11,12 @@ import { omit } from "pastable"; import { ReportWithUser } from "./ReportList"; import { useNavigate } from "@tanstack/react-router"; import { api } from "../api"; +import { useCanEditReport } from "../hooks/useCanEditReport"; export const ReportActions = forwardRef(({ report }, ref) => { const user = useUser()!; - const isOwner = report.createdBy === user.id; + const canEdit = useCanEditReport(report); const navigate = useNavigate(); @@ -32,8 +33,11 @@ export const ReportActions = forwardRef - {isOwner ? ( + {canEdit ? ( <> ) : null} - {isOwner ? ( + {canEdit ? ( <> { + const user = useUser()!; + const isOwner = report.redactedById === user.id; + const isCreator = report.createdBy === user.id; + + const userDelegations = useLiveQuery( + db.delegation.liveFirst({ where: { createdBy: report.createdBy, delegatedTo: user.id } }), + ); + + const hasDelegation = !!userDelegations.results; + const canEdit = isOwner || isCreator || hasDelegation; + + return canEdit; +}; diff --git a/packages/frontend/src/routes/edit.$reportId.tsx b/packages/frontend/src/routes/edit.$reportId.tsx index 27b119d7..b0801429 100644 --- a/packages/frontend/src/routes/edit.$reportId.tsx +++ b/packages/frontend/src/routes/edit.$reportId.tsx @@ -20,7 +20,7 @@ import { db } from "../db"; import { InfoForm } from "../features/InfoForm"; import { NotesForm } from "../features/NotesForm"; import { DisabledContext } from "../features/DisabledContext"; -import { useUser } from "../contexts/AuthContext"; +import { useCanEditReport } from "../hooks/useCanEditReport"; const EditReport = () => { const { reportId } = Route.useParams(); @@ -64,7 +64,6 @@ function useFormWithFocus(props: return [form, () => focusedRef.current] as const; } - const WithReport = ({ report }: { report: Report }) => { const { tab } = Route.useSearch(); const [form, getFocused] = useFormWithFocus({ @@ -72,15 +71,7 @@ const WithReport = ({ report }: { report: Report }) => { resetOptions: {}, }); - const user = useUser()!; - const isOwner = report.redactedById === user.id; - - const userDelegations = useLiveQuery( - db.delegation.liveFirst({ where: { createdBy: report.createdBy, delegatedTo: user.id } }), - ); - - const hasDelegation = !!userDelegations.results; - const canEdit = isOwner || hasDelegation; + const canEdit = useCanEditReport(report); const navigate = useNavigate();