Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 22x 6x 7x 167x 167x 16x 16x 16x 16x 16x 22x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 3x 167x 167x 1x 1x 1x 1x 167x 159x 8x 167x 2x 2x 1x 4x | import React, { useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { ArrowRightLeft } from "lucide-react";
import { Modal, ModalActions } from "@/components/detail";
import { Resource } from "@/objects/Doc";
import { MovableKind, MoveItem, MovePlan } from "@/objects/Ownership";
import { OwnerOption, ownerKeyOf, PERSONAL, useOwners } from "@/hooks/useOwners";
import { useAccessCatalogue } from "@/hooks/useAccessCatalogue";
import { useWorkspace } from "@/context/WorkspaceContext";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
const ItemList: React.FC<{ title: string; items: MoveItem[]; tone?: string }> = ({
title,
items,
tone = "text-gray-700 dark:text-gray-200",
}) =>
items.length === 0 ? null : (
<div>
<div className="text-xs font-medium text-gray-500 dark:text-gray-400 mb-1">
{title}
</div>
<ul className="space-y-1">
{items.map((item) => (
<li key={`${item.kind}:${item.id}`} className={`text-sm ${tone}`}>
<span className="text-xs text-gray-400 dark:text-gray-500">
{item.label}:
</span>{" "}
{item.name}
{item.reason && (
<span className="block text-xs text-gray-400 dark:text-gray-500">
{item.reason}
</span>
)}
</li>
))}
</ul>
</div>
);
/** Whether the caller may create a `kind` in `option`: always for Personal
* and for a superuser outside it; otherwise their level in its area. */
function useCreatableOwners() {
const { needs, allows } = useAccessCatalogue();
return {
canCreateIn: (option: OwnerOption, kind: string) => {
Iif (option.kind === "personal") return true;
const need = needs(kind);
Iif (!need) return false;
Iif (option.access == null) return option.kind === "organisation";
return allows(option.access, need.area, need.level);
},
};
}
/**
* "Move to…" for personal work: into an organisation or project the user
* belongs to. Previews first, so the user sees what goes with it (its
* personal repositories, sources, skills…) and anything that blocks it,
* before anything changes. Hidden on work that is already shared, built-in,
* or when the user belongs to nothing to move it into.
*/
export const MoveToShared: React.FC<{
kind: MovableKind;
resource: Resource & { is_system?: boolean | null };
onMoved?: () => void;
}> = ({ kind, resource, onMoved }) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const workspace = useWorkspace();
const { shared } = useOwners();
const [open, setOpen] = useState(false);
const [destKey, setDestKey] = useState<string>("");
const { canCreateIn } = useCreatableOwners();
// Moving it in is creating it there: only owners where the caller's level
// in its area allows that (svc-core says why for anything it depends on).
const offered = shared.filter((o) => canCreateIn(o, kind));
const dest = offered.find((o) => o.key === destKey) ?? offered[0];
const to = dest
? dest.kind === "organisation"
? { organisation_id: dest.id! }
: { project_id: dest.id! }
: {};
const previewQuery = useQuery<MovePlan>({
queryKey: ["ownership:preview", kind, resource.id, dest?.key],
queryFn: () => getApiHandler().ownership_router.preview(kind, resource.id, to),
enabled: open && !!dest,
retry: false,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const plan: MovePlan | undefined = previewQuery.data;
const move = useMutation({
mutationFn: () => getApiHandler().ownership_router.move(kind, resource.id, to),
onSuccess: () => {
setOpen(false);
// Its owner changed, and so possibly did its dependencies': every
// cached list and page may now show it differently.
queryClient.invalidateQueries();
onMoved?.();
},
});
if (
resource.is_system === true ||
// Only from Personal: work moves out of a person's own workspace.
workspace.kind !== "personal" ||
ownerKeyOf(resource) !== PERSONAL.key ||
offered.length === 0
)
return null;
const blocked = (plan?.blockers.length ?? 0) > 0;
return (
<>
<button
type="button"
onClick={() => {
move.reset();
setOpen(true);
}}
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
>
<ArrowRightLeft className="size-3.5" />
Move to…
</button>
{open && (
<Modal
title="Move to an organisation or project"
icon={ArrowRightLeft}
description="Everyone in it will be able to use and edit it. Personal work it depends on moves with it."
busy={move.isPending}
onClose={() => setOpen(false)}
footer={
<ModalActions
onCancel={() => setOpen(false)}
onConfirm={() => move.mutate()}
confirmLabel="Move"
loadingLabel="Moving…"
loading={move.isPending}
disabled={!plan || blocked || previewQuery.isFetching}
/>
}
>
<div className="space-y-4">
<label className="block text-xs text-gray-500 dark:text-gray-400">
Move to
<select
aria-label="Move to"
value={dest?.key ?? ""}
onChange={(e) => setDestKey(e.target.value)}
className="mt-1 block w-full text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-3 py-1.5"
>
{offered.map((o) => (
<option key={o.key} value={o.key}>
{o.name} ({o.kind})
</option>
))}
</select>
</label>
{previewQuery.isError && (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
{errorDetail(previewQuery.error, "Could not plan the move.")}
</p>
)}
{plan && (
<>
<ItemList title="Moves" items={plan.moves} />
<ItemList
title="Already there, or built in"
items={plan.already_there}
tone="text-gray-500 dark:text-gray-400"
/>
<ItemList
title="Can't move: someone else's"
items={plan.blockers}
tone="text-red-600 dark:text-red-400"
/>
</>
)}
{move.isError && (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
{errorDetail(move.error, "Could not move it.")}
</p>
)}
</div>
</Modal>
)}
</>
);
};
export default MoveToShared;
|