Path: blob/master/webhooks/webhook_preview/lib/utils.ts
1072 views
import { type ClassValue, clsx } from "clsx";1import { twMerge } from "tailwind-merge";2import { WebhookData, isType1, isType2 } from "@/types/webhook";34export function cn(...inputs: ClassValue[]) {5return twMerge(clsx(inputs));6}78export function generateUUID() {9return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {10const r = (Math.random() * 16) | 0,11v = c === "x" ? r : (r & 0x3) | 0x8;12return v.toString(16);13});14}1516export function getBaseUrl() {17if (typeof window !== "undefined") {18return window.location.origin;19}2021// For server-side rendering22if (process.env.VERCEL_URL) {23return `https://${process.env.VERCEL_URL}`;24}2526// Fallback for local development27return "http://localhost:3000";28}2930export function formatDate(dateString: string) {31if (!dateString) return "Unknown Date";3233try {34const date = new Date(dateString);35return new Intl.DateTimeFormat("en-US", {36dateStyle: "short",37timeStyle: "short",38}).format(date);39} catch (error) {40console.error("Error formatting date:", error);41return "Invalid Date";42}43}4445export function getPlateNumber(data: WebhookData): string {46try {47// Check if data has the expected structure48if (!data?.data?.results?.[0]) {49return "Unknown Plate";50}5152if (isType1(data)) {53const result = data.data.results[0];54if (result.plate && typeof result.plate === "string") {55return result.plate.trim() === "" ? "NO PLATE" : result.plate;56}57}5859if (isType2(data)) {60const result = data.data.results[0];61if (result.plate?.props?.plate?.[0]?.value) {62const plateValue = result.plate.props.plate[0].value;63return plateValue.trim() === "" ? "NO PLATE" : plateValue;64}65}6667// If we get here, there's no valid plate68return "NO PLATE";69} catch (error) {70console.error("Error getting plate number:", error);71return "Unknown Plate";72}73}7475export function getPlateScore(data: WebhookData): number | null {76try {77if (isType1(data)) {78const result = data.data.results[0];79return result.score ?? null;80}8182if (isType2(data)) {83const result = data.data.results[0];84return result.plate?.score ?? null;85}8687return null;88} catch (error) {89console.error("Error getting plate score:", error);90return null;91}92}9394export function getRegion(data: WebhookData): string | null {95try {96if (isType1(data)) {97const firstResult = data.data.results[0];98return firstResult.region?.code?.toUpperCase() ?? null;99}100101if (isType2(data)) {102const firstResult = data.data.results[0];103return (104firstResult.plate?.props?.region?.[0]?.value?.toUpperCase() ?? null105);106}107108return null;109} catch (error) {110console.error("Error getting region:", error);111return null;112}113}114115export function getRegionScore(data: WebhookData): number | null {116try {117if (isType1(data)) {118const firstResult = data.data.results[0];119return firstResult.region?.score ?? null;120}121122if (isType2(data)) {123const firstResult = data.data.results[0];124return firstResult.plate?.props?.region?.[0]?.score ?? null;125}126127return null;128} catch (error) {129console.error("Error getting region score:", error);130return null;131}132}133134export function getDirection(data: WebhookData): number | null {135try {136if (isType1(data)) {137const firstResult = data.data.results[0];138return firstResult.direction ?? null;139}140141if (isType2(data)) {142const firstResult = data.data.results[0];143return firstResult.direction ?? null;144}145146return null;147} catch (error) {148console.error("Error getting direction:", error);149return null;150}151}152153export function getVehicleOrientation(data: WebhookData): string | null {154try {155if (isType1(data)) {156const firstResult = data.data.results[0];157if (firstResult.orientation?.[0]?.orientation) {158return firstResult.orientation[0].orientation;159}160}161162if (isType2(data)) {163const firstResult = data.data.results[0];164if (firstResult.vehicle?.props?.orientation?.[0]?.value) {165return firstResult.vehicle.props.orientation[0].value;166}167}168169return null;170} catch (error) {171console.error("Error getting vehicle orientation:", error);172return null;173}174}175176export function getVehicleType(data: WebhookData): string {177try {178if (isType1(data)) {179const firstResult = data.data.results[0];180if (firstResult.vehicle?.type) {181return firstResult.vehicle.type;182}183}184185if (isType2(data)) {186const firstResult = data.data.results[0];187if (firstResult.vehicle?.type) {188return firstResult.vehicle.type;189}190}191192return "Unknown Vehicle";193} catch (error) {194console.error("Error getting vehicle type:", error);195return "Unknown Vehicle";196}197}198199export function getVehicleMake(data: WebhookData): string {200try {201if (isType1(data)) {202const firstResult = data.data.results[0];203if (firstResult.model_make?.[0]?.make) {204return firstResult.model_make[0].make;205}206}207208if (isType2(data)) {209const firstResult = data.data.results[0];210if (firstResult.vehicle?.props?.make_model?.[0]?.make) {211return firstResult.vehicle.props.make_model[0].make;212}213}214215return "Unknown Make";216} catch (error) {217console.error("Error getting vehicle make:", error);218return "Unknown Make";219}220}221222export function getVehicleModel(data: WebhookData): string {223try {224if (isType1(data)) {225const firstResult = data.data.results[0];226if (firstResult.model_make?.[0]?.model) {227return firstResult.model_make[0].model;228}229}230231if (isType2(data)) {232const firstResult = data.data.results[0];233if (firstResult.vehicle?.props?.make_model?.[0]?.model) {234return firstResult.vehicle.props.make_model[0].model;235}236}237238return "Unknown Model";239} catch (error) {240console.error("Error getting vehicle model:", error);241return "Unknown Model";242}243}244245export function getVehicleColor(data: WebhookData): string {246try {247if (isType1(data)) {248const firstResult = data.data.results[0];249if (firstResult.color?.[0]?.color) {250return firstResult.color[0].color;251}252}253254if (isType2(data)) {255const firstResult = data.data.results[0];256if (firstResult.vehicle?.props?.color?.[0]?.value) {257return firstResult.vehicle.props.color[0].value;258}259}260261return "Unknown Color";262} catch (error) {263console.error("Error getting vehicle color:", error);264return "Unknown Color";265}266}267268export function getCameraId(data: WebhookData): string {269try {270return data?.data?.camera_id || "Unknown Camera";271} catch (error) {272console.error("Error getting camera ID:", error);273return "Unknown Camera";274}275}276277export function getTimestamp(data: WebhookData): string {278try {279return data?.data?.timestamp || "Unknown Time";280} catch (error) {281console.error("Error getting timestamp:", error);282return "Unknown Time";283}284}285286export function getImageurl(data: WebhookData): string | null {287try {288return data?.image?.url || null;289} catch (error) {290console.error("Error getting Image:", error);291return "Unknown Image";292}293}294295export const setStoredUuid = (uuid: string) => {296if (typeof window !== "undefined") {297localStorage.setItem("webhookUuid", uuid);298}299};300301302