Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 1003 additions and 370 deletions
import { Plus } from "@styled-icons/boxicons-regular";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { Children } from "../../types/Preact"; import { Children } from "../../types/Preact";
import { Plus } from "@styled-icons/feather";
const CategoryBase = styled.div<Pick<Props, 'variant'>>` const CategoryBase = styled.div<Pick<Props, "variant">>`
font-size: 12px; font-size: 12px;
font-weight: 700; font-weight: 700;
text-transform: uppercase; text-transform: uppercase;
margin-top: 4px; margin-top: 4px;
padding: 6px 10px; padding: 6px 0;
margin-bottom: 4px; margin-bottom: 4px;
white-space: nowrap; white-space: nowrap;
display: flex; display: flex;
align-items: center; align-items: center;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
svg { svg {
stroke: var(--foreground);
cursor: pointer; cursor: pointer;
} }
${ props => props.variant === 'uniform' && css` &:first-child {
padding-top: 6px; margin-top: 0;
` } padding-top: 0;
}
${(props) =>
props.variant === "uniform" &&
css`
padding-top: 6px;
`}
`; `;
interface Props { type Props = Omit<
JSX.HTMLAttributes<HTMLDivElement>,
"children" | "as" | "action"
> & {
text: Children; text: Children;
action?: () => void; action?: () => void;
variant?: 'default' | 'uniform'; variant?: "default" | "uniform";
} };
export default function Category(props: Props) { export default function Category(props: Props) {
const { text, action, ...otherProps } = props;
return ( return (
<CategoryBase> <CategoryBase {...otherProps}>
{props.text} {text}
{props.action && ( {action && <Plus size={16} onClick={action} />}
<Plus size={16} onClick={props.action} />
)}
</CategoryBase> </CategoryBase>
); );
}; }
import { Check } from "@styled-icons/feather"; import { Check } from "@styled-icons/boxicons-regular";
import { Children } from "../../types/Preact";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { Children } from "../../types/Preact";
const CheckboxBase = styled.label` const CheckboxBase = styled.label`
gap: 4px; gap: 4px;
z-index: 1; z-index: 1;
padding: 4px;
display: flex; display: flex;
border-radius: 4px; margin-top: 20px;
align-items: center; align-items: center;
border-radius: var(--border-radius);
cursor: pointer; cursor: pointer;
font-size: 18px; font-size: 18px;
...@@ -16,33 +17,36 @@ const CheckboxBase = styled.label` ...@@ -16,33 +17,36 @@ const CheckboxBase = styled.label`
transition: 0.2s ease all; transition: 0.2s ease all;
p {
margin: 0;
}
input { input {
display: none; display: none;
} }
&:hover { &:hover {
background: var(--secondary-background);
.check { .check {
background: var(--background); background: var(--background);
} }
} }
&[disabled] {
opacity: 0.5;
cursor: not-allowed;
&:hover {
background: unset;
}
}
`; `;
const CheckboxContent = styled.span` const CheckboxContent = styled.span`
flex-grow: 1;
display: flex; display: flex;
flex-grow: 1;
font-size: 1rem; font-size: 1rem;
font-weight: 600; font-weight: 600;
flex-direction: column; flex-direction: column;
`; `;
const CheckboxDescription = styled.span` const CheckboxDescription = styled.span`
font-size: 0.8em; font-size: 0.75rem;
font-weight: 400; font-weight: 400;
color: var(--secondary-foreground); color: var(--secondary-foreground);
`; `;
...@@ -52,14 +56,14 @@ const Checkmark = styled.div<{ checked: boolean }>` ...@@ -52,14 +56,14 @@ const Checkmark = styled.div<{ checked: boolean }>`
width: 24px; width: 24px;
height: 24px; height: 24px;
display: grid; display: grid;
border-radius: 4px; flex-shrink: 0;
place-items: center; place-items: center;
transition: 0.2s ease all; transition: 0.2s ease all;
border-radius: var(--border-radius);
background: var(--secondary-background); background: var(--secondary-background);
svg { svg {
color: var(--secondary-background); color: var(--secondary-background);
stroke-width: 2;
} }
${(props) => ${(props) =>
......
import { useRef } from "preact/hooks"; import { Check } from "@styled-icons/boxicons-regular";
import { Check } from "@styled-icons/feather"; import { Palette } from "@styled-icons/boxicons-solid";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { Pencil } from "@styled-icons/bootstrap";
import { RefObject } from "preact";
import { useRef } from "preact/hooks";
interface Props { interface Props {
value: string; value: string;
...@@ -32,21 +34,40 @@ const presets = [ ...@@ -32,21 +34,40 @@ const presets = [
]; ];
const SwatchesBase = styled.div` const SwatchesBase = styled.div`
gap: 8px; /*gap: 8px;*/
display: flex; display: flex;
input { input {
width: 0;
height: 0;
top: 72px;
opacity: 0; opacity: 0;
margin-top: 44px; padding: 0;
position: absolute; border: 0;
position: relative;
pointer-events: none; pointer-events: none;
} }
.overlay {
position: relative;
width: 0;
div {
width: 8px;
height: 68px;
background: linear-gradient(
to right,
var(--primary-background),
transparent
);
}
}
`; `;
const Swatch = styled.div<{ type: "small" | "large"; colour: string }>` const Swatch = styled.div<{ type: "small" | "large"; colour: string }>`
flex-shrink: 0; flex-shrink: 0;
cursor: pointer; cursor: pointer;
border-radius: 4px; border-radius: var(--border-radius);
background-color: ${(props) => props.colour}; background-color: ${(props) => props.colour};
display: grid; display: grid;
...@@ -68,7 +89,7 @@ const Swatch = styled.div<{ type: "small" | "large"; colour: string }>` ...@@ -68,7 +89,7 @@ const Swatch = styled.div<{ type: "small" | "large"; colour: string }>`
height: 30px; height: 30px;
svg { svg {
stroke-width: 2; /*stroke-width: 2;*/
} }
` `
: css` : css`
...@@ -81,32 +102,39 @@ const Rows = styled.div` ...@@ -81,32 +102,39 @@ const Rows = styled.div`
gap: 8px; gap: 8px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
overflow: auto;
padding-bottom: 4px;
> div { > div {
gap: 8px; gap: 8px;
display: flex; display: flex;
flex-direction: row; flex-direction: row;
padding-inline-start: 8px;
} }
`; `;
export default function ColourSwatches({ value, onChange }: Props) { export default function ColourSwatches({ value, onChange }: Props) {
const ref = useRef<HTMLInputElement>(); const ref = useRef<HTMLInputElement>() as RefObject<HTMLInputElement>;
return ( return (
<SwatchesBase> <SwatchesBase>
<Swatch
colour={value}
type="large"
onClick={() => ref.current.click()}
>
<Pencil size={32} />
</Swatch>
<input <input
type="color" type="color"
value={value} value={value}
ref={ref} ref={ref}
onChange={(ev) => onChange(ev.currentTarget.value)} onChange={(ev) => onChange(ev.currentTarget.value)}
/> />
<Swatch
colour={value}
type="large"
onClick={() => ref.current?.click()}>
<Palette size={32} />
</Swatch>
<div class="overlay">
<div />
</div>
<Rows> <Rows>
{presets.map((row, i) => ( {presets.map((row, i) => (
<div key={i}> <div key={i}>
...@@ -115,11 +143,8 @@ export default function ColourSwatches({ value, onChange }: Props) { ...@@ -115,11 +143,8 @@ export default function ColourSwatches({ value, onChange }: Props) {
colour={swatch} colour={swatch}
type="small" type="small"
key={i} key={i}
onClick={() => onChange(swatch)} onClick={() => onChange(swatch)}>
> {swatch === value && <Check size={22} />}
{swatch === value && (
<Check size={18} strokeWidth={2} />
)}
</Swatch> </Swatch>
))} ))}
</div> </div>
......
import styled from "styled-components"; import styled from "styled-components";
export default styled.select` export default styled.select`
padding: 8px; width: 100%;
border-radius: 2px; padding: 10px;
cursor: pointer;
border-radius: var(--border-radius);
font-family: inherit;
font-size: var(--text-size);
color: var(--secondary-foreground); color: var(--secondary-foreground);
background: var(--secondary-background); background: var(--secondary-background);
border: none;
outline: 2px solid transparent;
transition: box-shadow 0.2s ease-in-out;
transition: outline-color 0.2s ease-in-out;
&:focus {
box-shadow: 0 0 0 1.5pt var(--accent);
}
`; `;
import dayjs from "dayjs";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { dayjs } from "../../context/Locale";
const Base = styled.div<{ unread?: boolean }>` const Base = styled.div<{ unread?: boolean }>`
height: 0; height: 0;
display: flex; display: flex;
margin: 14px 10px;
user-select: none; user-select: none;
align-items: center; align-items: center;
margin: 17px 12px 5px;
border-top: thin solid var(--tertiary-foreground); border-top: thin solid var(--tertiary-foreground);
time { time {
margin-top: -2px; margin-top: -2px;
font-size: .6875rem; font-size: 0.6875rem;
line-height: .6875rem; line-height: 0.6875rem;
padding: 2px 5px 2px 0; padding: 2px 0 2px 0;
padding-inline-end: 5px;
color: var(--tertiary-foreground); color: var(--tertiary-foreground);
background: var(--primary-background); background: var(--primary-background);
} }
${ props => props.unread && css` ${(props) =>
border-top: thin solid var(--accent); props.unread &&
` } css`
border-top: thin solid var(--accent);
`}
`; `;
const Unread = styled.div` const Unread = styled.div`
...@@ -39,10 +43,8 @@ interface Props { ...@@ -39,10 +43,8 @@ interface Props {
export default function DateDivider(props: Props) { export default function DateDivider(props: Props) {
return ( return (
<Base unread={props.unread}> <Base unread={props.unread}>
{ props.unread && <Unread>NEW</Unread> } {props.unread && <Unread>NEW</Unread>}
<time> <time>{dayjs(props.date).format("LL")}</time>
{ dayjs(props.date).format("LL") }
</time>
</Base> </Base>
); );
} }
import styled, { css } from "styled-components";
export default styled.details<{ sticky?: boolean; large?: boolean }>`
summary {
${(props) =>
props.sticky &&
css`
top: -1px;
z-index: 10;
position: sticky;
`}
${(props) =>
props.large &&
css`
/*padding: 5px 0;*/
background: var(--primary-background);
color: var(--secondary-foreground);
.padding {
/*TOFIX: make this applicable only for the friends list menu, DO NOT REMOVE.*/
display: flex;
align-items: center;
padding: 5px 0;
margin: 0.8em 0px 0.4em;
cursor: pointer;
}
`}
outline: none;
cursor: pointer;
list-style: none;
user-select: none;
align-items: center;
transition: 0.2s opacity;
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
&::marker,
&::-webkit-details-marker {
display: none;
}
.title {
flex-grow: 1;
margin-top: 1px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
.padding {
display: flex;
align-items: center;
> svg {
flex-shrink: 0;
margin-inline-end: 4px;
transition: 0.2s ease transform;
}
}
}
&:not([open]) {
summary {
opacity: 0.7;
}
summary svg {
transform: rotateZ(-90deg);
}
}
`;
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
interface Props { interface Props {
borders?: boolean;
background?: boolean; background?: boolean;
placement: 'primary' | 'secondary' placement: "primary" | "secondary";
} }
export default styled.div<Props>` export default styled.div<Props>`
height: 56px; gap: 6px;
font-weight: 600; height: 48px;
user-select: none;
gap: 10px;
flex: 0 auto; flex: 0 auto;
display: flex; display: flex;
padding: 20px;
flex-shrink: 0; flex-shrink: 0;
padding: 0 16px;
font-weight: 600;
user-select: none;
align-items: center; align-items: center;
background-color: var(--primary-header);
background-size: cover !important; background-size: cover !important;
background-position: center !important; background-position: center !important;
background-color: var(--primary-header);
svg {
flex-shrink: 0;
}
.menu {
margin-inline-end: 8px;
color: var(--secondary-foreground);
}
/*@media only screen and (max-width: 768px) {
padding: 0 12px;
}*/
@media (pointer: coarse) {
height: 56px;
}
${(props) =>
props.background &&
css`
height: 120px !important;
align-items: flex-end;
text-shadow: 0px 0px 1px black;
`}
${ props => props.background && css` ${(props) =>
height: 120px; props.placement === "secondary" &&
align-items: flex-end; css`
` } background-color: var(--secondary-header);
padding: 14px;
`}
${ props => props.placement === 'secondary' && css` ${(props) =>
background-color: var(--secondary-header); props.borders &&
padding: 14px; css`
` } border-start-start-radius: 8px;
`}
`; `;
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
interface Props { interface Props {
type?: 'default' | 'circle' rotate?: string;
type?: "default" | "circle";
} }
const normal = `var(--secondary-foreground)`; const normal = `var(--secondary-foreground)`;
...@@ -12,32 +13,47 @@ export default styled.div<Props>` ...@@ -12,32 +13,47 @@ export default styled.div<Props>`
display: grid; display: grid;
cursor: pointer; cursor: pointer;
place-items: center; place-items: center;
transition: 0.1s ease background-color;
fill: ${normal}; fill: ${normal};
color: ${normal}; color: ${normal};
stroke: ${normal}; /*stroke: ${normal};*/
a { a {
color: ${normal}; color: ${normal};
} }
svg {
transition: 0.2s ease transform;
}
&:hover { &:hover {
fill: ${hover}; fill: ${hover};
color: ${hover}; color: ${hover};
stroke: ${hover}; /*stroke: ${hover};*/
a { a {
color: ${hover}; color: ${hover};
} }
} }
${ props => props.type === 'circle' && css` ${(props) =>
padding: 4px; props.type === "circle" &&
border-radius: 50%; css`
background-color: var(--secondary-header); padding: 4px;
border-radius: 50%;
&:hover { background-color: var(--secondary-header);
background-color: var(--primary-header);
} &:hover {
` } background-color: var(--primary-header);
}
`}
${(props) =>
props.rotate &&
css`
svg {
transform: rotateZ(${props.rotate});
}
`}
`; `;
...@@ -6,20 +6,26 @@ interface Props { ...@@ -6,20 +6,26 @@ interface Props {
export default styled.input<Props>` export default styled.input<Props>`
z-index: 1; z-index: 1;
font-size: 1rem;
padding: 8px 16px; padding: 8px 16px;
border-radius: 6px; border-radius: var(--border-radius);
font-family: inherit;
color: var(--foreground); color: var(--foreground);
border: 2px solid transparent;
background: var(--primary-background); background: var(--primary-background);
transition: 0.2s ease background-color; transition: 0.2s ease background-color;
transition: border-color 0.2s ease-in-out;
border: none;
outline: 2px solid transparent;
transition: outline-color 0.2s ease-in-out;
transition: box-shadow 0.2s ease-in-out;
&:hover { &:hover {
background: var(--secondary-background); background: var(--secondary-background);
} }
&:focus { &:focus {
border: 2px solid var(--accent); box-shadow: 0 0 0 1.5pt var(--accent);
} }
${(props) => ${(props) =>
......
// This file must be imported and used at least once for SVG masks.
export default function Masks() {
return (
<svg width={0} height={0} style={{ position: "fixed" }}>
<defs>
<mask id="server">
<rect x="0" y="0" width="32" height="32" fill="white" />
<circle cx="27" cy="5" r="7" fill={"black"} />
</mask>
<mask id="user">
<rect x="0" y="0" width="32" height="32" fill="white" />
<circle cx="27" cy="27" r="7" fill={"black"} />
</mask>
<mask id="session">
<rect x="0" y="0" width="32" height="32" fill="white" />
<circle cx="26" cy="28" r="10" fill={"black"} />
</mask>
<mask id="overlap">
<rect x="0" y="0" width="32" height="32" fill="white" />
<circle cx="32" cy="16" r="18" fill={"black"} />
</mask>
</defs>
</svg>
);
}
import Button from "./Button"; /* eslint-disable react-hooks/rules-of-hooks */
import classNames from "classnames";
import { Children } from "../../types/Preact";
import { createPortal, useEffect } from "preact/compat";
import styled, { css, keyframes } from "styled-components"; import styled, { css, keyframes } from "styled-components";
import { createPortal, useCallback, useEffect, useState } from "preact/compat";
import { internalSubscribe } from "../../lib/eventEmitter";
import { Children } from "../../types/Preact";
import Button, { ButtonProps } from "./Button";
const open = keyframes` const open = keyframes`
0% {opacity: 0;} 0% {opacity: 0;}
70% {opacity: 0;} 70% {opacity: 0;}
100% {opacity: 1;} 100% {opacity: 1;}
`; `;
const close = keyframes`
0% {opacity: 1;}
70% {opacity: 0;}
100% {opacity: 0;}
`;
const zoomIn = keyframes` const zoomIn = keyframes`
0% {transform: scale(0.5);} 0% {transform: scale(0.5);}
98% {transform: scale(1.01);} 98% {transform: scale(1.01);}
100% {transform: scale(1);} 100% {transform: scale(1);}
`; `;
const zoomOut = keyframes`
0% {transform: scale(1);}
100% {transform: scale(0.5);}
`;
const ModalBase = styled.div` const ModalBase = styled.div`
top: 0; top: 0;
left: 0; left: 0;
...@@ -35,42 +50,65 @@ const ModalBase = styled.div` ...@@ -35,42 +50,65 @@ const ModalBase = styled.div`
color: var(--foreground); color: var(--foreground);
background: rgba(0, 0, 0, 0.8); background: rgba(0, 0, 0, 0.8);
&.closing {
animation-name: ${close};
}
&.closing > div {
animation-name: ${zoomOut};
}
`; `;
const ModalContainer = styled.div` const ModalContainer = styled.div`
overflow: hidden; overflow: hidden;
border-radius: 8px;
max-width: calc(100vw - 20px); max-width: calc(100vw - 20px);
border-radius: var(--border-radius);
animation-name: ${zoomIn}; animation-name: ${zoomIn};
animation-duration: 0.25s; animation-duration: 0.25s;
animation-timing-function: cubic-bezier(.3,.3,.18,1.1); animation-timing-function: cubic-bezier(0.3, 0.3, 0.18, 1.1);
`; `;
const ModalContent = styled.div<{ [key in 'attachment' | 'noBackground' | 'border' | 'padding']?: boolean }>` const ModalContent = styled.div<
border-radius: 8px; { [key in "attachment" | "noBackground" | "border" | "padding"]?: boolean }
>`
text-overflow: ellipsis; text-overflow: ellipsis;
border-radius: var(--border-radius);
h3 { h3 {
margin-top: 0; margin-top: 0;
} }
${ props => !props.noBackground && css` form {
background: var(--secondary-header); display: flex;
` } flex-direction: column;
}
${ props => props.padding && css`
padding: 1.5em;
` }
${ props => props.attachment && css`
border-radius: 8px 8px 0 0;
` }
${ props => props.border && css` ${(props) =>
border-radius: 10px; !props.noBackground &&
border: 2px solid var(--secondary-background); css`
` } background: var(--secondary-header);
`}
${(props) =>
props.padding &&
css`
padding: 1.5em;
`}
${(props) =>
props.attachment &&
css`
border-radius: var(--border-radius) var(--border-radius) 0 0;
`}
${(props) =>
props.border &&
css`
border-radius: var(--border-radius);
border: 2px solid var(--secondary-background);
`}
`; `;
const ModalActions = styled.div` const ModalActions = styled.div`
...@@ -83,13 +121,10 @@ const ModalActions = styled.div` ...@@ -83,13 +121,10 @@ const ModalActions = styled.div`
background: var(--secondary-background); background: var(--secondary-background);
`; `;
export interface Action { export type Action = Omit<ButtonProps, "onClick"> & {
text: Children;
onClick: () => void;
confirmation?: boolean; confirmation?: boolean;
contrast?: boolean; onClick: () => void;
error?: boolean; };
}
interface Props { interface Props {
children?: Children; children?: Children;
...@@ -98,23 +133,26 @@ interface Props { ...@@ -98,23 +133,26 @@ interface Props {
disallowClosing?: boolean; disallowClosing?: boolean;
noBackground?: boolean; noBackground?: boolean;
dontModal?: boolean; dontModal?: boolean;
padding?: boolean;
onClose: () => void; onClose?: () => void;
actions?: Action[]; actions?: Action[];
disabled?: boolean; disabled?: boolean;
border?: boolean; border?: boolean;
visible: boolean; visible: boolean;
} }
export let isModalClosing = false;
export default function Modal(props: Props) { export default function Modal(props: Props) {
if (!props.visible) return null; if (!props.visible) return null;
let content = ( const content = (
<ModalContent <ModalContent
attachment={!!props.actions} attachment={!!props.actions}
noBackground={props.noBackground} noBackground={props.noBackground}
border={props.border} border={props.border}
padding={!props.dontModal}> padding={props.padding ?? !props.dontModal}>
{props.title && <h3>{props.title}</h3>} {props.title && <h3>{props.title}</h3>}
{props.children} {props.children}
</ModalContent> </ModalContent>
...@@ -124,11 +162,36 @@ export default function Modal(props: Props) { ...@@ -124,11 +162,36 @@ export default function Modal(props: Props) {
return content; return content;
} }
let confirmationAction = props.actions?.find(action => action.confirmation); const [animateClose, setAnimateClose] = useState(false);
isModalClosing = animateClose;
const onClose = useCallback(() => {
setAnimateClose(true);
setTimeout(() => props.onClose?.(), 2e2);
}, [setAnimateClose, props]);
useEffect(() => internalSubscribe("Modal", "close", onClose), [onClose]);
useEffect(() => {
if (props.disallowClosing) return;
function keyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
onClose();
}
}
document.body.addEventListener("keydown", keyDown);
return () => document.body.removeEventListener("keydown", keyDown);
}, [props.disallowClosing, onClose]);
const confirmationAction = props.actions?.find(
(action) => action.confirmation,
);
useEffect(() => { useEffect(() => {
if (!confirmationAction) return; if (!confirmationAction) return;
// ! FIXME: this may be done better if we // ! TODO: this may be done better if we
// ! can focus the button although that // ! can focus the button although that
// ! doesn't seem to work... // ! doesn't seem to work...
function keyDown(e: KeyboardEvent) { function keyDown(e: KeyboardEvent) {
...@@ -139,27 +202,27 @@ export default function Modal(props: Props) { ...@@ -139,27 +202,27 @@ export default function Modal(props: Props) {
document.body.addEventListener("keydown", keyDown); document.body.addEventListener("keydown", keyDown);
return () => document.body.removeEventListener("keydown", keyDown); return () => document.body.removeEventListener("keydown", keyDown);
}, [ confirmationAction ]); }, [confirmationAction]);
return createPortal( return createPortal(
<ModalBase onClick={(!props.disallowClosing && props.onClose) || undefined}> <ModalBase
<ModalContainer onClick={e => (e.cancelBubble = true)}> className={animateClose ? "closing" : undefined}
onClick={(!props.disallowClosing && props.onClose) || undefined}>
<ModalContainer onClick={(e) => (e.cancelBubble = true)}>
{content} {content}
{props.actions && ( {props.actions && (
<ModalActions> <ModalActions>
{props.actions.map(x => ( {props.actions.map((x, index) => (
<Button <Button
contrast={x.contrast ?? true} key={index}
error={x.error ?? false} {...x}
onClick={x.onClick} disabled={props.disabled}
disabled={props.disabled}> />
{x.text}
</Button>
))} ))}
</ModalActions> </ModalActions>
)} )}
</ModalContainer> </ModalContainer>
</ModalBase>, </ModalBase>,
document.body document.body,
); );
} }
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { Text } from "preact-i18n";
import { Children } from "../../types/Preact"; import { Children } from "../../types/Preact";
import { Text } from 'preact-i18n';
interface Props { type Props = Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "as"> & {
error?: string; error?: string;
block?: boolean; block?: boolean;
spaced?: boolean;
noMargin?: boolean;
children?: Children; children?: Children;
type?: "default" | "subtle" | "error"; type?: "default" | "subtle" | "error";
} };
const OverlineBase = styled.div<Omit<Props, "children" | "error">>` const OverlineBase = styled.div<Omit<Props, "children" | "error">>`
display: inline; display: inline;
margin: 0.4em 0;
margin-top: 0.8em; ${(props) =>
!props.noMargin &&
css`
margin: 0.4em 0;
`}
${(props) =>
props.spaced &&
css`
margin-top: 0.8em;
`}
font-size: 14px; font-size: 14px;
font-weight: 600; font-weight: 600;
...@@ -46,9 +60,11 @@ export default function Overline(props: Props) { ...@@ -46,9 +60,11 @@ export default function Overline(props: Props) {
<OverlineBase {...props}> <OverlineBase {...props}>
{props.children} {props.children}
{props.children && props.error && <> &middot; </>} {props.children && props.error && <> &middot; </>}
{props.error && <Overline type="error"> {props.error && (
<Text id={`error.${props.error}`}>{props.error}</Text> <Overline type="error">
</Overline>} <Text id={`error.${props.error}`}>{props.error}</Text>
</Overline>
)}
</OverlineBase> </OverlineBase>
); );
} }
export default function Preloader() { import styled, { keyframes } from "styled-components";
return <span>LOADING</span>;
const skSpinner = keyframes`
0%, 80%, 100% {
-webkit-transform: scale(0);
transform: scale(0);
}
40% {
-webkit-transform: scale(1.0);
transform: scale(1.0);
}
`;
const prRing = keyframes`
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
`;
const PreloaderBase = styled.div`
width: 100%;
height: 100%;
display: grid;
place-items: center;
.spinner {
width: 58px;
display: flex;
text-align: center;
margin: 100px auto 0;
justify-content: space-between;
}
.spinner > div {
width: 14px;
height: 14px;
background-color: var(--tertiary-foreground);
border-radius: 100%;
display: inline-block;
animation: ${skSpinner} 1.4s infinite ease-in-out both;
}
.spinner div:nth-child(1) {
animation-delay: -0.32s;
}
.spinner div:nth-child(2) {
animation-delay: -0.16s;
}
.ring {
display: inline-block;
position: relative;
width: 48px;
height: 52px;
}
.ring div {
width: 32px;
margin: 8px;
height: 32px;
display: block;
position: absolute;
border-radius: 50%;
box-sizing: border-box;
border: 2px solid #fff;
animation: ${prRing} 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
border-color: #fff transparent transparent transparent;
}
.ring div:nth-child(1) {
animation-delay: -0.45s;
}
.ring div:nth-child(2) {
animation-delay: -0.3s;
}
.ring div:nth-child(3) {
animation-delay: -0.15s;
}
`;
interface Props {
type: "spinner" | "ring";
}
export default function Preloader({ type }: Props) {
return (
<PreloaderBase>
<div class={type}>
<div />
<div />
<div />
</div>
</PreloaderBase>
);
} }
import { Children } from "../../types/Preact"; import { Circle } from "@styled-icons/boxicons-regular";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
import { CircleFill } from "@styled-icons/bootstrap";
import { Children } from "../../types/Preact";
interface Props { interface Props {
children: Children; children: Children;
...@@ -26,8 +27,8 @@ const RadioBase = styled.label<BaseProps>` ...@@ -26,8 +27,8 @@ const RadioBase = styled.label<BaseProps>`
font-size: 1rem; font-size: 1rem;
font-weight: 600; font-weight: 600;
user-select: none; user-select: none;
border-radius: 4px;
transition: 0.2s ease all; transition: 0.2s ease all;
border-radius: var(--border-radius);
&:hover { &:hover {
background: var(--hover); background: var(--hover);
...@@ -48,7 +49,7 @@ const RadioBase = styled.label<BaseProps>` ...@@ -48,7 +49,7 @@ const RadioBase = styled.label<BaseProps>`
svg { svg {
color: var(--foreground); color: var(--foreground);
stroke-width: 2; /*stroke-width: 2;*/
} }
} }
...@@ -92,10 +93,9 @@ export default function Radio(props: Props) { ...@@ -92,10 +93,9 @@ export default function Radio(props: Props) {
disabled={props.disabled} disabled={props.disabled}
onClick={() => onClick={() =>
!props.disabled && props.onSelect && props.onSelect() !props.disabled && props.onSelect && props.onSelect()
} }>
>
<div> <div>
<CircleFill size={12} /> <Circle size={12} />
</div> </div>
<input type="radio" checked={props.checked} /> <input type="radio" checked={props.checked} />
<span> <span>
......
.container { .container {
font-size: 0.875rem; font-size: .875rem;
line-height: 20px; line-height: 20px;
position: relative; position: relative;
} }
......
// ! FIXME: temporarily here until re-written import styled, { css } from "styled-components";
// ! DO NOT IMRPOVE, JUST RE-WRITE
import classNames from "classnames";
import { memo } from "preact/compat";
import styles from "./TextArea.module.scss";
import { useState, useEffect, useRef, useLayoutEffect } from "preact/hooks";
export interface TextAreaProps { export interface TextAreaProps {
id?: string; code?: boolean;
value: string; padding?: string;
maxRows?: number; lineHeight?: string;
padding?: number; hideBorder?: boolean;
minHeight?: number;
disabled?: boolean;
maxLength?: number;
className?: string;
autoFocus?: boolean;
forceFocus?: boolean;
placeholder?: string;
onKeyDown?: (ev: KeyboardEvent) => void;
onKeyUp?: (ev: KeyboardEvent) => void;
onChange: (
value: string,
ev: JSX.TargetedEvent<HTMLTextAreaElement, Event>
) => void;
onFocus?: (current: HTMLTextAreaElement) => void;
onBlur?: () => void;
} }
const lineHeight = 20; export const TEXT_AREA_BORDER_WIDTH = 2;
export const DEFAULT_TEXT_AREA_PADDING = 16;
export const TextArea = memo((props: TextAreaProps) => { export const DEFAULT_LINE_HEIGHT = 20;
const padding = props.padding ? props.padding * 2 : 0;
export default styled.textarea<TextAreaProps>`
const [height, setHeightState] = useState( width: 100%;
props.minHeight ?? lineHeight + padding resize: none;
); display: block;
const ghost = useRef<HTMLDivElement>(); color: var(--foreground);
const ref = useRef<HTMLTextAreaElement>(); background: var(--secondary-background);
padding: ${(props) => props.padding ?? "var(--textarea-padding)"};
function setHeight(h: number = lineHeight) { line-height: ${(props) =>
let newHeight = Math.min( props.lineHeight ?? "var(--textarea-line-height)"};
Math.max(
lineHeight, ${(props) =>
props.maxRows ? Math.min(h, props.maxRows * lineHeight) : h props.hideBorder &&
), css`
props.minHeight ?? Infinity border: none;
); `}
if (props.padding) newHeight += padding; ${(props) =>
if (height !== newHeight) { !props.hideBorder &&
setHeightState(newHeight); css`
} border-radius: var(--border-radius);
transition: border-color 0.2s ease-in-out;
border: var(--input-border-width) solid transparent;
`}
&:focus {
outline: none;
${(props) =>
!props.hideBorder &&
css`
border: var(--input-border-width) solid var(--accent);
`}
} }
function onChange(ev: JSX.TargetedEvent<HTMLTextAreaElement, Event>) { ${(props) =>
props.onChange(ev.currentTarget.value, ev); props.code
} ? css`
font-family: var(--monospace-font), monospace;
useLayoutEffect(() => { `
setHeight(ghost.current.clientHeight); : css`
}, [ghost, props.value]); font-family: inherit;
`}
useEffect(() => {
if (props.autoFocus) ref.current.focus(); font-variant-ligatures: var(--ligatures);
}, [props.value]); `;
const inputSelected = () =>
["TEXTAREA", "INPUT"].includes(document.activeElement?.nodeName ?? "");
useEffect(() => {
if (props.forceFocus) {
ref.current.focus();
}
if (props.autoFocus && !inputSelected()) {
ref.current.focus();
}
// ? if you are wondering what this is
// ? it is a quick and dirty hack to fix
// ? value not setting correctly
// ? I have no clue what's going on
ref.current.value = props.value;
if (!props.autoFocus) return;
function keyDown(e: KeyboardEvent) {
if ((e.ctrlKey && e.key !== "v") || e.altKey || e.metaKey) return;
if (e.key.length !== 1) return;
if (ref && !inputSelected()) {
ref.current.focus();
}
}
document.body.addEventListener("keydown", keyDown);
return () => document.body.removeEventListener("keydown", keyDown);
}, [ref]);
useEffect(() => {
function focus(textarea_id: string) {
if (props.id === textarea_id) {
ref.current.focus();
}
}
// InternalEventEmitter.addListener("focus_textarea", focus);
// return () =>
// InternalEventEmitter.removeListener("focus_textarea", focus);
}, [ref]);
return (
<div className={classNames(styles.container, props.className)}>
<textarea
id={props.id}
name={props.id}
style={{ height }}
value={props.value}
onChange={onChange}
disabled={props.disabled}
maxLength={props.maxLength}
className={styles.textarea}
onKeyDown={props.onKeyDown}
placeholder={props.placeholder}
onContextMenu={e => e.stopPropagation()}
onKeyUp={ev => {
setHeight(ghost.current.clientHeight);
props.onKeyUp && props.onKeyUp(ev);
}}
ref={ref}
onFocus={() => props.onFocus && props.onFocus(ref.current)}
onBlur={props.onBlur}
/>
<div className={styles.hide}>
<div className={styles.ghost} ref={ghost}>
{props.value
? props.value
.split("\n")
.map(x => `${x}`)
.join("\n")
: undefined ?? "\n"}
</div>
</div>
</div>
);
});
import styled from "styled-components"; import { InfoCircle } from "@styled-icons/boxicons-regular";
import { Info } from "@styled-icons/feather"; import styled, { css } from "styled-components";
import { Children } from "../../types/Preact"; import { Children } from "../../types/Preact";
export const TipBase = styled.div` interface Props {
warning?: boolean;
error?: boolean;
}
export const Separator = styled.div<Props>`
height: 1px;
width: calc(100% - 10px);
background: var(--secondary-header);
margin: 18px auto;
`;
export const TipBase = styled.div<Props>`
display: flex; display: flex;
padding: 12px; padding: 12px;
overflow: hidden; overflow: hidden;
align-items: center; align-items: center;
font-size: 14px; font-size: 14px;
border-radius: 7px;
background: var(--primary-header); background: var(--primary-header);
border-radius: var(--border-radius);
border: 2px solid var(--secondary-header); border: 2px solid var(--secondary-header);
a { a {
...@@ -22,15 +35,37 @@ export const TipBase = styled.div` ...@@ -22,15 +35,37 @@ export const TipBase = styled.div`
svg { svg {
flex-shrink: 0; flex-shrink: 0;
margin-right: 10px; margin-inline-end: 10px;
} }
${(props) =>
props.warning &&
css`
color: var(--warning);
border: 2px solid var(--warning);
background: var(--secondary-header);
`}
${(props) =>
props.error &&
css`
color: var(--error);
border: 2px solid var(--error);
background: var(--secondary-header);
`}
`; `;
export default function Tip(props: { children: Children }) { export default function Tip(
props: Props & { children: Children; hideSeparator?: boolean },
) {
const { children, hideSeparator, ...tipProps } = props;
return ( return (
<TipBase> <>
<Info size={20} strokeWidth={2} /> {!hideSeparator && <Separator />}
<span>{props.children}</span> <TipBase {...tipProps}>
</TipBase> <InfoCircle size={20} />
<span>{children}</span>
</TipBase>
</>
); );
} }
import { ChevronRight, LinkExternal } from "@styled-icons/boxicons-regular";
import styled, { css } from "styled-components";
import { Children } from "../../../types/Preact";
interface BaseProps {
readonly hover?: boolean;
readonly account?: boolean;
readonly disabled?: boolean;
readonly largeDescription?: boolean;
}
const CategoryBase = styled.div<BaseProps>`
/*height: 54px;*/
padding: 9.8px 12px;
border-radius: 6px;
margin-bottom: 10px;
color: var(--foreground);
background: var(--secondary-header);
gap: 12px;
display: flex;
align-items: center;
flex-direction: row;
> svg {
flex-shrink: 0;
}
.content {
display: flex;
flex-grow: 1;
flex-direction: column;
font-weight: 600;
font-size: 14px;
.title {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.description {
${(props) =>
props.largeDescription
? css`
font-size: 14px;
`
: css`
font-size: 11px;
`}
font-weight: 400;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
a:hover {
text-decoration: underline;
}
}
}
${(props) =>
props.hover &&
css`
cursor: pointer;
opacity: 1;
transition: 0.1s ease background-color;
&:hover {
background: var(--secondary-background);
}
`}
${(props) =>
props.disabled &&
css`
opacity: 0.4;
/*.content,
.action {
color: var(--tertiary-foreground);
}*/
.action {
font-size: 14px;
}
`}
${(props) =>
props.account &&
css`
height: 54px;
.content {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
.title {
text-transform: uppercase;
font-size: 12px;
color: var(--secondary-foreground);
}
.description {
font-size: 15px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
}
`}
`;
interface Props extends BaseProps {
icon?: Children;
children?: Children;
description?: Children;
onClick?: () => void;
action?: "chevron" | "external" | Children;
}
export default function CategoryButton({
icon,
children,
description,
account,
disabled,
onClick,
hover,
action,
}: Props) {
return (
<CategoryBase
hover={hover || typeof onClick !== "undefined"}
onClick={onClick}
disabled={disabled}
account={account}>
{icon}
<div class="content">
<div className="title">{children}</div>
<div className="description">{description}</div>
</div>
<div class="action">
{typeof action === "string" ? (
action === "chevron" ? (
<ChevronRight size={24} />
) : (
<LinkExternal size={20} />
)
) : (
action
)}
</div>
</CategoryBase>
);
}
import dayJS from "dayjs";
import calendar from "dayjs/plugin/calendar";
import format from "dayjs/plugin/localizedFormat";
import update from "dayjs/plugin/updateLocale";
import defaultsDeep from "lodash.defaultsdeep";
import { IntlProvider } from "preact-i18n"; import { IntlProvider } from "preact-i18n";
import { useCallback, useEffect, useState } from "preact/hooks";
import { connectState } from "../redux/connector"; import { connectState } from "../redux/connector";
import { useEffect, useState } from "preact/hooks";
import definition from "../../external/lang/en.json"; import definition from "../../external/lang/en.json";
import dayjs from "dayjs"; export const dayjs = dayJS;
import calendar from "dayjs/plugin/calendar";
import update from "dayjs/plugin/updateLocale";
import format from "dayjs/plugin/localizedFormat";
dayjs.extend(calendar); dayjs.extend(calendar);
dayjs.extend(format); dayjs.extend(format);
dayjs.extend(update); dayjs.extend(update);
...@@ -18,6 +24,7 @@ export enum Language { ...@@ -18,6 +24,7 @@ export enum Language {
AZERBAIJANI = "az", AZERBAIJANI = "az",
CZECH = "cs", CZECH = "cs",
GERMAN = "de", GERMAN = "de",
GREEK = "el",
SPANISH = "es", SPANISH = "es",
FINNISH = "fi", FINNISH = "fi",
FRENCH = "fr", FRENCH = "fr",
...@@ -25,6 +32,7 @@ export enum Language { ...@@ -25,6 +32,7 @@ export enum Language {
CROATIAN = "hr", CROATIAN = "hr",
HUNGARIAN = "hu", HUNGARIAN = "hu",
INDONESIAN = "id", INDONESIAN = "id",
ITALIAN = "it",
LITHUANIAN = "lt", LITHUANIAN = "lt",
MACEDONIAN = "mk", MACEDONIAN = "mk",
DUTCH = "nl", DUTCH = "nl",
...@@ -34,6 +42,7 @@ export enum Language { ...@@ -34,6 +42,7 @@ export enum Language {
RUSSIAN = "ru", RUSSIAN = "ru",
SERBIAN = "sr", SERBIAN = "sr",
SWEDISH = "sv", SWEDISH = "sv",
TOKIPONA = "tokipona",
TURKISH = "tr", TURKISH = "tr",
UKRANIAN = "uk", UKRANIAN = "uk",
CHINESE_SIMPLIFIED = "zh_Hans", CHINESE_SIMPLIFIED = "zh_Hans",
...@@ -42,7 +51,6 @@ export enum Language { ...@@ -42,7 +51,6 @@ export enum Language {
PIRATE = "pr", PIRATE = "pr",
BOTTOM = "bottom", BOTTOM = "bottom",
PIGLATIN = "piglatin", PIGLATIN = "piglatin",
HARDCORE = "hardcore",
} }
export interface LanguageEntry { export interface LanguageEntry {
...@@ -51,6 +59,7 @@ export interface LanguageEntry { ...@@ -51,6 +59,7 @@ export interface LanguageEntry {
i18n: string; i18n: string;
dayjs?: string; dayjs?: string;
rtl?: boolean; rtl?: boolean;
cat?: "const" | "alt";
} }
export const Languages: { [key in Language]: LanguageEntry } = { export const Languages: { [key in Language]: LanguageEntry } = {
...@@ -65,13 +74,15 @@ export const Languages: { [key in Language]: LanguageEntry } = { ...@@ -65,13 +74,15 @@ export const Languages: { [key in Language]: LanguageEntry } = {
az: { display: "Azərbaycan dili", emoji: "🇦🇿", i18n: "az" }, az: { display: "Azərbaycan dili", emoji: "🇦🇿", i18n: "az" },
cs: { display: "Čeština", emoji: "🇨🇿", i18n: "cs" }, cs: { display: "Čeština", emoji: "🇨🇿", i18n: "cs" },
de: { display: "Deutsch", emoji: "🇩🇪", i18n: "de" }, de: { display: "Deutsch", emoji: "🇩🇪", i18n: "de" },
el: { display: "Ελληνικά", emoji: "🇬🇷", i18n: "el" },
es: { display: "Español", emoji: "🇪🇸", i18n: "es" }, es: { display: "Español", emoji: "🇪🇸", i18n: "es" },
fi: { display: "suomi", emoji: "🇫🇮", i18n: "fi" }, fi: { display: "suomi", emoji: "🇫🇮", i18n: "fi" },
fr: { display: "Français", emoji: "🇫🇷", i18n: "fr" }, fr: { display: "Français", emoji: "🇫🇷", i18n: "fr" },
hi: { display: "हिन्दी", emoji: "🇮🇳", i18n: "hi" }, hi: { display: "हिन्दी", emoji: "🇮🇳", i18n: "hi" },
hr: { display: "Hrvatski", emoji: "🇭🇷", i18n: "hr" }, hr: { display: "Hrvatski", emoji: "🇭🇷", i18n: "hr" },
hu: { display: "magyar", emoji: "🇭🇺", i18n: "hu" }, hu: { display: "Magyar", emoji: "🇭🇺", i18n: "hu" },
id: { display: "bahasa Indonesia", emoji: "🇮🇩", i18n: "id" }, id: { display: "bahasa Indonesia", emoji: "🇮🇩", i18n: "id" },
it: { display: "Italiano", emoji: "🇮🇹", i18n: "it" },
lt: { display: "Lietuvių", emoji: "🇱🇹", i18n: "lt" }, lt: { display: "Lietuvių", emoji: "🇱🇹", i18n: "lt" },
mk: { display: "Македонски", emoji: "🇲🇰", i18n: "mk" }, mk: { display: "Македонски", emoji: "🇲🇰", i18n: "mk" },
nl: { display: "Nederlands", emoji: "🇳🇱", i18n: "nl" }, nl: { display: "Nederlands", emoji: "🇳🇱", i18n: "nl" },
...@@ -95,20 +106,41 @@ export const Languages: { [key in Language]: LanguageEntry } = { ...@@ -95,20 +106,41 @@ export const Languages: { [key in Language]: LanguageEntry } = {
dayjs: "zh", dayjs: "zh",
}, },
owo: { display: "OwO", emoji: "🐱", i18n: "owo", dayjs: "en-gb" }, tokipona: {
pr: { display: "Pirate", emoji: "🏴‍☠️", i18n: "pr", dayjs: "en-gb" }, display: "Toki Pona",
bottom: { display: "Bottom", emoji: "🥺", i18n: "bottom", dayjs: "en-gb" }, emoji: "🙂",
i18n: "tokipona",
dayjs: "en-gb",
cat: "const",
},
owo: {
display: "OwO",
emoji: "🐱",
i18n: "owo",
dayjs: "en-gb",
cat: "alt",
},
pr: {
display: "Pirate",
emoji: "🏴‍☠️",
i18n: "pr",
dayjs: "en-gb",
cat: "alt",
},
bottom: {
display: "Bottom",
emoji: "🥺",
i18n: "bottom",
dayjs: "en-gb",
cat: "alt",
},
piglatin: { piglatin: {
display: "Pig Latin", display: "Pig Latin",
emoji: "🐖", emoji: "🐖",
i18n: "piglatin", i18n: "piglatin",
dayjs: "en-gb", dayjs: "en-gb",
}, cat: "alt",
hardcore: {
display: "Hardcore Mode",
emoji: "🔥",
i18n: "hardcore",
dayjs: "en-gb",
}, },
}; };
...@@ -117,40 +149,118 @@ interface Props { ...@@ -117,40 +149,118 @@ interface Props {
locale: Language; locale: Language;
} }
export interface Dictionary {
dayjs?: {
defaults?: {
twelvehour?: "yes" | "no";
separator?: string;
date?: "traditional" | "simplified" | "ISO8601";
};
timeFormat?: string;
};
[key: string]:
| Record<string, Omit<Dictionary, "dayjs">>
| string
| undefined;
}
function Locale({ children, locale }: Props) { function Locale({ children, locale }: Props) {
const [defns, setDefinition] = useState(definition); const [defns, setDefinition] = useState<Dictionary>(
const lang = Languages[locale]; definition as Dictionary,
);
useEffect(() => { // Load relevant language information, fallback to English if invalid.
if (locale === "en") { const lang = Languages[locale] ?? Languages.en;
setDefinition(definition);
dayjs.locale("en"); function transformLanguage(source: Dictionary) {
return; // Fallback untranslated strings to English (UK)
} const obj = defaultsDeep(source, definition);
if (lang.i18n === "hardcore") { // Take relevant objects out, dayjs and defaults
// eslint-disable-next-line @typescript-eslint/no-explicit-any // should exist given we just took defaults above.
setDefinition({} as any); const { dayjs } = obj;
return; const { defaults } = dayjs;
}
// Determine whether we are using 12-hour clock.
import(`../../external/lang/${lang.i18n}.json`).then( const twelvehour = defaults?.twelvehour
async (lang_file) => { ? defaults.twelvehour === "yes"
const defn = lang_file.default; : false;
const target = lang.dayjs ?? lang.i18n;
const dayjs_locale = await import(`../../node_modules/dayjs/esm/locale/${target}.js`); // Determine what date separator we are using.
const separator: string = defaults?.date_separator ?? "/";
if (defn.dayjs) {
dayjs.updateLocale(target, { calendar: defn.dayjs }); // Determine what date format we are using.
} const date: "traditional" | "simplified" | "ISO8601" =
defaults?.date_format ?? "traditional";
dayjs.locale(dayjs_locale.default);
// Available date formats.
const DATE_FORMATS = {
traditional: `DD${separator}MM${separator}YYYY`,
simplified: `MM${separator}DD${separator}YYYY`,
ISO8601: "YYYY-MM-DD",
};
// Replace data in dayjs object, make sure to provide fallbacks.
dayjs["sameElse"] = DATE_FORMATS[date] ?? DATE_FORMATS.traditional;
dayjs["timeFormat"] = twelvehour ? "hh:mm A" : "HH:mm";
// Replace {{time}} format string in dayjs strings with the time format.
Object.keys(dayjs)
.filter((k) => typeof dayjs[k] === "string")
.forEach(
(k) =>
(dayjs[k] = dayjs[k].replace(
/{{time}}/g,
dayjs["timeFormat"],
)),
);
return obj;
}
const loadLanguage = useCallback(
(locale: string) => {
if (locale === "en") {
// If English, make sure to restore everything to defaults.
// Use what we already have.
const defn = transformLanguage(definition as Dictionary);
setDefinition(defn); setDefinition(defn);
dayjs.locale("en");
dayjs.updateLocale("en", { calendar: defn.dayjs });
return;
} }
);
}, [locale, lang]); import(`../../external/lang/${lang.i18n}.json`).then(
async (lang_file) => {
// Transform the definitions data.
const defn = transformLanguage(lang_file.default);
// Determine and load dayjs locales.
const target = lang.dayjs ?? lang.i18n;
const dayjs_locale = await import(
`../../node_modules/dayjs/esm/locale/${target}.js`
);
// Load dayjs locales.
dayjs.locale(target, dayjs_locale.default);
if (defn.dayjs) {
// Override dayjs calendar locales with our own.
dayjs.updateLocale(target, { calendar: defn.dayjs });
}
// Apply definition to app.
setDefinition(defn);
},
);
},
[lang.dayjs, lang.i18n],
);
useEffect(() => loadLanguage(locale), [locale, lang, loadLanguage]);
useEffect(() => { useEffect(() => {
// Apply RTL language format.
document.body.style.direction = lang.rtl ? "rtl" : ""; document.body.style.direction = lang.rtl ? "rtl" : "";
}, [lang.rtl]); }, [lang.rtl]);
...@@ -164,5 +274,5 @@ export default connectState<Omit<Props, "locale">>( ...@@ -164,5 +274,5 @@ export default connectState<Omit<Props, "locale">>(
locale: state.locale, locale: state.locale,
}; };
}, },
true true,
); );
...@@ -4,29 +4,58 @@ ...@@ -4,29 +4,58 @@
// //
// Replace references to SettingsContext with connectState in the future // Replace references to SettingsContext with connectState in the future
// if it does cause problems though. // if it does cause problems though.
//
// This now also supports Audio stuff.
import defaultsDeep from "lodash.defaultsdeep";
import { createContext } from "preact";
import { useMemo } from "preact/hooks";
import { Settings } from "../redux/reducers/settings";
import { connectState } from "../redux/connector"; import { connectState } from "../redux/connector";
import {
DEFAULT_SOUNDS,
Settings,
SoundOptions,
} from "../redux/reducers/settings";
import { playSound, Sounds } from "../assets/sounds/Audio";
import { Children } from "../types/Preact"; import { Children } from "../types/Preact";
import { createContext } from "preact";
export const SettingsContext = createContext<Settings>({} as any); export const SettingsContext = createContext<Settings>({});
export const SoundContext = createContext<(sound: Sounds) => void>(null!);
interface Props { interface Props {
children?: Children, children?: Children;
settings: Settings settings: Settings;
} }
function Settings(props: Props) { function SettingsProvider({ settings, children }: Props) {
const play = useMemo(() => {
const enabled: SoundOptions = defaultsDeep(
settings.notification?.sounds ?? {},
DEFAULT_SOUNDS,
);
return (sound: Sounds) => {
if (enabled[sound]) {
playSound(sound);
}
};
}, [settings.notification]);
return ( return (
<SettingsContext.Provider value={props.settings}> <SettingsContext.Provider value={settings}>
{ props.children } <SoundContext.Provider value={play}>
{children}
</SoundContext.Provider>
</SettingsContext.Provider> </SettingsContext.Provider>
) );
} }
export default connectState(Settings, state => { export default connectState<Omit<Props, "settings">>(
return { SettingsProvider,
settings: state.settings (state) => {
} return {
}); settings: state.settings,
};
},
);