Files
element-call/src/grid/SpotlightLayout.tsx

125 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-05-17 16:38:00 -04:00
/*
Copyright 2024 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { CSSProperties, forwardRef, useMemo } from "react";
2024-05-17 16:38:00 -04:00
import { useObservableEagerState } from "observable-hooks";
import classNames from "classnames";
2024-05-17 16:38:00 -04:00
import { CallLayout, GridTileModel, TileModel } from "./CallLayout";
2024-05-17 16:38:00 -04:00
import { SpotlightLayout as SpotlightLayoutModel } from "../state/CallViewModel";
import styles from "./SpotlightLayout.module.css";
import { useReactiveState } from "../useReactiveState";
2024-05-17 16:38:00 -04:00
interface GridCSSProperties extends CSSProperties {
"--grid-columns": number;
}
interface Layout {
orientation: "portrait" | "landscape";
gridColumns: number;
}
function getLayout(gridLength: number, width: number): Layout {
const orientation = width < 800 ? "portrait" : "landscape";
return {
orientation,
gridColumns:
orientation === "portrait"
? Math.floor(width / 190)
: gridLength > 20
? 2
: 1,
};
}
2024-05-17 16:38:00 -04:00
export const makeSpotlightLayout: CallLayout<SpotlightLayoutModel> = ({
minBounds,
}) => ({
fixed: forwardRef(function SpotlightLayoutFixed({ model, Slot }, ref) {
const { width, height } = useObservableEagerState(minBounds);
const layout = getLayout(model.grid.length, width);
const tileModel: TileModel = useMemo(
() => ({
type: "spotlight",
vms: model.spotlight,
maximised: layout.orientation === "portrait",
}),
[model.spotlight, layout.orientation],
);
const [generation] = useReactiveState<number>(
(prev) => (prev === undefined ? 0 : prev + 1),
[model.grid.length, width, height],
);
2024-05-17 16:38:00 -04:00
return (
<div
ref={ref}
data-generation={generation}
data-orientation={layout.orientation}
2024-06-07 16:59:56 -04:00
className={styles.layer}
style={{ "--grid-columns": layout.gridColumns } as GridCSSProperties}
>
<div className={styles.spotlight}>
<Slot className={styles.slot} id="spotlight" model={tileModel} />
2024-05-17 16:38:00 -04:00
</div>
<div className={styles.grid} />
</div>
);
}),
2024-05-17 16:38:00 -04:00
scrolling: forwardRef(function SpotlightLayoutScrolling(
{ model, Slot },
ref,
) {
const { width, height } = useObservableEagerState(minBounds);
const layout = getLayout(model.grid.length, width);
const tileModels: GridTileModel[] = useMemo(
() => model.grid.map((vm) => ({ type: "grid", vm })),
[model.grid],
);
const [generation] = useReactiveState<number>(
(prev) => (prev === undefined ? 0 : prev + 1),
[model.spotlight.length, model.grid, width, height],
);
2024-05-17 16:38:00 -04:00
return (
<div
ref={ref}
data-generation={generation}
data-orientation={layout.orientation}
2024-06-07 16:59:56 -04:00
className={styles.layer}
style={{ "--grid-columns": layout.gridColumns } as GridCSSProperties}
>
2024-05-17 16:38:00 -04:00
<div
className={classNames(styles.spotlight, {
[styles.withIndicators]: model.spotlight.length > 1,
})}
/>
<div className={styles.grid}>
{tileModels.map((m) => (
<Slot
key={m.vm.id}
className={styles.slot}
id={m.vm.id}
model={m}
/>
))}
2024-05-17 16:38:00 -04:00
</div>
</div>
);
}),
2024-05-17 16:38:00 -04:00
});