Improve handling of rawPath

Instead of computing it in multiple components, we should do it in
router.ts in the routes where we need it, and add the commit param when
we have it.
This commit is contained in:
Sebastian Martinez
2023-11-27 14:12:24 +01:00
parent 999adff9c1
commit b6ac9bd4a7
12 changed files with 48 additions and 34 deletions
+1
View File
@@ -143,6 +143,7 @@
{#if editComment && state !== "read"}
{@const editComment_ = editComment}
<ExtendedTextarea
{rawPath}
{body}
{enableAttachments}
submitInProgress={state === "submit"}
+2
View File
@@ -6,6 +6,7 @@
export let body: string | undefined = undefined;
export let placeholder: string | undefined = undefined;
export let submitCaption: string | undefined = undefined;
export let rawPath: string;
export let enableAttachments: boolean = false;
export let inline: boolean = false;
export let focus: boolean = false;
@@ -31,6 +32,7 @@
{#if state !== "collapsed"}
<ExtendedTextarea
{rawPath}
{inline}
{placeholder}
{submitCaption}
+2 -1
View File
@@ -21,6 +21,7 @@
export let submitCaption: string = "Comment";
export let focus: boolean = false;
export let inline: boolean = false;
export let rawPath: string;
export let body: string = "";
export let submitInProgress: boolean = false;
@@ -195,7 +196,7 @@
</Radio>
{#if preview}
<div class="preview">
<Markdown content={body} embeds={newEmbeds} />
<Markdown {rawPath} content={body} embeds={newEmbeds} />
</div>
{:else}
<input
+1 -1
View File
@@ -23,7 +23,7 @@
// If present, resolve all relative links with respect to this URL
export let linkBaseUrl: string | undefined = undefined;
export let path: string = "/";
export let rawPath: string | undefined = undefined;
export let rawPath: string;
// If present, means we are in a preview context,
// use this for image previews instead of /raw URLs.
export let embeds: EmbedWithOid[] | undefined = undefined;
+1
View File
@@ -129,6 +129,7 @@
{#if createReply}
<div id={`reply-${root.id}`} class="reply">
<CommentToggleInput
{rawPath}
inline
placeholder="Reply to comment"
on:click={toggleReply}
-10
View File
@@ -1,5 +1,3 @@
import type { BaseUrl } from "@httpd-client";
import md5 from "md5";
import bs58 from "bs58";
import twemojiModule from "twemoji";
@@ -10,14 +8,6 @@ export async function toClipboard(text: string): Promise<void> {
await navigator.clipboard.writeText(text);
}
export function getRawBasePath(
id: string,
baseUrl: BaseUrl,
commit: string,
): string {
return `${baseUrl.scheme}://${baseUrl.hostname}:${baseUrl.port}/raw/${id}/${commit}`;
}
export function formatLocationHash(hash: string | null): number | null {
if (hash && hash.match(/^#L[0-9]+$/)) return parseInt(hash.slice(2));
return null;
+4 -3
View File
@@ -30,6 +30,7 @@
export let baseUrl: BaseUrl;
export let expanded: boolean = false;
export let rawPath: (commit?: string) => string;
export let patchId: string;
export let patchState: PatchState;
export let projectHead: string;
@@ -391,7 +392,7 @@
{#if revisionDescription && !first}
<div class="revision-description txt-small">
<Markdown
rawPath={utils.getRawBasePath(projectId, baseUrl, projectHead)}
rawPath={rawPath(projectHead)}
content={revisionDescription} />
</div>
{/if}
@@ -430,8 +431,8 @@
<div class="connector" />
<Thread
enableAttachments
rawPath={utils.getRawBasePath(projectId, baseUrl, projectHead)}
thread={element.inner}
rawPath={rawPath(projectHead)}
{canEditComment}
{editComment}
{createReply}
@@ -474,11 +475,11 @@
class:positive-review={review.verdict === "accept"}
class:negative-review={review.verdict === "reject"}>
<CommentComponent
rawPath={rawPath(projectHead)}
caption={formatVerdict(review.verdict)}
authorId={author}
authorAlias={review.author.alias}
timestamp={review.timestamp}
rawPath={utils.getRawBasePath(projectId, baseUrl, projectHead)}
body={review.summary ?? ""}>
<div slot="icon" style:color={verdictIconColor(review.verdict)}>
{#if review.verdict === "accept"}
+5 -7
View File
@@ -38,9 +38,9 @@
export let baseUrl: BaseUrl;
export let issue: Issue;
export let project: Project;
export let rawPath: (commit?: string) => string;
export let tracking: boolean;
const rawPath = utils.getRawBasePath(project.id, baseUrl, project.head);
const api = new HttpdClient(baseUrl);
const items: [string, IssueState][] = [
@@ -514,6 +514,7 @@
<div slot="description">
{#if issueState !== "read"}
<ExtendedTextarea
rawPath={rawPath(project.head)}
enableAttachments
body={issue.discussion[0].body}
submitCaption="Save"
@@ -543,11 +544,7 @@
<div class="markdown">
<Markdown
content={issue.discussion[0].body}
rawPath={utils.getRawBasePath(
project.id,
baseUrl,
project.head,
)} />
rawPath={rawPath(project.head)} />
</div>
{/if}
<div class="reactions">
@@ -585,7 +582,7 @@
<ThreadComponent
enableAttachments
{thread}
{rawPath}
rawPath={rawPath(project.head)}
canEditComment={partial(
role.isDelegateOrAuthor,
session?.publicKey,
@@ -599,6 +596,7 @@
{/if}
{#if session}
<CommentToggleInput
rawPath={rawPath(project.head)}
placeholder="Leave your comment"
enableAttachments
submit={partial(createComment, session.id)} />
+5 -1
View File
@@ -25,6 +25,7 @@
export let baseUrl: BaseUrl;
export let project: Project;
export let rawPath: (commit?: string) => string;
export let tracking: boolean;
let newEmbeds: EmbedWithOid[] = [];
@@ -191,7 +192,10 @@
{:else if !issueText}
<p class="txt-missing">No description</p>
{:else}
<Markdown embeds={newEmbeds} content={issueText} />
<Markdown
rawPath={rawPath(project.head)}
embeds={newEmbeds}
content={issueText} />
{/if}
</svelte:fragment>
<div class="author" slot="author">
+5 -5
View File
@@ -77,6 +77,7 @@
export let baseUrl: BaseUrl;
export let patch: Patch;
export let rawPath: (commit?: string) => string;
export let project: Project;
export let view: PatchView;
export let tracking: boolean;
@@ -678,6 +679,7 @@
<div class="revision-description">
{#if session && patchState !== "read"}
<ExtendedTextarea
rawPath={rawPath(patch.revisions[0].id)}
body={newDescription}
submitCaption="Save"
submitInProgress={patchState === "submit"}
@@ -699,11 +701,7 @@
{:else if description}
<Markdown
content={description}
rawPath={utils.getRawBasePath(
project.id,
baseUrl,
patch.revisions[0].id,
)} />
rawPath={rawPath(patch.revisions[0].id)} />
{:else}
<span class="txt-missing">No description available</span>
{/if}
@@ -841,6 +839,7 @@
index > 0 ? patch.revisions[index - 1] : undefined}
<RevisionComponent
{baseUrl}
{rawPath}
projectId={project.id}
{timelines}
projectDefaultBranch={project.defaultBranch}
@@ -867,6 +866,7 @@
{#if session && view.name === "activity"}
<div class="connector" />
<CommentToggleInput
rawPath={rawPath(patch.revisions[0].id)}
enableAttachments
placeholder="Leave your comment"
submit={partial(
+2 -6
View File
@@ -3,7 +3,6 @@
import type { BlobResult } from "./router";
import type { Route } from "@app/lib/router";
import * as utils from "@app/lib/utils";
import { HttpdClient } from "@httpd-client";
import Button from "@app/components/Button.svelte";
@@ -17,6 +16,7 @@
import FilePath from "@app/components/FilePath.svelte";
export let baseUrl: BaseUrl;
export let rawPath: (commit?: string) => string;
export let blobResult: BlobResult;
export let branches: string[];
export let path: string;
@@ -208,11 +208,7 @@
{path}
blob={blobResult.blob}
highlighted={blobResult.highlighted}
rawPath={utils.getRawBasePath(
project.id,
baseUrl,
tree.lastCommit.id,
)} />
rawPath={rawPath(tree.lastCommit.id)} />
{:else}
<File>
<FilePath
+20
View File
@@ -113,6 +113,7 @@ export type ProjectLoadedRoute =
revision: string | undefined;
tree: Tree;
path: string;
rawPath: (commit?: string) => string;
blobResult: BlobResult;
tracking: boolean;
};
@@ -146,6 +147,7 @@ export type ProjectLoadedRoute =
params: {
baseUrl: BaseUrl;
project: Project;
rawPath: (commit?: string) => string;
issue: Issue;
tracking: boolean;
};
@@ -165,6 +167,7 @@ export type ProjectLoadedRoute =
params: {
baseUrl: BaseUrl;
project: Project;
rawPath: (commit?: string) => string;
tracking: boolean;
};
}
@@ -183,6 +186,7 @@ export type ProjectLoadedRoute =
params: {
baseUrl: BaseUrl;
project: Project;
rawPath: (commit?: string) => string;
patch: Patch;
view: PatchView;
tracking: boolean;
@@ -263,6 +267,10 @@ export async function loadProjectRoute(
route: ProjectRoute,
): Promise<ProjectLoadedRoute | LoadErrorRoute | NotFoundRoute> {
const api = new HttpdClient(route.node);
const rawPath = (commit?: string) =>
`${route.node.scheme}://${route.node.hostname}:${route.node.port}/raw/${
route.project
}${commit ? `/${commit}` : ""}`;
try {
if (route.resource === "project.source") {
@@ -296,6 +304,7 @@ export async function loadProjectRoute(
params: {
baseUrl: route.node,
project,
rawPath,
issue,
tracking,
},
@@ -314,6 +323,7 @@ export async function loadProjectRoute(
params: {
baseUrl: route.node,
project,
rawPath,
tracking,
},
};
@@ -416,6 +426,10 @@ async function loadTreeView(
route: ProjectTreeRoute,
): Promise<ProjectLoadedRoute> {
const api = new HttpdClient(route.node);
const rawPath = (commit?: string) =>
`${route.node.scheme}://${route.node.hostname}:${route.node.port}/raw/${
route.project
}${commit ? `/${commit}` : ""}`;
const [project, peers, branchMap, tracking] = await Promise.all([
api.project.getById(route.project),
@@ -453,6 +467,7 @@ async function loadTreeView(
peers,
peer: route.peer,
branches: Object.keys(branchMap || {}),
rawPath,
revision: route.revision,
tree,
path,
@@ -559,6 +574,10 @@ async function loadPatchView(
route: ProjectPatchRoute,
): Promise<ProjectLoadedRoute> {
const api = new HttpdClient(route.node);
const rawPath = (commit?: string) =>
`${route.node.scheme}://${route.node.hostname}:${route.node.port}/raw/${
route.project
}${commit ? `/${commit}` : ""}`;
const [project, patch, tracking] = await Promise.all([
api.project.getById(route.project),
api.project.getPatchById(route.project, route.patch),
@@ -615,6 +634,7 @@ async function loadPatchView(
params: {
baseUrl: route.node,
project,
rawPath,
patch,
view,
tracking,