-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathmetadata-decoder.svelte
More file actions
46 lines (37 loc) · 1.18 KB
/
metadata-decoder.svelte
File metadata and controls
46 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<script lang="ts">
import type { Payload } from '$lib/types';
import { decodeUserMetadata } from '$lib/utilities/decode-payload';
export let value: Payload | undefined = undefined;
export let fallback: string = '';
export let prefix: string = '';
export let onDecode: (decodedValue: string) => void | undefined = undefined;
const maxLength = 120;
let decodedValue = '';
const setPrefix = (metadata: string) => {
if (prefix) {
metadata = `${prefix} • ${metadata}`;
if (metadata.length < maxLength) return metadata;
return metadata.slice(0, maxLength) + '...';
}
return metadata;
};
$: decodePayload = async (_value: Payload | undefined) => {
if (!_value) return fallback;
if (decodedValue) return decodedValue;
const metadata = await decodeUserMetadata(_value);
if (typeof metadata === 'string') {
decodedValue = setPrefix(metadata);
if (onDecode) {
onDecode(decodedValue);
}
return decodedValue;
}
decodedValue = fallback;
return fallback;
};
</script>
{#await decodePayload(value) then metadata}
<slot decodedValue={metadata} />
{:catch}
<slot decodedValue={fallback} />
{/await}