Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ declare global {
table?: {
/** List of indexes for the table. If the index is multivariate, add a '[]' at the end of the string */
indexes: string[];
/** A function to update a _search multivariate, given the (raw, ie as-stored) columns of the object */
searchIndex?: (raw: Record<string, unknown>) => string[];
};
};
}
Expand Down
67 changes: 29 additions & 38 deletions src/lib/MetadataCombobox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import { databaseHandle } from './idb.svelte.js';
import LearnMoreLink from './LearnMoreLink.svelte';
import Markdown from './Markdown.svelte';
import { metadataOptionsOf } from './metadata/index.js';
import {Tables} from './database.js';
import { metadataOption, metadataOptionsOf } from './metadata/index.js';
import MetadataCascadesTable from './MetadataCascadesTable.svelte';
import { namespaceOfMetadataId } from './schemas/metadata.js';
import { uiState } from './state.svelte';
import { cancellable, compareBy, readableOn } from './utils.js';
import { cancellable, compareBy, orEmpty, orEmpty2, readableOn } from './utils.js';

/**
* @import {WithoutChildrenOrChild} from 'bits-ui';
Expand Down Expand Up @@ -64,43 +65,33 @@
} = $props();

const protocolId = $derived(namespaceOfMetadataId(metadata.id));
const db = $derived(idb.databaseHandle());

const showConfidences = $derived(
Object.keys(confidences).length > 0 &&
Object.values(confidences).some((conf) => conf > 0 && conf < 1)
);

let options = $derived(precomputedOptions ?? []);

const loadingOptions = $derived(!precomputedOptions && options.length === 0);
const optionsLoader = cancellable(
async (
signal,
/** @type {NamespacedMetadataId} */ metadataId,
/** @type {boolean} */ precomputed
) => {
if (precomputed) return;
if (!uiState.currentProtocolId) return;
console.info('Fetching options for metadata', metadataId);
signal.throwIfAborted();
options = await metadataOptionsOf(
databaseHandle(),
uiState.currentProtocolId,
metadataId
);
console.info('Fetched options for metadata', metadataId, options);
}
);
/** @type {DB.MetadataEnumVariant|undefined} */
let currentlySelectedOption = $state();

$effect(() => {
const loader = optionsLoader(metadata.id, Boolean(precomputedOptions));

loader.do();
return loader.cancel;
(async () => {
if (!value) {
currentlySelectedOption = undefined;
return;
}

currentlySelectedOption = await metadataOption(db, metadata.id, value).then(opt => {
if (!opt) return undefined
return Tables.MetadataOption.assert(opt)
}).catch((err) => {
console.error('Failed to get currently selected option from database', err);
return undefined;
});
})();
});

const hasImages = $derived(options.some((opt) => opt.image));

/**
* @param {DB.MetadataEnumVariant} a
* @param {DB.MetadataEnumVariant} b
Expand All @@ -123,29 +114,29 @@
}
</script>

<div class="metadata-combobox" class:wide-docs={hasImages} class:multiple>
<div class="metadata-combobox wide-docs" class:multiple>
<Combobox
// Put disabled options last
items={options.toSorted(compareBy((opt) => (optionIsDisabled(opt) ? 1 : -1)))}
items={orEmpty2(currentlySelectedOption, (o) => o)}
{value}
values={multiple ? [value, ...Object.keys(alternatives)] : undefined}
{multiple}
bind:open
bind:focuser
sorter={compareByConfidence}
searcher={nameMatches}
sorter={() => 0}
searcher={(k) => k}
suggestions={async (query) => {
const results = await idb.search('MetadataOption', query);
return results.filter((result) => result.metadataId === metadata.id);
}}
viewport-testid="metadata-combobox-viewport"
{inputProps}
{contentProps}
{...restProps}
>
{#snippet searchbox({ focusSetter, ...props })}
<div class="searchbox">
{#if loadingOptions}
<LoadingText value={Loading}>Chargement</LoadingText>
{:else}
<input {...props} {@attach focusSetter} />
{/if}
<input {...props} {@attach focusSetter} />
</div>
{/snippet}
{#snippet listItem({ selected, ...item })}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/MetadataInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
$effect(() => {
const loader = optionsLoader(
definition.id,
Boolean(precomputedOptions) || definition.type !== 'enum'
Boolean(precomputedOptions) || definition.type !== 'enum' || !isCompactEnum
);

loader.do();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/WorldLocationCombobox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
value={coordsToKey(value)}
sorter={() => 0}
searcher={(label) => label}
onValueChange={(val) => onblur(keyToCoords(val))}
onValueChange={async (val) => onblur(keyToCoords(val))}
suggestions={async (search) => {
const coords = Coords(search);

Expand Down
97 changes: 87 additions & 10 deletions src/lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ import {
Protocol as ProtocolSchema,
} from './schemas/protocols.js';
import { Session as SessionSchema } from './schemas/sessions.js';
import { clamp } from './utils.js';
import { clamp, ensureArray } from './utils.js';


/**
* Normalize a search index string or a search query
* - Lowercase it
* - Normalize unicode characters
* - Remove accents
* @param {string} s
*/
export function normalizeSearchStrings(s) {
return s
.normalize('NFKD')
.replaceAll(/\p{Diacritic}/gu, '')
.toLowerCase();
}


