-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Block for download-links in Sanity (#3108)
* feat: Downloads block * feat: Block for adding file-downloads to articles * Update aksel.nav.no/website/components/sanity-modules/download-block/DownloadBlock.stories.tsx * Update aksel.nav.no/website/components/sanity-modules/download-block/DownloadBlock.tsx * feat: Added max width * bug: Used nb locale for file size format * refactor: DownloadLink -> attachment * bug: attachment rename in query * feat: Body-text not required * memo: Updated story for no body variant of attachment * bug: Fixed reference to old naming
- Loading branch information
Showing
8 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
aksel.nav.no/website/components/sanity-modules/attachment/Attachment.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { AkselTheme, getBlocks } from "@/sb-util"; | ||
import Attachment from "./Attachment"; | ||
|
||
const meta = { | ||
title: "Sanity-modules/Attachment", | ||
component: Attachment, | ||
tags: ["autodocs"], | ||
} satisfies Meta<typeof Attachment>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Aksel: Story = { | ||
args: { | ||
node: { | ||
title: "NAV-logo (for digitale flater)", | ||
body: getBlocks({ length: 1 }), | ||
downloadLink: "1234.zip", | ||
fileName: "NAV logopakke digitale flater", | ||
size: "4321", | ||
}, | ||
}, | ||
decorators: [AkselTheme], | ||
}; | ||
|
||
export const NoBody: Story = { | ||
args: { | ||
node: { | ||
title: "NAV-logo (for digitale flater)", | ||
downloadLink: "1234.zip", | ||
fileName: "NAV logopakke digitale flater", | ||
size: "4321", | ||
}, | ||
}, | ||
decorators: [AkselTheme], | ||
}; |
74 changes: 74 additions & 0 deletions
74
aksel.nav.no/website/components/sanity-modules/attachment/Attachment.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useId } from "react"; | ||
import { DownloadIcon } from "@navikt/aksel-icons"; | ||
import { Heading, Link } from "@navikt/ds-react"; | ||
import ErrorBoundary from "@/error-boundary"; | ||
import { SanityBlockContent } from "@/sanity-block"; | ||
|
||
type AttachmentProps = { | ||
node: { | ||
title?: string; | ||
body?: any[]; | ||
downloadLink: string; | ||
fileName: string; | ||
size: string; | ||
}; | ||
}; | ||
|
||
const Attachment = ({ node }: AttachmentProps) => { | ||
const id = useId(); | ||
|
||
if (!node.title || !node.downloadLink || !node.fileName || !node.size) { | ||
return null; | ||
} | ||
|
||
const filetype = node.downloadLink.split(".").at(-1); | ||
|
||
return ( | ||
<section | ||
aria-labelledby={id} | ||
className="mb-12 flex max-w-2xl gap-2 rounded-lg bg-pink-100 px-6 py-4 ring-1 ring-inset ring-pink-300" | ||
> | ||
<span className="-mt-[1px] grid h-[1.625rem] shrink-0 place-content-center text-2xl"> | ||
<DownloadIcon aria-hidden /> | ||
</span> | ||
<div> | ||
<Heading size="small" level="2" id={id} aria-hidden> | ||
{node.title} | ||
</Heading> | ||
{node.body && ( | ||
<SanityBlockContent blocks={node.body} className="mt-2" /> | ||
)} | ||
|
||
<Link | ||
href={`${node.downloadLink}?dl=${node.fileName}.${filetype}`} | ||
className="mt-2 text-lg" | ||
rel="noreferrer noopener" | ||
download={node.fileName} | ||
> | ||
{`${node.fileName}.${filetype} (${bytesToSize(parseInt(node.size))})`} | ||
</Link> | ||
</div> | ||
</section> | ||
); | ||
}; | ||
|
||
/* https://gist.github.com/lanqy/5193417?permalink_comment_id=3874119 */ | ||
function bytesToSize(bytes: number) { | ||
const units = ["byte", "kilobyte", "megabyte"]; | ||
const unit = Math.floor(Math.log(bytes) / Math.log(1024)); | ||
return new Intl.NumberFormat("nb-NO", { | ||
style: "unit", | ||
minimumFractionDigits: 0, | ||
maximumFractionDigits: 0, | ||
roundingMode: "ceil", | ||
unit: units[unit], | ||
}).format(bytes / 1024 ** unit); | ||
} | ||
|
||
export default function Component(props: AttachmentProps) { | ||
return ( | ||
<ErrorBoundary boundaryName="Attachment"> | ||
<Attachment {...props} /> | ||
</ErrorBoundary> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
aksel.nav.no/website/sanity/schema/objects/shared/attachment.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { defineField, defineType } from "sanity"; | ||
import { DownloadIcon } from "@navikt/aksel-icons"; | ||
|
||
export const Attachment = defineType({ | ||
title: "Vedlegg med nedlastning", | ||
name: "attachment", | ||
type: "file", | ||
icon: DownloadIcon, | ||
fields: [ | ||
defineField({ | ||
title: "Tittel", | ||
name: "title", | ||
type: "string", | ||
validation: (Rule) => Rule.required().error("Vedlegg trenger tittel."), | ||
}), | ||
defineField({ | ||
title: "Filnavn", | ||
description: "Filtype blir automatisk lagt til.", | ||
name: "fileName", | ||
type: "string", | ||
validation: (Rule) => Rule.required().error("Vedlegg trenger filnavn."), | ||
}), | ||
defineField({ | ||
title: "Innhold", | ||
name: "body", | ||
type: "riktekst_enkel", | ||
}), | ||
], | ||
preview: { | ||
select: { | ||
title: "title", | ||
}, | ||
prepare(selection) { | ||
return { | ||
title: selection.title, | ||
subtitle: "Vedlegg med nedlastning", | ||
media: DownloadIcon, | ||
}; | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters