Files
radicle-explorer/src/base/projects/BranchSelector.svelte
T
2022-01-18 11:01:58 +01:00

119 lines
3.2 KiB
Svelte

<script lang="ts">
import { createEventDispatcher } from "svelte";
import { Info, getOid } from "@app/project";
import { formatCommit, isOid } from "@app/utils";
import Dropdown from "@app/Dropdown.svelte";
export let branches: [string, string][];
export let project: Info;
export let revision: string;
export let toggleDropdown: (input: string) => void;
export let branchesDropdown = false;
const dispatch = createEventDispatcher();
const switchBranch = (name: string) => {
dispatch("revisionChanged", name);
};
const showSelector = branches.length > 1;
// Sort branches array alphabetically
const sortBranches = ([firstBranchName,]: [string, string], [secondBranchName,]: [string, string]) => {
if (firstBranchName < secondBranchName) return -1;
if (firstBranchName > secondBranchName) return 1;
return 0;
};
let branchLabel: string;
branches = branches.sort(sortBranches);
$: commit = getOid(project.head, revision, branches);
$: isLabel = commit == project.head || !isOid(revision);
$: if (commit == project.head) {
branchLabel = project.meta.defaultBranch;
} else if (!isOid(revision)) {
branchLabel = revision;
}
</script>
<style>
.commit {
display: flex;
align-items: center;
justify-content: center;
font-family: var(--font-family-monospace);
}
.commit .branch {
cursor: pointer;
padding: 0.5rem 0.75rem;
color: var(--color-secondary);
background-color: var(--color-secondary-background);
border-radius: 0.25rem 0 0 0.25rem;
}
.commit .branch.not-allowed {
cursor: not-allowed;
}
.branch:hover:not(.not-allowed) {
background-color: var(--color-foreground-background-lighter);
}
.commit .hash {
display: inline-block;
color: var(--color-secondary);
background-color: var(--color-secondary-background-darker);
padding: 0.5rem 0.75rem;
border-radius: inherit;
}
.hidden {
display: none;
}
.stat {
font-family: var(--font-family-monospace);
padding: 0.5rem 0.75rem;
background: var(--color-foreground-background);
}
</style>
<div class="commit">
<!-- Check for branches listing feature -->
{#if branches.length > 0}
<span>
<div
class="stat branch"
class:not-allowed={!showSelector}
class:hidden={!isLabel}
on:click={() => showSelector && toggleDropdown("branch")}
>
{branchLabel}
</div>
<Dropdown
items={branches.map(([name,]) => name)}
visible={branchesDropdown}
on:select={(e) => switchBranch(e.detail)} />
</span>
<div class="hash desktop">
{#if isLabel}
{formatCommit(commit)}
{:else}
{commit}
{/if}
</div>
<div class="hash mobile">
{formatCommit(commit)}
</div>
<!-- If there is no branch listing available, show default branch name if commit is head and else show entire commit -->
{:else if commit === project.head}
<div class="stat branch not-allowed">
{project.meta.defaultBranch}
</div>
<div class="hash">
{formatCommit(commit)}
</div>
{:else}
<div class="hash desktop">
{commit}
</div>
<div class="hash mobile">
{formatCommit(commit)}
</div>
{/if}
</div>