Sha256: 6b64be78d3fbb46f3066db51df1f212065576989953886a9b1356bed0cd41e33

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

import { useState } from "react";

import * as Images from "../../types/Images";

import { useDragCollection } from "../drag";

function filterRecords(records: Images.Record[], enablePrimary = false) {
  const primaryRecords = enablePrimary
    ? records.filter((r) => r.primary).slice(0, 1)
    : [];
  const imageRecords = records.filter((r) => primaryRecords.indexOf(r) === -1);
  return [primaryRecords, imageRecords];
}

export default function useImageGrid(
  records: Images.Record[],
  enablePrimary = false
): Images.GridState {
  const [primaryRecords, imageRecords] = filterRecords(records, enablePrimary);

  const primary = useDragCollection(primaryRecords);
  const images = useDragCollection(imageRecords);
  const [deleted, setDeleted] = useState<Images.Record[]>([]);

  const update = (records: Images.Record[]) => {
    const [primaryRecords, imageRecords] = filterRecords(
      records,
      enablePrimary
    );
    primary.dispatch({ type: "reinitialize", payload: primaryRecords });
    images.dispatch({ type: "reinitialize", payload: imageRecords });
    setDeleted([]);
  };

  return {
    primary: primary,
    images: images,
    deleted: deleted,
    setDeleted: setDeleted,
    update: update
  };
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/javascript/components/ImageGrid/useImageGrid.ts
pages_core-3.15.4 app/javascript/components/ImageGrid/useImageGrid.ts
pages_core-3.15.3 app/javascript/components/ImageGrid/useImageGrid.ts