wcz-layout
Version:
1 lines • 6.27 kB
Source Map (JSON)
{"version":3,"file":"utils-DpcYsXTK.mjs","names":["AnyFieldApi","ParsedLocation","redirect","permissions","clientEnv","TokenPayload","User","getUser","WISTRON_PRIMARY_COLOR","WISTRON_SECONDARY_COLOR","Platform","isAndroid","test","userAgent","isIOS","isWindows","isMacOS","navigator","RootRouteHeadOptions","manifest","rootRouteHead","options","meta","charSet","name","content","title","VITE_APP_TITLE","links","rel","sizes","href","type","requirePermission","permissionKey","location","user","encodeURIComponent","reloadDocument","hasPermission","Error","FormOmittedProps","getFieldStatus","field","state","isTouched","hasError","errors","length","helperText","message","toKebabCase","str","replaceAll","toLowerCase","buildUser","payload","split","email","preferred_username","department","toUpperCase","employeeId","companyName","groups","key","some","group","includes"],"sources":["../src/lib/utils.ts"],"sourcesContent":["import type { AnyFieldApi } from \"@tanstack/react-form\";\nimport { ParsedLocation, redirect } from \"@tanstack/react-router\";\nimport { permissions } from \"virtual:wcz-layout\";\nimport { clientEnv } from \"~/env\";\nimport type { TokenPayload } from \"~/models/TokenPayload\";\nimport type { User } from \"~/models/User\";\nimport { getUser } from \"./auth/user\";\n\nexport const WISTRON_PRIMARY_COLOR = \"#00506E\";\nexport const WISTRON_SECONDARY_COLOR = \"#64DC00\";\n\nexport class Platform {\n static get isAndroid() {\n return /android/i.test(this.userAgent);\n }\n static get isIOS() {\n return /iPad|iPhone|iPod/.test(this.userAgent);\n }\n static get isWindows() {\n return /windows/i.test(this.userAgent);\n }\n static get isMacOS() {\n return /Macintosh|MacIntel|MacPPC|Mac68K/.test(this.userAgent);\n }\n\n private static get userAgent() {\n return typeof navigator === \"undefined\" ? \"\" : navigator.userAgent;\n }\n}\n\ninterface RootRouteHeadOptions {\n manifest?: string;\n}\n\nexport const rootRouteHead = (options?: RootRouteHeadOptions) => ({\n meta: [\n { charSet: \"utf-8\" },\n { name: \"viewport\", content: \"width=device-width, initial-scale=1\" },\n { title: clientEnv.VITE_APP_TITLE },\n { name: \"og:type\", content: \"website\" },\n { name: \"og:title\", content: clientEnv.VITE_APP_TITLE },\n { name: \"og:image\", content: \"/favicon-32x32.png\" },\n ],\n links: [\n { rel: \"apple-touch-icon\", sizes: \"180x180\", href: \"/apple-touch-icon.png\" },\n { rel: \"icon\", type: \"image/png\", sizes: \"32x32\", href: \"/favicon-32x32.png\" },\n { rel: \"icon\", type: \"image/png\", sizes: \"16x16\", href: \"/favicon-16x16.png\" },\n { rel: \"manifest\", href: options?.manifest || \"/manifest.json\" },\n { rel: \"icon\", href: \"/favicon.ico\" },\n ],\n});\n\nexport const requirePermission = (permissionKey: keyof typeof permissions) => {\n return async ({ location }: { location: ParsedLocation }) => {\n const user = await getUser();\n\n if (!user)\n throw redirect({\n href: `/auth/login?returnTo=${encodeURIComponent(location.href)}`,\n reloadDocument: true,\n });\n\n if (!hasPermission(user, permissionKey))\n throw new Error(\"You do not have permission to access this page.\");\n\n return { user };\n };\n};\n\n/* Internal Utils */\nexport type FormOmittedProps =\n | \"name\"\n | \"value\"\n | \"onChange\"\n | \"onBlur\"\n | \"error\"\n | \"helperText\"\n | \"renderInput\"\n | \"type\"\n | \"aria-label\";\n\nexport const getFieldStatus = (field: AnyFieldApi) => {\n const { meta } = field.state;\n\n const isTouched = meta.isTouched;\n const hasError = !!meta.errors.length;\n const helperText = meta.errors[0]?.message;\n\n return { isTouched, hasError, helperText };\n};\n\nexport const toKebabCase = (str: string): string => {\n return str\n .replaceAll(/([a-z])([A-Z])/g, \"$1-$2\")\n .replaceAll(/[\\s_]+/g, \"-\")\n .replaceAll(/[^a-zA-Z0-9-]/g, \"\")\n .toLowerCase()\n .replaceAll(/-+/g, \"-\")\n .replaceAll(/(^-|-$)/g, \"\");\n};\n\nexport const buildUser = (payload: TokenPayload): User => ({\n name: payload.name?.split(\"/\")[0],\n email: payload.preferred_username?.toLowerCase(),\n department: payload.department?.toUpperCase() || \"\",\n employeeId: payload.employeeId?.toUpperCase() || \"\",\n companyName: payload.companyName || \"\",\n groups: payload.groups ?? [],\n});\n\nexport const hasPermission = (\n user: User | null | undefined,\n key: keyof typeof permissions,\n): boolean => (user ? permissions[key].some((group) => user.groups.includes(group)) : false);\n"],"mappings":";;;;;AAQA,MAAaQ,wBAAwB;AACrC,MAAaC,0BAA0B;AAEvC,IAAaC,WAAb,MAAsB;CACpB,WAAWC,YAAY;EACrB,OAAO,WAAWC,KAAK,KAAKC,SAAS;CACvC;CACA,WAAWC,QAAQ;EACjB,OAAO,mBAAmBF,KAAK,KAAKC,SAAS;CAC/C;CACA,WAAWE,YAAY;EACrB,OAAO,WAAWH,KAAK,KAAKC,SAAS;CACvC;CACA,WAAWG,UAAU;EACnB,OAAO,mCAAmCJ,KAAK,KAAKC,SAAS;CAC/D;CAEA,WAAmBA,YAAY;EAC7B,OAAO,OAAOI,cAAc,cAAc,KAAKA,UAAUJ;CAC3D;AACF;AAMA,MAAaO,iBAAiBC,aAAoC;CAChEC,MAAM;EACJ,EAAEC,SAAS,QAAQ;EACnB;GAAEC,MAAM;GAAYC,SAAS;EAAsC;EACnE,EAAEC,OAAOtB,UAAUuB,eAAe;EAClC;GAAEH,MAAM;GAAWC,SAAS;EAAU;EACtC;GAAED,MAAM;GAAYC,SAASrB,UAAUuB;EAAe;EACtD;GAAEH,MAAM;GAAYC,SAAS;EAAqB;CAAC;CAErDG,OAAO;EACL;GAAEC,KAAK;GAAoBC,OAAO;GAAWC,MAAM;EAAwB;EAC3E;GAAEF,KAAK;GAAQG,MAAM;GAAaF,OAAO;GAASC,MAAM;EAAqB;EAC7E;GAAEF,KAAK;GAAQG,MAAM;GAAaF,OAAO;GAASC,MAAM;EAAqB;EAC7E;GAAEF,KAAK;GAAYE,MAAMV,SAASF,YAAY;EAAiB;EAC/D;GAAEU,KAAK;GAAQE,MAAM;EAAe;CAAC;AAEzC;AAEA,MAAaE,qBAAqBC,kBAA4C;CAC5E,OAAO,OAAO,EAAEC,eAA6C;EAC3D,MAAMC,OAAO,MAAM7B,QAAQ;EAE3B,IAAI,CA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