Files
neuf-www/web/src/lib/common.ts
T

248 lines
4.4 KiB
TypeScript

import { graphql } from "@/gql";
export type PigName =
| "logo"
| "music"
| "drink"
| "dance"
| "point"
| "student"
| "listen"
| "guard"
| "key"
| "chill"
| "peek";
export const PIG_NAMES = [
"logo",
"music",
"drink",
"dance",
"point",
"student",
"listen",
"guard",
"key",
"chill",
"peek",
];
export function getSearchPath(query: string): string {
const params = new URLSearchParams();
if (query) {
params.set("q", query);
} else {
params.delete("q");
}
return `/sok?${params.toString()}`;
}
export function randomElement(array: any[]): any | undefined {
return array.length
? array[Math.floor(Math.random() * array.length)]
: undefined;
}
export function unique<T>(array: any[]): any[] {
return Array.from(array.reduce((set, item) => set.add(item), new Set()));
}
export function stripWhitespace(s: string): string {
return s.replace(/\s/g, "");
}
export function stripHtml(s: string): string {
return s.replace(/(<([^>]+)>)/gi, "");
}
export function formatPhoneE164(phone: string): string {
phone = stripWhitespace(phone);
if (phone.startsWith("+") || phone.length != 8) {
return phone;
}
return "+47" + phone;
}
export function formatNorwegianPhoneNumber(phone: string): string {
if (phone.startsWith("+47")) {
const local = phone.substring(3);
if (local.length === 8) {
return `${local.substring(0, 3)} ${local.substring(
3,
5
)} ${local.substring(5)}`;
}
}
return phone;
}
export function formatHumanReadableList(array: (string | number)[]): string {
const length = array.length;
if (length === 0) {
return "";
}
if (length === 1) {
return array[0].toString();
}
if (length === 2) {
return array.join(" og ");
}
return array.slice(0, -1).join(", ") + " og " + array[length - 1];
}
const LeafBlocksFragmentDefinition = graphql(`
fragment LeafBlocks on StreamFieldInterface {
id
blockType
field
... on RichTextBlock {
rawValue
value
}
... on ImageWithTextBlock {
image {
...Image
}
imageFormat
text
}
... on ImageSliderBlock {
images {
... on ImageSliderItemBlock {
image {
...Image
}
text
}
}
}
... on HorizontalRuleBlock {
color
}
... on FeaturedBlock {
title
featuredBlockText: text
linkText
imagePosition
backgroundColor
featuredPage {
contentType
pageType
url
... on EventPage {
featuredImage {
...Image
}
}
... on NewsPage {
featuredImage {
...Image
}
}
}
featuredImageOverride {
...Image
}
}
... on ContactListBlock {
items {
blockType
... on ContactEntityBlock {
contactEntity {
...ContactEntity
}
}
}
}
... on EmbedBlock {
url
embed
rawEmbed
}
... on FactBoxBlock {
backgroundColor
factBoxBody: body
}
}
`);
const OneLevelOfBlocksFragmentDefinition = graphql(`
fragment OneLevelOfBlocks on StreamFieldInterface {
...LeafBlocks
... on AccordionBlock {
heading
body {
...LeafBlocks
}
}
... on PageSectionBlock {
title
backgroundColor
icon
body {
...LeafBlocks
}
}
}
`);
const BlockFragmentDefinition = graphql(`
fragment Blocks on StreamFieldInterface {
... on AccordionBlock {
heading
body {
...OneLevelOfBlocks
}
}
... on PageSectionBlock {
title
backgroundColor
icon
body {
...OneLevelOfBlocks
}
}
... on ContactSectionBlock {
title
text
blocks {
... on ContactSubsectionBlock {
title
text
blocks {
...OneLevelOfBlocks
}
}
...OneLevelOfBlocks
}
}
...OneLevelOfBlocks
}
`);
const ImageFragmentDefinition = graphql(`
fragment Image on CustomImage {
id
url
width
height
alt
attribution
}
`);
const ContactEntityFragmentDefinition = graphql(`
fragment ContactEntity on ContactEntity {
id
name
contactType
title
email
phoneNumber
image {
...Image
}
}
`);