Something went wrong. Try again.
COSMO is a tool for exploring astronomical data from the Gaia catalog. tedius.tngl.sh/cosmo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470<script lang="ts"> import { matchColumns, type ColumnMatch, type Table } from "@tedius-npm/newman"; import { proyectState } from "../State.svelte"; import Block from "./Block.svelte"; import Button from "./Button.svelte"; import DropFile from "./DropFile.svelte"; import FullBlock from "./FullBlock.svelte"; import MiniTable from "./MiniTable.svelte"; import Scroll from "./Scroll.svelte"; import Spinner from "./Spinner.svelte"; import Toggle from "./Toggle.svelte";
// safe unwrap: AddFile lives in Workspace, which mounts once a project is loaded const base = $derived(proyectState.table!);
const EXACT_COLOR = "var(--green)"; const CASE_COLOR = "var(--blue)"; const NONE_COLOR = "var(--gray)"; // thinner than the default, so the small boxes keep room for their text const BAR_WIDTH = 6;
const STEPS = [ "Pick a file", "We match its columns by name", "Its rows go below yours", ];
let fileName = $state<string | null>(null); let incoming = $state.raw<Table | null>(null); let error = $state<string | null>(null); let includeCase = $state(false); let adding = $state(false);
function reset() { fileName = null; incoming = null; error = null; includeCase = false; adding = false; }
function onLoad(table: Table, file: File) { reset(); fileName = file.name; incoming = table; }
// the last step is the Add click itself, so it never shows as reached const reached = $derived(incoming ? 2 : 1);
// A calculated column takes no file's values: its new rows are calculated const computedCount = $derived(proyectState.computed.length); const matchable = $derived({ ...base, columns: base.columns.filter((c) => !proyectState.computedAt(c.index)), }); const matches = $derived(incoming ? matchColumns(matchable, incoming) : []); const exact = $derived(matches.filter((m) => m.kind === "exact")); const caseOnly = $derived(matches.filter((m) => m.kind === "case")); const different = $derived(matches.filter((m) => m.kind === "none")); const used = $derived( [...exact, ...(includeCase ? caseOnly : [])].sort( (a, b) => a.base! - b.base!, ), );
/** Incoming column feeding each base column, by base index. */ const sources = $derived.by(() => { const out: (number | null)[] = base.columns.map(() => null); for (const m of used) out[m.base!] = m.incoming; return out; });
const emptyCount = $derived(base.columns.length - computedCount - used.length);
/** The used incoming columns as they will land: under the base's names, in base order. */ const previewColumns = $derived( used.map((m) => ({ ...incoming!.columns[m.incoming], name: base.columns[m.base!].name, })), );
/** Used columns whose cells may not fit: text into a number, or anything else into an integer. */ const lossy = $derived( used.filter((m) => { const from = incoming!.columns[m.incoming].type; const to = base.columns[m.base!].type; return from !== to && (from === "utf8" || to === "i64"); }), );
const headerColors = $derived( used.map((m) => (m.kind === "case" ? CASE_COLOR : EXACT_COLOR)), );
/** Incoming names of a group, with the project's name when it differs. */ function names(list: ColumnMatch[]): string { return list .map((m) => { const own = incoming!.columns[m.incoming].name; const target = m.base === null ? own : base.columns[m.base].name; return own === target ? own : `${own} as ${target}`; }) .join(", "); }
function count(n: number): string { return n === 1 ? "1 column" : `${n} columns`; }
async function add(close: () => void) { // read before the wait, so a toggle flipped meanwhile does not change what is added const table = incoming; const feeds = sources; if (!table || used.length === 0) return; adding = true; // two frames, so the spinner is painted before the merge blocks the main thread await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)), ); // Change File or reopening the dialog ran reset() meanwhile, which cancels the add if (!adding) return; const res = proyectState.appendRows(table, feeds); adding = false; if (!res.ok) { error = res.error.message; return; } reset(); close(); }</script>
<div> <FullBlock color="var(--violet)" barWidth={BAR_WIDTH} tooltip="Do you want to add more data from another file?" tooltipPos="left" onopen={reset} > {#snippet button()} Add {/snippet} {#snippet children(close)} <div class="add"> <h2>Add Data</h2>
<ol class="steps"> {#each STEPS as step, i} <li class:reached={i < reached} class:current={i === reached - 1} aria-current={i === reached - 1 ? "step" : undefined} > <span class="marker">{i + 1}</span> <span class="step">{step}</span> </li> {/each} </ol>
{#if !incoming} <div class="drop"> <DropFile title="New File" {onLoad} /> </div>
<p class="muted center"> Your current rows are never changed. </p> {:else} <Block color="var(--violet)" title="New File"> <div class="file"> <div class="file-text"> <strong>{fileName}</strong> <span class="muted" >{incoming.rowCount} rows, {count( incoming.columns.length, )}</span > </div> <Button color="var(--violet)" onclick={reset} >Change File</Button > </div> </Block>
<div class="groups"> <Block color={EXACT_COLOR} title="Identical" tooltip="Same name as one of your columns, so their values land right in it." > <strong>{count(exact.length)}</strong> {#if exact.length > 0} <Scroll class="names" width={BAR_WIDTH}> <p class="muted name-list"> {names(exact)} </p> </Scroll> {/if} </Block>
{#if caseOnly.length > 0} <Block color={CASE_COLOR} title="Differ in Case" tooltip="Same name as one of your columns but for upper and lower case. Turn them on to use them too." > <Toggle label="Use {count(caseOnly.length)}" bind:value={includeCase} offColor={NONE_COLOR} onColor={CASE_COLOR} /> <Scroll class="names" width={BAR_WIDTH}> <p class="muted name-list"> {names(caseOnly)} </p> </Scroll> </Block> {/if}
<Block color={NONE_COLOR} title="Ignored" tooltip="No column of yours has this name, so they are left out." > <strong>{count(different.length)}</strong> {#if different.length > 0} <Scroll class="names" width={BAR_WIDTH}> <p class="muted name-list"> {names(different)} </p> </Scroll> {/if} </Block> </div>
{#if used.length > 0} <Block color="var(--black)" title="Preview"> <MiniTable table={{ ...incoming, columns: previewColumns }} barWidth={BAR_WIDTH} maxRows={5} {headerColors} /> </Block> {#if emptyCount > 0} <p class="muted"> {emptyCount} of your columns stay empty on the new rows. </p> {/if} {#if computedCount > 0} <p class="muted"> {count(computedCount)} calculated by COSMO get calculated for the new rows too. </p> {/if} {#if lossy.length > 0} <p class="error"> A different type than yours: {names(lossy)}. Cells that do not fit stay empty. </p> {/if} {:else} <p class="error"> No column matches this project, so there is nothing to add. </p> {/if}
{#if error} <p class="error" role="alert">{error}</p> {/if}
{#if adding} <div class="adding"> <Spinner label="Adding {incoming.rowCount} rows..." size={3} /> </div> {:else} <div class="actions"> <Button color="var(--red)" onclick={close} >Cancel</Button > <Button color="var(--green)" disabled={used.length === 0} onclick={() => add(close)} > Add {incoming.rowCount} Rows </Button> </div> {/if} {/if} </div> {/snippet} </FullBlock></div>
<style> .add { display: flex; flex-direction: column; gap: 1.2em; width: min(52em, 80vw); }
h2 { color: var(--violet); margin: 0; }
.center { text-align: center; }
/* The Step.svelte marker laid out as a row, joined by a rule */ .steps { display: flex; margin: 0; padding: 0; list-style: none; --marker: 2em; }
.steps li { flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; gap: 0.5em; padding: 0 0.5em; text-align: center; }
.steps li:not(:last-child)::after { content: ""; position: absolute; top: calc(var(--marker) / 2 - var(--border-stroke) / 2); left: calc(50% + var(--marker) / 2 + 0.4em); right: calc(-50% + var(--marker) / 2 + 0.4em); height: var(--border-stroke); border-radius: var(--border-stroke); background: var(--lightgray); }
.steps li.reached:not(:last-child)::after { background: var(--violet); }
.steps li.current::after { background: color-mix( in srgb, var(--violet) var(--transparent-opacity), transparent ); }
.marker { width: var(--marker); height: var(--marker); box-sizing: border-box; border-radius: 50%; border: var(--border-stroke) solid var(--violet); background: var(--white); color: var(--violet); font-weight: bold; display: flex; align-items: center; justify-content: center; }
.reached .marker { background: var(--violet); color: var(--white); }
.step { font-size: 0.85em; color: var(--gray); }
.reached .step { color: var(--black); }
.current .step { color: var(--violet); font-weight: bold; }
/* narrower than the dialog so its hover growth stays inside the FullBlock */ .drop { display: grid; align-self: center; width: min(28em, 90%); }
.file { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1em; }
.file-text { display: flex; flex-direction: column; gap: 0.2em; min-width: 0; }
.file-text strong { color: var(--violet); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.groups { display: flex; flex-wrap: wrap; gap: 1.2em var(--padding); }
.groups > :global(*) { flex: 1 1 14em; min-width: 0; }
.groups :global(.content) { text-align: justify; display: flex; flex-direction: column; gap: 0.6em; min-height: 7em; padding: calc(1.5 * var(--padding)); }
/* every name stays reachable without a tooltip */ .groups :global(.names) { max-height: 7.5em; }
.name-list { overflow-wrap: anywhere; }
.adding { display: flex; justify-content: center; }
.actions { display: flex; justify-content: space-between; gap: 1em; }</style>