Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
import type { PullRequestActor } from "@t3tools/contracts";
import type { AssetResource, EnvironmentId, PullRequestActor } from "@t3tools/contracts";
import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle";
import { Image } from "expo-image";
import { useGT } from "gt-react-native";
import { useContext, useState } from "react";
import { View } from "react-native";
import { useResolveClassNames } from "uniwind";

import { AppText as Text } from "../../components/ui/AppText";
import { cn } from "../../lib/cn";
import { changeRequestTermKind } from "../../lib/changeRequestTerm";
import { EmptyState } from "../../components/ui/EmptyState";
import { assetImageCacheKey } from "../../lib/assetImageSource";
import { useAssetUrlState } from "../../state/assets";
import { PullRequestMediaContext } from "./PullRequestMedia";
import { pullRequestMediaSource } from "./pullRequestMediaSource";

export function PullRequestReadStatus(props: {
readonly pending: boolean;
Expand Down Expand Up @@ -52,13 +57,66 @@ export function PullRequestAvatar(props: {
readonly size?: number;
}) {
const size = props.size ?? 24;
return props.actor?.avatarUrl ? (
const media = useContext(PullRequestMediaContext);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const avatarUrl = props.actor?.avatarUrl;
const source = avatarUrl ? pullRequestMediaSource(avatarUrl, media) : null;
const resource = source && "resource" in source ? (source.resource ?? null) : null;
if (media !== null && resource !== null) {
return (
<HostAvatar
environmentId={media.environmentId}
resource={resource}
actor={props.actor}
size={size}
/>
);
}
return <AvatarImage uri={avatarUrl ?? null} actor={props.actor} size={size} />;
}

function HostAvatar(props: {
readonly environmentId: EnvironmentId;
readonly resource: AssetResource;
readonly actor: PullRequestActor | null;
readonly size: number;
}) {
const asset = useAssetUrlState(props.environmentId, props.resource);
return (
<AvatarImage
uri={
asset._tag === "Success"
? asset.url
: asset._tag === "Failure"
? (props.actor?.avatarUrl ?? null)
Comment thread
Ernest0-Production marked this conversation as resolved.
Resolved
: null
}
cacheKey={
asset._tag === "Success"
? assetImageCacheKey(props.environmentId, props.resource)
: undefined
}
actor={props.actor}
size={props.size}
/>
);
}

function AvatarImage(props: {
readonly uri: string | null;
readonly cacheKey?: string | undefined;
readonly actor: PullRequestActor | null;
readonly size: number;
}) {
const { size } = props;
const [failedUri, setFailedUri] = useState<string | null>(null);
return props.uri && failedUri !== props.uri ? (
<Image
source={{ uri: props.actor.avatarUrl }}
recyclingKey={props.actor.avatarUrl}
accessibilityLabel={props.actor.login}
source={props.cacheKey ? { uri: props.uri, cacheKey: props.cacheKey } : { uri: props.uri }}
recyclingKey={props.cacheKey ?? props.uri}
accessibilityLabel={props.actor?.login}
style={{ width: size, height: size, borderRadius: size / 2 }}
cachePolicy="memory-disk"
onError={() => setFailedUri(props.uri)}
/>
) : (
<View
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logi
import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
import {
PullRequestActorLabel,
PullRequestAvatarSourceContext,
PullRequestMetaLine,
PullRequestReviewOutcomeIcon,
pullRequestChecksState,
Expand Down Expand Up @@ -1601,7 +1602,7 @@ export function PullRequestDetailPanel({
);
}

return (
const panel = (
// One tooltip for the panel's many triggers; mounting one per trigger dominated opening it.
<SharedTooltipProvider>
<div className="relative flex h-full min-h-0 w-full flex-col bg-background">
Expand Down Expand Up @@ -2343,4 +2344,13 @@ export function PullRequestDetailPanel({
</div>
</SharedTooltipProvider>
);
return (
<PullRequestAvatarSourceContext
value={
detail ? { environmentId, cwd: detail.workspaceRoot, repository: detail.repository } : null
}
>
{panel}
</PullRequestAvatarSourceContext>
);
}
106 changes: 89 additions & 17 deletions apps/web/src/components/pullRequest/pullRequestPresentation.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Spinner } from "~/components/ui/spinner";
import type {
PullRequestActor,
PullRequestCheck,
PullRequestCheckStatus,
PullRequestChecksState,
PullRequestLabel,
PullRequestMergeability,
PullRequestReviewDecision,
PullRequestState,
ScopedThreadRef,
import {
SOURCE_CONTROL_MEDIA_ASSET,
type AssetResource,
type EnvironmentId,
type PullRequestActor,
type PullRequestCheck,
type PullRequestCheckStatus,
type PullRequestChecksState,
type PullRequestLabel,
type PullRequestMergeability,
type PullRequestReviewDecision,
type PullRequestState,
type ScopedThreadRef,
} from "@t3tools/contracts";
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import {
CircleCheckIcon,
CircleDashedIcon,
Expand All @@ -23,8 +27,17 @@ import {
UserRoundXIcon,
} from "lucide-react";
import { msg, useGT, useMessages } from "gt-react";
import { Children, type CSSProperties, isValidElement, type ReactNode, useState } from "react";

import {
Children,
createContext,
type CSSProperties,
isValidElement,
type ReactNode,
useContext,
useState,
} from "react";

import { useAssetUrlState } from "~/assets/assetUrls";
import { m, type MessageResolver } from "~/i18n/messages";
import { useOpenLink } from "~/browser/useOpenLink";
import { cn } from "~/lib/utils";
Expand Down Expand Up @@ -460,17 +473,76 @@ export function PullRequestReviewOutcomeBadge({
);
}

/** Where the server fetches an avatar the client cannot load from the host itself. */
export const PullRequestAvatarSourceContext = createContext<{
readonly environmentId: EnvironmentId;
readonly cwd: string;
readonly repository: string;
} | null>(null);

export function PullRequestActorAvatar({
actor,
className,
}: {
actor: PullRequestActor | null;
className?: string;
}) {
const login = actor?.login ?? "ghost";
const source = useContext(PullRequestAvatarSourceContext);
const avatarUrl = actor?.avatarUrl ?? null;
const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
const fetchUrl = avatarUrl === null ? null : sourceControlClients.mediaUrl(avatarUrl)?.fetchUrl;
const login = actor?.login ?? "ghost";
return source !== null && fetchUrl != null ? (
<HostActorAvatar
environmentId={source.environmentId}
resource={{
_tag: SOURCE_CONTROL_MEDIA_ASSET,
cwd: source.cwd,
url: fetchUrl,
repository: source.repository,
}}
avatarUrl={avatarUrl}
login={login}
className={className}
/>
) : (
<ActorAvatarImage src={avatarUrl} login={login} className={className} />
);
}

function HostActorAvatar({
environmentId,
resource,
avatarUrl,
login,
className,
}: {
environmentId: EnvironmentId;
resource: AssetResource;
avatarUrl: string | null;
login: string;
className: string | undefined;
}) {
const asset = useAssetUrlState(environmentId, resource);
return (
<ActorAvatarImage
src={asset._tag === "Success" ? asset.url : asset._tag === "Failure" ? avatarUrl : null}
Comment thread
Ernest0-Production marked this conversation as resolved.
Resolved
login={login}
className={className}
/>
);
}

function ActorAvatarImage({
src,
login,
className,
}: {
src: string | null;
login: string;
className: string | undefined;
}) {
const [failedSrc, setFailedSrc] = useState<string | null>(null);
return src === null || failedSrc === src ? (
<span
aria-hidden
className={cn(
Expand All @@ -484,10 +556,10 @@ export function PullRequestActorAvatar({
<img
aria-hidden
alt=""
src={avatarUrl}
src={src}
loading="lazy"
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
onError={() => setFailedAvatarUrl(avatarUrl)}
onError={() => setFailedSrc(src)}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/source-control-core/src/shared/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface ChangeRequestCheckoutInput {
readonly repositoryUrl?: string | null | undefined;
}

/** Media a host serves only to a request carrying a credential, as `mediaUrl` reads it. */
/** Media a client may not be able to load itself, as `mediaUrl` reads it. */
export interface SourceControlMediaUrl {
/** The canonical URL to fetch, the same for every spelling of one file. */
readonly fetchUrl: string;
Expand Down Expand Up @@ -387,8 +387,8 @@ export interface SourceControlDefinition {
*/
readonly canonicalRepositoryKey?: (key: string) => string | null;
/**
* Null when `url` is not media this host keeps behind a credential. A client cannot load such
* media itself, so it asks the server, whose driver for this host fetches it.
* Null when `url` is not media this host keeps behind a credential or where a remote client may
* not reach it. A client asks the server for such media, whose driver for this host fetches it.
*/
readonly mediaUrl?: (url: URL) => SourceControlMediaUrl | null;
/**
Expand Down
18 changes: 13 additions & 5 deletions packages/source-control-gitlab/src/server/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ import {
} from "@t3tools/source-control-core/server/driver";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { HttpClient } from "effect/http";

import { definition } from "../shared/definition.ts";

import * as GitLabCli from "./GitLabCli.ts";
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
import * as GitLabIssueTracker from "./GitLabIssueTracker.ts";
import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";

const services = GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer));

const makeInstance = Effect.all({
sourceControl: GitLabSourceControlProvider.make,
discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
pullRequests: GitLabPullRequestProvider.make,
issues: GitLabIssueTracker.make,
const makeInstance = Effect.gen(function* () {
const mediaHttpClient = yield* HttpClient.HttpClient;
return yield* Effect.all({
sourceControl: GitLabSourceControlProvider.make,
discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
pullRequests: GitLabPullRequestProvider.make,
issues: GitLabIssueTracker.make,
fetchMedia: GitLabMediaFetch.make.pipe(
Effect.provideService(HttpClient.HttpClient, mediaHttpClient),
),
});
});

export const driver = defineSourceControlDriver({
Expand Down
Loading
Loading