/**
* Generate an ID for a given table
Expand Down Expand Up @@ -63,7 +79,7 @@ if (import.meta.vitest) {
}

const ImageFile = table(
['id', 'sessionId'],
['id', 'sessionId', 'filename'],
type({
/** ID of the associated Image object */
id: ID,
Expand All @@ -76,7 +92,7 @@ const ImageFile = table(
);

const ImagePreviewFile = table(
['id', 'sessionId'],
['id', 'sessionId', 'filename'],
type({
/** ID of the associated Image object */
id: ID,
Expand Down Expand Up @@ -105,19 +121,65 @@ const MetadataValueFile = table(
})
);

const Image = table(['id', 'addedAt', 'sessionId'], ImageSchema);
const Image = table(
['id', 'addedAt', 'sessionId', '_search[]'],
ImageSchema.and({
/** Indexes for searching */
_search: ['string[]', '=', () => []],
}),
{
searchIndex({ filename, id }) {
return [filename, id];
},
}
);

const Observation = table(['id', 'addedAt', 'sessionId'], ObservationSchema);
const Observation = table(
['id', 'addedAt', 'sessionId', 'images[]', '_search[]'],
ObservationSchema.and({
/** Indexes for searching */
_search: ['string[]', '=', () => []],
}),
{
searchIndex({ label, id, images }) {
return [label, id, ...images];
},
}
);

const Session = table(['id', 'remoteId'], SessionSchema);
const Session = table(
['id', 'remoteId', 'protocol', '_search[]'],
SessionSchema.and({
/** Indexes for searching */
_search: ['string[]', '=', () => []],
}),
{
searchIndex({ name, id, remoteId, description, protocol }) {
return [name, id, remoteId, description, protocol];
},
}
);

const Metadata = table('id', MetadataSchema.omit('options'));
const MetadataOption = table(
['id', 'kobocollectId', '_narrowableIn[]'],
['id', 'kobocollectId', 'metadataId', '_narrowableIn[]', '_search[]'],
MetadataEnumVariant.and({
id: [/\w+__\w+:\w+/, '@', 'ID of the form namespaced_metadata_id:key'],
metadataId: NamespacedMetadataID,
})
_search: ['string[]', '=', () => []],
}),
{
searchIndex({ key, label, synonyms, description, kobocollect, learnMore }) {
return [
key,
label,
learnMore,
...(synonyms ?? []),
description,
...ensureArray(kobocollect ?? []),
];
},
}
);
const Protocol = table('id', ProtocolSchema);

Expand Down Expand Up @@ -302,14 +364,29 @@ export const Tables = {
* Indexes with '[]' at the end are multivariate indexes, meaning they index arrays of values instead of single values.
* @param {Schema} schema
* @template {import('arktype').Type} Schema
* @param {object} [options]
* @param {(object: Schema['inferIn']) => undefined | string | Array<string|undefined|null>} [options.searchIndex] Undefined means empty array, string means single-element array. Function to compute a value for a _search column
* @returns
*/
function table(keyPaths, schema) {
function table(keyPaths, schema, options) {
const expandedKeyPaths = Array.isArray(keyPaths)
? keyPaths.map((keyPath) => keyPath)
: [keyPaths];

return schema.configure({ table: { indexes: expandedKeyPaths } });
return schema.configure({
table: {
indexes: expandedKeyPaths,
searchIndex(object) {
if (!options?.searchIndex) return [];
const result = options.searchIndex(object);
if (!result) return [];

return ensureArray(result)
.map((s) => normalizeSearchStrings(s ?? ''))
.filter((v) => Boolean(v));
},
},
});
}

/**
Expand Down
Loading