Implement project browsing

This commit is contained in:
Alexis Sellier
2021-06-09 20:53:35 +02:00
parent e7aa71edd1
commit 6d313a4d45
17 changed files with 596 additions and 51 deletions
+89
View File
@@ -0,0 +1,89 @@
<script lang="typescript">
import type { Blob } from "@app/project";
export let blob: Blob;
const lines = (blob.content.match(/\n/g) || []).length;
const lineNumbers = Array(lines).fill(0).map((_, index) => (index + 1).toString());
</script>
<style>
.file-source {
border: 1px solid var(--color-foreground-level-3);
border-radius: 0.5rem;
min-width: var(--content-min-width);
}
header .file-header {
display: flex;
height: 3rem;
align-items: center;
padding: 0 1rem;
color: var(--color-foreground);
border-width: 1px 1px 0 1px;
border-color: var(--color-foreground-subtle);
border-style: solid;
border-top-left-radius: 0.25rem;
border-top-right-radius: 0.25rem;
}
header .file-name {
font-weight: normal;
}
.line-numbers {
color: var(--color-foreground-subtle);
font-family: var(--font-family-sans-serif);
text-align: right;
user-select: none;
padding: 0 1rem 0.5rem 1rem;
}
.code {
padding-bottom: 0.5rem;
overflow-x: auto;
}
.container {
display: flex;
border: 1px solid var(--color-foreground-subtle);
border-top-style: dashed;
}
.no-scrollbar {
scrollbar-width: none;
}
.no-scrollbar::-webkit-scrollbar {
display: none;
}
</style>
<div>
<div class="file-source">
<header>
<div class="file-header" data-cy="file-header">
<span class="file-name">
<span>{blob.path.split("/").join(" / ")}</span>
</span>
</div>
</header>
<div class="container">
{#if blob.binary}
👀 Binary content
{:else}
<pre class="line-numbers">
{@html lineNumbers.join("\n")}
</pre>
<pre
class="code no-scrollbar">
{#if blob.html}
{@html blob.content}
{:else}
{blob.content}
{/if}
</pre>
{/if}
</div>
</div>
</div>
-35
View File
@@ -1,35 +0,0 @@
import type { Config } from '@app/config';
export interface Project {
id: string
anchor: {
stateHash: string
stateHashFormat: string
}
}
export interface User {
urn: string
avatar: { emoji: string, background: { r: number, g: number, b: number } }
}
export interface Meta {
name: string
description: string
maintainers: User[]
}
export async function getMetadata(urn: string, config: Config): Promise<Meta | null> {
const response = await fetch(`${config.seed.url}/projects/${urn}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return await response.json();
}
+19
View File
@@ -0,0 +1,19 @@
<script lang="typescript">
import { Route } from "svelte-routing";
import View from '@app/base/projects/View.svelte';
import type { Config } from '@app/config';
export let config: Config;
const joinPaths = (path: string, glob: string): string => {
return glob.length > 0 ? [path, glob].join("/") : path;
};
</script>
<Route path="/projects/:urn/:commit/:path/*" let:params>
<View {config} urn={params.urn} commit={params.commit} path={joinPaths(params.path, params['*'])} />
</Route>
<Route path="/projects/:urn/:commit" let:params>
<View {config} urn={params.urn} commit={params.commit} path="/" />
</Route>
+37
View File
@@ -0,0 +1,37 @@
<script lang="typescript">
import { createEventDispatcher } from "svelte";
import type { Tree } from "@app/project";
import { ObjectType } from "@app/project";
import File from './Tree/File.svelte';
import Folder from './Tree/Folder.svelte';
export let fetchTree: (path: string) => Promise<Tree>;
export let path: string;
export let tree: Tree;
const dispatch = createEventDispatcher();
const onSelect = ({ detail: path }: { detail: string }): void => {
dispatch("select", path);
};
</script>
{#each tree.entries as entry (entry.path)}
{#if entry.info.objectType === ObjectType.Tree}
<Folder
{fetchTree}
name={entry.info.name}
prefix={`${entry.path}/`}
currentPath={path}
on:select={onSelect}
/>
{:else}
<File
active={entry.path === path}
loading={false}
name={entry.info.name}
on:click={() => onSelect({ detail: entry.path })}
/>
{/if}
{/each}
+55
View File
@@ -0,0 +1,55 @@
<script lang="typescript">
import Loading from '@app/Loading.svelte';
export let active: boolean;
export let loading: boolean;
export let name: string;
</script>
<style>
.file {
color: var(--color-foreground-90);
border-radius: 0.25rem;
cursor: pointer;
display: flex;
flex: 1;
line-height: 1.5em;
margin: 0.125rem 0;
padding: 0.25rem;
width: 100%;
}
.file:hover {
background-color: var(--color-foreground-background);
}
.file.active {
color: var(--color-foreground) !important;
background-color: var(--color-foreground-background);
}
.spinner {
align-items: center;
display: flex;
justify-content: center;
height: 24px;
width: 24px;
}
.name {
margin-left: 0.25rem;
user-select: none;
white-space: nowrap;
}
</style>
<div class="file" class:active on:click>
{#if loading}
<div class="spinner">
<Loading small />
</div>
{:else}
<!-- Nothing -->
{/if}
<span class="name">{name}</span>
</div>
+89
View File
@@ -0,0 +1,89 @@
<script lang="typescript">
import { createEventDispatcher } from "svelte";
import Loading from '@app/Loading.svelte';
import type { Tree } from "@app/project";
import { ObjectType } from "@app/project";
import File from "./File.svelte";
export let fetchTree: (path: string) => Promise<Tree>;
export let name: string;
export let prefix: string;
export let currentPath: string;
let expanded = currentPath.indexOf(prefix) === 0;
let tree: Promise<Tree> | null = expanded ? fetchTree(prefix) : null;
const dispatch = createEventDispatcher();
const onSelectFile = ({ detail: path }: { detail: string }) =>
dispatch("select", path);
const onClick = () => {
expanded = !expanded;
if (expanded) {
tree = fetchTree(prefix);
}
};
</script>
<style>
.folder {
display: flex;
cursor: pointer;
padding: 0.25rem;
margin: 0.125rem 0;
color: var(--color-foreground-level-6);
user-select: none;
line-height: 1.5rem;
white-space: nowrap;
}
.folder:hover {
background-color: var(--color-foreground-background);
border-radius: 0.25rem;
}
.folder-name {
margin-left: 0.25rem;
color: var(--color-light);
}
.container {
padding-left: 0.5rem;
margin: 0;
}
</style>
<div class="folder" on:click={onClick}>
<span class="folder-name">{name}/</span>
</div>
<div class="container">
{#if expanded}
{#await tree}
<Loading small center />
{:then tree}
{#if tree}
{#each tree.entries as entry (entry.path)}
{#if entry.info.objectType === ObjectType.Tree}
<svelte:self
{fetchTree}
name={entry.info.name}
on:select={onSelectFile}
prefix={`${entry.path}/`}
{currentPath} />
{:else}
<File
active={entry.path === currentPath}
loading={false}
name={entry.info.name}
on:click={() => {
onSelectFile({ detail: entry.path });
}} />
{/if}
{/each}
{/if}
{/await}
{/if}
</div>
+143
View File
@@ -0,0 +1,143 @@
<script lang="typescript">
import { onMount } from 'svelte';
import { navigate } from 'svelte-routing';
import type { Config } from '@app/config';
import * as proj from '@app/project';
import Loading from '@app/Loading.svelte';
import Tree from './Tree.svelte';
import Blob from './Blob.svelte';
export let urn: string;
export let commit: string;
export let config: Config;
export let path: string;
let meta: "loading" | proj.Meta | null = null;
let blob: Promise<proj.Blob | null> | null = null;
const fetchTree = async (path: string) => {
return proj.getTree(urn, commit, path, config);
};
const onSelect = async ({ detail: path }: { detail: string }) => {
navigate(`/projects/${urn}/${commit}/${path}`);
};
onMount(async () => {
meta = "loading";
meta = await proj.getMetadata(urn, config);
});
$: if (path === "/") {
blob = proj.getReadme(urn, commit, config);
} else {
blob = proj.getBlob(urn, commit, path, config);
}
</script>
<style>
main {
width: 100%;
padding: 4rem 0;
}
main > header {
margin-bottom: 4rem;
padding: 0 8rem;
}
.title {
font-size: 2.25rem;
margin-bottom: 0.5rem;
}
.anchor {
display: inline-block;
font-size: 0.75rem;
font-family: var(--font-family-monospace);
color: var(--color-secondary);
background-color: var(--color-secondary-background);
padding: 0.75rem;
border-radius: 0.25rem;
}
.urn {
font-family: var(--font-family-monospace);
font-size: 0.75rem;
color: var(--color-foreground-faded);
}
.description {
margin: 1rem 0 1.5rem 0;
}
.center-content {
margin: 0 auto;
max-width: var(--content-max-width);
min-width: var(--content-min-width);
}
.container {
display: flex;
width: inherit;
margin-bottom: 4rem;
padding: 0 2rem 0 8rem;
}
.column-left {
display: flex;
flex-direction: column;
padding-right: 1rem;
}
.column-right {
display: flex;
flex-direction: column;
padding-left: 1rem;
min-width: var(--content-min-width);
width: 100%;
}
.source-tree {
overflow-x: auto;
}
</style>
<main>
<header>
{#if meta === "loading"}
<Loading small />
{:else if meta}
<div class="title bold">{meta.name}</div>
<div class="urn">{urn}</div>
<div class="description">{meta.description}</div>
{/if}
<div class="anchor">
commit {commit}
</div>
</header>
<div class="container center-content">
<div class="column-left">
<div class="source-tree">
{#await proj.getTree(urn, commit, "/", config)}
Loading..
{:then tree}
<Tree {tree} {path} {fetchTree} on:select={onSelect} />
{:catch err}
{err}
{/await}
</div>
</div>
<div class="column-right">
{#await blob}
<Loading small center />
{:then blob}
{#if blob}
<Blob {blob} />
{:else}
<!-- Project has no README -->
{/if}
{:catch}
<!-- TODO: Handle error -->
{/await}
</div>
</div>
</main>
+4 -6
View File
@@ -1,12 +1,10 @@
<script type="typescript">
import { onMount } from 'svelte';
import type { Config } from '@app/config';
import * as proj from '@app/project';
import Loading from '@app/Loading.svelte';
import Blockies from '@app/Blockies.svelte';
import { getMetadata } from './Project';
import type { Project, Meta } from './Project';
enum Status { Loading, Loaded, Error }
type State =
@@ -14,15 +12,15 @@
| { status: Status.Loaded }
| { status: Status.Error, error: string };
export let project: Project;
export let project: proj.Project;
export let config: Config;
let state: State = { status: Status.Loading };
let meta: Meta | null = null;
let meta: proj.Meta | null = null;
onMount(async () => {
try {
const result = await getMetadata(project.id, config);
const result = await proj.getMetadata(project.id, config);
state = { status: Status.Loaded };
meta = result;
} catch (err) {