Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Surrealist mini embeds #922

Merged
merged 4 commits into from
Oct 8, 2024
Merged
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
39 changes: 24 additions & 15 deletions src/components/SurrealistMini.astro
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
---
type Props = {
dataset?: 'surreal-deal';
url?: string;
dataset?: 'surreal-deal-store';
setup?: string;
query?: string;
variables?: string | Record<string, unknown>;
theme?: 'automatic' | 'light' | 'dark';
url?: string;
theme?: 'auto' | 'light' | 'dark';
height?: string;
compact?: boolean;
transparent?: boolean;
};

const { url, height, ...props } = Astro.props;
const endpoint = new URL(url ?? 'https://surrealist.app/mini');
const config = new URLSearchParams(endpoint.search);

const values = {
compact: true,
theme: 'auto',
...props,
};

const { url: _url, height, ...props } = Astro.props;
for (const [k, v] of Object.entries(values)) {
if (v === false) {
config.delete(k);
} else {
config.set(k, typeof v === 'string' ? v : JSON.stringify(v));
}
}

props.variables =
typeof props.variables === 'string'
? props.variables
: JSON.stringify(props.variables);
const stringified = Object.fromEntries(
Object.entries(props).map(([k, v]) => [k, (v ?? '').toString()])
);
const search = new URLSearchParams(stringified).toString();
// biome-ignore lint/style/useTemplate: We join here because otherwise it becomes a string literal nesting mess
const url = (_url ?? `https://surrealist.app/mini?${search}`) + '&compact';
endpoint.search = config.toString();
---

<iframe
src={url}
src={endpoint.toString()}
style={{
width: '100%',
height: height ?? '600px',
Expand Down
5 changes: 3 additions & 2 deletions src/pages/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import BaseLayout from '@components/layout/BaseLayout.astro';
While you're here, try some queries on Surrealist
</p>
<SurrealistMini
url="https://surrealist.app/mini?dataset=surreal-deal&query=%2F%2Fshow+complete+information+for+database+%0AINFO+FOR+DB%3B+%0A%0A%2F%2F+Select+all+from+artist+table+%0ASELECT+*+FROM+artist%3B%0A%0A%2F%2F+Select+specific+field+within+table+%0ASELECT+address.city+FROM+artist+%0A%0A%2F%2F+Add+your+queries+%F0%9F%98%83%0A%0A"
/>
url="https://surrealist.app/mini?query=%2F%2F+Select+all+product+names%0ASELECT+name+FROM+product%3B%0A%0A%2F%2F+Select+most+recently+updated+products%0ASELECT+*+FROM+product+ORDER+BY+time.updated_at+DESC+LIMIT+3%3B%0A%0A%2F%2F+Write+your+own+queries%21+%F0%9F%98%83%0A&dataset=surreal-deal-store"
compact={false}
/>
</div>
</div>
</BaseLayout>