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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 2x 2x 20x 20x 20x 20x 12x 20x 20x 20x 20x 1x 10x 1x 1x 7x 2x 14x 14x 14x 14x 14x 14x 14x 7x 14x 14x 14x 3x 14x 2x 14x 1x 1x 1x 1x 14x 1x 1x 1x 1x 1x 14x 14x 14x 9x 18x 18x 2x 1x 1x 1x | import React, { useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { ArrowDownToLine, ShieldAlert, X } from "lucide-react";
import WorkspaceSettingsTabs from "./settings/WorkspaceSettingsTabs";
import MainPage from "@/components/page/MainPage/MainPage";
import { ConfirmDialog, SectionCard, SkeletonLines, Strong } from "@/components/detail";
import { useWorkspace } from "@/context/WorkspaceContext";
import {
InheritableKind,
InheritanceCandidate,
InheritanceDeclaration,
InheritanceDependent,
ProjectInheritance,
} from "@/objects/Inheritance";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
const selectCls =
"text-xs rounded-md border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-2 py-1 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500";
/** One kind: everything of it, or the ones picked. */
const KindRow: React.FC<{
projectId: string;
kind: InheritableKind;
parentName: string;
declarations: InheritanceDeclaration[];
busy: boolean;
onDeclare: (resourceId: string | null) => void;
onWithdraw: (declaration: InheritanceDeclaration) => void;
}> = ({ projectId, kind, parentName, declarations, busy, onDeclare, onWithdraw }) => {
const { getApiHandler } = useApiHandler();
const all = declarations.find((d) => d.resource_id === null);
const picked = declarations.filter((d) => d.resource_id !== null);
const candidatesQuery = useQuery<InheritanceCandidate[]>({
queryKey: [QUERY_KEYS.GET.INHERITANCE_CANDIDATES, projectId, kind.key],
queryFn: () => getApiHandler().project_router.inheritanceCandidates(projectId, kind.key),
enabled: !all,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const candidates: InheritanceCandidate[] = Array.isArray(candidatesQuery.data)
? candidatesQuery.data
: [];
const available = candidates.filter((c) => !c.inherited);
const label = kind.label.toLowerCase();
return (
<div className="px-5 py-3 space-y-2">
<label className="flex items-start gap-2 text-sm text-gray-800 dark:text-gray-200">
<input
type="checkbox"
className="mt-0.5"
checked={!!all}
disabled={busy}
onChange={() => (all ? onWithdraw(all) : onDeclare(null))}
/>
<span>
<span className="font-medium">{kind.label}</span>
<span className="block text-xs text-gray-400 dark:text-gray-500">
Every one of {parentName}'s {label}, including ones added later.
</span>
</span>
</label>
{!all && (
<div className="pl-6 flex flex-wrap items-center gap-2">
{picked.map((d) => (
<span
key={d.id}
className="inline-flex items-center gap-1 text-xs px-2 py-0.5 rounded-full bg-sky-50 dark:bg-sky-950/40 text-sky-700 dark:text-sky-300 border border-sky-200 dark:border-sky-800"
>
{d.resource_name ?? "Unnamed"}
<button
type="button"
aria-label={`Stop inheriting ${d.resource_name ?? "it"}`}
disabled={busy}
onClick={() => onWithdraw(d)}
className="hover:text-red-600"
>
<X className="size-3" />
</button>
</span>
))}
{available.length > 0 && (
<select
aria-label={`Inherit one of ${parentName}'s ${label}`}
className={selectCls}
value=""
disabled={busy}
onChange={(e) => e.target.value && onDeclare(e.target.value)}
>
<option value="">Add one…</option>
{available.map((c) => (
<option key={c.id} value={c.id}>
{c.name}
</option>
))}
</select>
)}
{picked.length === 0 && available.length === 0 && !candidatesQuery.isLoading && (
<span className="text-xs text-gray-400 dark:text-gray-500">
{parentName} has none.
</span>
)}
</div>
)}
</div>
);
};
/**
* A project's Inheritance page: what it uses of its parent's work, by
* reference (svc-core `app/services/inheritance.py`). Rules the parent
* requires apply whatever is chosen here; everything else is opt-in, a
* whole kind or one resource at a time. Inherited work is usable in the
* project and changed only in the parent.
*/
const InheritancePage: React.FC = () => {
const ws = useWorkspace();
const projectId = ws.id ?? "";
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const [withdrawing, setWithdrawing] = useState<InheritanceDeclaration | null>(null);
const [dependents, setDependents] = useState<InheritanceDependent[] | null>(null);
const query = useQuery<ProjectInheritance>({
queryKey: [QUERY_KEYS.GET.PROJECT_INHERITANCE, projectId],
queryFn: () => getApiHandler().project_router.inheritance(projectId),
enabled: !!projectId,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`. Anything not
// shaped like an answer counts as still loading.
const raw: ProjectInheritance | undefined = query.data;
const data = raw && Array.isArray(raw.kinds) && raw.parent ? raw : undefined;
const refresh = () => {
// Inheriting changes what every list in the project shows.
void queryClient.invalidateQueries();
};
const declare = useMutation({
mutationFn: (body: { kind: string; resource_id?: string | null }) =>
getApiHandler().project_router.declareInheritance(projectId, body),
onSuccess: refresh,
});
const withdraw = useMutation({
mutationFn: (declarationId: string) =>
getApiHandler().project_router.withdrawInheritance(projectId, declarationId),
onSuccess: () => {
setWithdrawing(null);
setDependents(null);
refresh();
},
});
const askWithdraw = async (declaration: InheritanceDeclaration) => {
withdraw.reset();
setWithdrawing(declaration);
setDependents(null);
try {
setDependents(
await getApiHandler().project_router.inheritanceDependents(projectId, declaration.id),
);
} catch {
setDependents([]);
}
};
const parentName =
data?.parent.kind === "personal"
? "your personal workspace"
: (data?.parent.name ?? "the parent");
const busy = declare.isPending || withdraw.isPending;
return (
<MainPage
title="Inheritance"
description={`What this project uses of ${parentName}'s work. Inherited work is used here and changed only there.`}
links={[<WorkspaceSettingsTabs key="tabs" active="inheritance" />]}
>
<div className="space-y-6 max-w-3xl">
{declare.isError && (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
{errorDetail(declare.error, "Couldn't inherit that.")}
</p>
)}
<SectionCard
variant="panel"
icon={ShieldAlert}
title={`Required by ${parentName}`}
description="Rules that govern this project whatever is chosen below. Only the parent can change them."
>
{query.isLoading ? (
<SkeletonLines count={2} />
) : (data?.required.length ?? 0) === 0 ? (
<p className="px-5 py-3 text-sm text-gray-500 dark:text-gray-400">None.</p>
) : (
<ul className="divide-y divide-gray-100 dark:divide-gray-700/60">
{data!.required.map((rule) => (
<li key={rule.id} className="px-5 py-2.5 flex items-center justify-between gap-3">
<span className="text-sm text-gray-800 dark:text-gray-200">{rule.name}</span>
<span className="text-xs text-amber-700 dark:text-amber-300">
{rule.reason === "pinned" ? "Pinned to this project" : "Required of every project"}
</span>
</li>
))}
</ul>
)}
</SectionCard>
<SectionCard
variant="panel"
icon={ArrowDownToLine}
title={`Inherited from ${parentName}`}
description="Choose a whole kind, or pick them one at a time. A project's own work always comes first."
>
{query.isLoading || !data ? (
<SkeletonLines count={4} />
) : (
<div className="divide-y divide-gray-100 dark:divide-gray-700/60">
{data.kinds.map((kind) => (
<KindRow
key={kind.key}
projectId={projectId}
kind={kind}
parentName={parentName}
declarations={data.declarations.filter((d) => d.kind === kind.key)}
busy={busy}
onDeclare={(resourceId) =>
declare.mutate({ kind: kind.key, resource_id: resourceId })
}
onWithdraw={(d) => void askWithdraw(d)}
/>
))}
</div>
)}
</SectionCard>
</div>
{withdrawing && (
<ConfirmDialog
title="Stop inheriting?"
body={
withdrawing.resource_id ? (
<>
This project will stop using <Strong>{withdrawing.resource_name ?? "it"}</Strong>.
</>
) : (
<>This project will stop using {parentName}'s {withdrawing.kind}s.</>
)
}
confirmLabel="Stop inheriting"
loadingLabel="Removing…"
loading={withdraw.isPending}
error={withdraw.isError ? errorDetail(withdraw.error, "Couldn't change that.") : null}
onConfirm={() => withdraw.mutate(withdrawing.id)}
onCancel={() => {
setWithdrawing(null);
setDependents(null);
}}
>
{dependents === null ? (
<SkeletonLines count={1} />
) : dependents.length > 0 ? (
<div className="text-xs text-gray-600 dark:text-gray-300 space-y-1">
<p>These use it here, and will stop reaching it:</p>
<ul className="list-disc pl-5">
{dependents.map((d) => (
<li key={`${d.kind}:${d.id}`}>
{d.name ?? d.id} ({d.kind})
</li>
))}
</ul>
</div>
) : null}
</ConfirmDialog>
)}
</MainPage>
);
};
export default InheritancePage;
|