File size: 1,232 Bytes
89682f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { presetArtStyles } from "@/util/presets";
import {
  decompressFromEncodedURIComponent,
  compressToEncodedURIComponent,
} from "lz-string";
import { useSearchParams } from "next/navigation";

export interface Option {
  emoji: string;
  name: string;
  prompt: string;
  seed: number;
  strength: number;
}

const fallbackOptions: Option = {
  emoji: "🐤",
  name: "cat",
  prompt: presetArtStyles[0].prompt,
  seed: 2159232,
  strength: 0.7,
};

export const shareString2Json = (shareString: string): Option => {
  try {
    return JSON.parse(decompressFromEncodedURIComponent(shareString));
  } catch (_) {
    return fallbackOptions;
  }
};

export const useShare = (): { option: Option; hasShare: boolean } => {
  const searchParams = useSearchParams();
  const shareParam = searchParams.get("share");
  if (shareParam) {
    try {
      return {
        option: JSON.parse(decompressFromEncodedURIComponent(shareParam)),
        hasShare: true,
      };
    } catch (_) {
      return { option: fallbackOptions, hasShare: false };
    }
  }
  return { option: fallbackOptions, hasShare: false };
};

export const getShareUrl = (option: Option) => {
  return compressToEncodedURIComponent(JSON.stringify(option));
};