Skip to content
Open
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
3 changes: 2 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let

in pkgs.mkShell {
packages = with pkgs; [
nodejs_24
mise
cargo-binstall
(writeShellScriptBin "fish" ''
Expand Down Expand Up @@ -49,4 +50,4 @@ in pkgs.mkShell {
echo "✅ Playwright versions in nix and npm are the same"
fi
'';
}
}
5 changes: 5 additions & 0 deletions packages/client/components/state/stores/Draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export const ALLOWED_IMAGE_TYPES = [
"image/webp",
];

/**
* Human-readable labels for each entry in ALLOWED_IMAGE_TYPES (same order)
*/
export const ALLOWED_IMAGE_TYPE_LABELS = ["JPEG", "PNG", "GIF", "WebP"];

/**
* Message drafts store
*/
Expand Down
34 changes: 30 additions & 4 deletions packages/client/components/ui/components/utils/Form2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import {
For,
Match,
Show,
Switch,
splitProps,
Switch,
} from "solid-js";

import { Trans } from "@lingui-solid/solid/macro";
import { Trans, useLingui } from "@lingui-solid/solid/macro";
import { VirtualContainer } from "@minht11/solid-virtual-container";
import { css } from "styled-system/css";
import { styled } from "styled-system/jsx";

import { useModals } from "../../../modal";
import {
ALLOWED_IMAGE_TYPE_LABELS,
ALLOWED_IMAGE_TYPES,
} from "../../../state/stores/Draft";

import { Button, Checkbox, Radio2, Text, TextField } from "../design";
import { TextEditor2 } from "../features/texteditor/TextEditor2";

Expand Down Expand Up @@ -138,6 +144,11 @@ const FormFileInput = (
>,
) => {
const [local, remote] = splitProps(props, ["label", "control"]);
const { t } = useLingui();
const { openModal } = useModals();

// track last accepted value so we can restore it if an invalid file is rejected
let lastAcceptedValue = local.control.value;

return (
<>
Expand All @@ -148,8 +159,23 @@ const FormFileInput = (
{...remote}
file={local.control.value}
onFiles={(files) => {
// TODO: do validation of files here

if (files && remote.accept === "image/*") {
const file = files[0];
if (!ALLOWED_IMAGE_TYPES.includes(file.type)) {
const formatList = ALLOWED_IMAGE_TYPE_LABELS.join(", ");
const error = new Error(
t`The file "${file.name}" has an unsupported format. Supported formats are: ${formatList}.`,
);
error.name = t`Unsupported file format`;
openModal({ type: "error2", error });
// FileInput resets to null before passing the new file for
// reactivity reasons, so restore the last accepted value
Comment on lines +171 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can fix the root cause here instead of doing a workaround?

local.control.setValue(lastAcceptedValue);
return;
}
}

if (files) lastAcceptedValue = files;
local.control.setValue(files);
local.control.markDirty(true);
}}
Expand Down