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

Add upload favicon settings section #6638

Merged
merged 4 commits into from
Feb 12, 2025
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
1 change: 0 additions & 1 deletion web-admin/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
crossorigin
/>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<!-- Set the mobile viewport width -->
<meta
name="viewport"
Expand Down
10 changes: 10 additions & 0 deletions web-admin/src/features/errors/error-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,13 @@ export function createErrorPagePropsFromRoutingError(
detail: errorMessage,
};
}

// Temporary function to get the correct error message.
// We get AxiosError<RpcStatus> but type is RpcStatus
// TODO: fix the root types in orval
export function getRpcErrorMessage(
error: RpcStatus | undefined,
): string | undefined {
const mappedError = error as unknown as AxiosError<RpcStatus> | undefined;
return mappedError?.response?.data?.message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<script lang="ts">
import { invalidate } from "$app/navigation";
import {
createAdminServiceUpdateOrganization,
getAdminServiceGetOrganizationQueryKey,
} from "@rilldata/web-admin/client";
import { getRpcErrorMessage } from "@rilldata/web-admin/features/errors/error-utils";
import SettingsContainer from "@rilldata/web-admin/features/organizations/settings/SettingsContainer.svelte";
import UploadImagePopover from "@rilldata/web-admin/features/organizations/settings/UploadImagePopover.svelte";
import { Button } from "@rilldata/web-common/components/button";
import { queryClient } from "@rilldata/web-common/lib/svelte-query/globalQueryClient";

export let organization: string;
export let organizationFaviconUrl: string | undefined;

const orgUpdater = createAdminServiceUpdateOrganization();
$: ({ error, isLoading, mutateAsync } = $orgUpdater);

async function onSave(assetId: string) {
await mutateAsync({
name: organization,
data: {
faviconAssetId: assetId,
},
});
void queryClient.invalidateQueries(
getAdminServiceGetOrganizationQueryKey(organization),
);
void invalidate("root");
}

async function onRemove() {
await mutateAsync({
name: organization,
data: {
faviconAssetId: "",
},
});
void queryClient.invalidateQueries(
getAdminServiceGetOrganizationQueryKey(organization),
);
void invalidate("root");
}
</script>

<SettingsContainer title="Favicon" suppressFooter={!organizationFaviconUrl}>
<div slot="body" class="flex flex-col gap-y-2">
<div>
Click to upload your favicon and customize Rill for your organization.
</div>
<UploadImagePopover
imageUrl={organizationFaviconUrl}
accept="image/png, image/ico, image/x-ico, image/icon, image/x-icon"
label="favicon"
{organization}
loading={isLoading}
error={getRpcErrorMessage(error)}
{onSave}
{onRemove}
>
<img src="/favicon.png" alt="favicon" class="h-10" />
</UploadImagePopover>
</div>
<svelte:fragment slot="action">
{#if organizationFaviconUrl}
<Button
type="secondary"
on:click={onRemove}
loading={isLoading}
disabled={isLoading}
>
Remove
</Button>
{/if}
</svelte:fragment>
</SettingsContainer>
135 changes: 31 additions & 104 deletions web-admin/src/features/organizations/settings/LogoSettings.svelte
Original file line number Diff line number Diff line change
@@ -1,89 +1,46 @@
<script lang="ts">
import { invalidateAll } from "$app/navigation";
import { invalidate } from "$app/navigation";
import {
createAdminServiceCreateAsset,
createAdminServiceUpdateOrganization,
getAdminServiceGetOrganizationQueryKey,
} from "@rilldata/web-admin/client";
import { CANONICAL_ADMIN_URL } from "@rilldata/web-admin/client/http-client";
import { getRpcErrorMessage } from "@rilldata/web-admin/features/errors/error-utils";
import SettingsContainer from "@rilldata/web-admin/features/organizations/settings/SettingsContainer.svelte";
import UploadImagePopover from "@rilldata/web-admin/features/organizations/settings/UploadImagePopover.svelte";
import { Button } from "@rilldata/web-common/components/button";
import Rill from "@rilldata/web-common/components/icons/Rill.svelte";
import EditIcon from "@rilldata/web-common/components/icons/EditIcon.svelte";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@rilldata/web-common/components/popover";
import { extractFileExtension } from "@rilldata/web-common/features/entity-management/file-path-utils";
import { queryClient } from "@rilldata/web-common/lib/svelte-query/globalQueryClient";
import { builderActions, getAttrs } from "bits-ui";
import FileInput from "@rilldata/web-common/components/forms/FileInput.svelte";

export let organization: string;
export let organizationLogoUrl: string | undefined;

$: logoUrl = organizationLogoUrl;

const assetCreator = createAdminServiceCreateAsset();
const orgUpdater = createAdminServiceUpdateOrganization();
$: ({ error, isLoading, mutateAsync } = $orgUpdater);

let open = false;
let assetId = "";

async function uploadFile(file: File) {
const ext = extractFileExtension(file.name);
const assetResp = await $assetCreator.mutateAsync({
organizationName: organization,
data: {
type: "image",
name: "logo",
extension: ext,
public: true,
estimatedSizeBytes: file.size.toString(),
},
});

await fetch(assetResp.signedUrl, {
method: "PUT",
body: file,
headers: assetResp.signingHeaders,
});
assetId = assetResp.assetId;
return `${CANONICAL_ADMIN_URL}/v1/assets/${assetId}/download`;
}

function onCancel() {
open = false;
logoUrl = organizationLogoUrl;
}

async function removeLogo() {
onCancel();
await $orgUpdater.mutateAsync({
async function onSave(assetId: string) {
await mutateAsync({
name: organization,
data: {
logoAssetId: "",
logoAssetId: assetId,
},
});
void queryClient.invalidateQueries(
getAdminServiceGetOrganizationQueryKey(organization),
);
void invalidateAll();
void invalidate("root");
}

async function onSave() {
onCancel();
await $orgUpdater.mutateAsync({
async function onRemove() {
await mutateAsync({
name: organization,
data: {
logoAssetId: assetId,
logoAssetId: "",
},
});
void queryClient.invalidateQueries(
getAdminServiceGetOrganizationQueryKey(organization),
);
void invalidateAll();
void invalidate("root");
}
</script>

Expand All @@ -92,59 +49,29 @@
<div>
Click to upload your logo and customize Rill for your organization.
</div>
<Popover
bind:open
onOpenChange={(o) => {
if (!o) onCancel();
}}
<UploadImagePopover
imageUrl={organizationLogoUrl}
accept="image/png, image/ico, image/x-ico, image/icon, image/x-icon"
label="favicon"
{organization}
loading={isLoading}
error={getRpcErrorMessage(error)}
{onSave}
{onRemove}
>
<PopoverTrigger asChild let:builder>
<button
class="flex items-center relative group h-[72px] border border-gray-300 hover:bg-slate-100 w-fit"
{...getAttrs([builder])}
use:builderActions={{ builders: [builder] }}
class:w-24={!organizationLogoUrl}
class:w-20={!!organizationLogoUrl}
>
<div class="m-auto px-4 w-fit h-10">
{#if organizationLogoUrl}
<img src={organizationLogoUrl} alt="logo" class="h-10" />
{:else}
<Rill width="64" height="40" />
{/if}
</div>
{#if !open}
<div
class="absolute -bottom-2 -right-2 rounded-2xl bg-slate-200 group-hover:bg-slate-500 w-6 h-6 px-1.5 py-[5px]"
>
<EditIcon
size="16px"
className="text-slate-600 group-hover:text-slate-50"
/>
</div>
{/if}
</button>
</PopoverTrigger>
<PopoverContent
align="start"
side="bottom"
class="flex flex-col gap-y-2 w-[400px] p-4"
>
<div class="text-base font-medium">Upload org logo</div>
<FileInput bind:value={logoUrl} accept="image/*" {uploadFile} />
<div class="flex flex-row justify-end gap-x-2">
<Button type="secondary" on:click={onCancel}>Cancel</Button>
{#if organizationLogoUrl}
<Button type="secondary" on:click={removeLogo}>Remove</Button>
{/if}
<Button type="primary" on:click={onSave}>Save</Button>
</div>
</PopoverContent>
</Popover>
<Rill width="64" height="40" />
</UploadImagePopover>
</div>
<svelte:fragment slot="action">
{#if organizationLogoUrl}
<Button type="secondary" on:click={removeLogo}>Remove</Button>
<Button
type="secondary"
on:click={onRemove}
loading={isLoading}
disabled={isLoading}
>
Remove
</Button>
{/if}
</svelte:fragment>
</SettingsContainer>
Loading