/ src / lib / utils / view-utils.ts
view-utils.ts
  1  import { timeFilterNames } from '../../hooks/use-time-filter';
  2  
  3  export type ParamsType = {
  4    accountCommentIndex?: string;
  5    commentCid?: string;
  6    subplebbitAddress?: string;
  7    timeFilterName?: string;
  8  };
  9  
 10  export const isAllView = (pathname: string, params: ParamsType): boolean => {
 11    const { timeFilterName } = params;
 12  
 13    if (timeFilterName && !timeFilterNames.includes(timeFilterName)) {
 14      return false;
 15    }
 16  
 17    return (
 18      pathname === '/p/all' ||
 19      pathname === '/p/all/settings' ||
 20      pathname === `/p/all/${timeFilterName}` ||
 21      pathname === `/p/all/${timeFilterName}/settings` ||
 22      pathname === '/p/all/catalog' ||
 23      pathname === '/p/all/catalog/settings' ||
 24      pathname === `/p/all/catalog/${timeFilterName}` ||
 25      pathname === `/p/all/catalog/${timeFilterName}/settings` ||
 26      pathname === '/p/all/description' ||
 27      pathname === '/p/all/description/settings'
 28    );
 29  };
 30  
 31  export const isBoardView = (pathname: string, params: ParamsType): boolean => {
 32    // some subs might use emojis in their address, so we need to decode the pathname
 33    const decodedPathname = decodeURIComponent(pathname);
 34    return params.subplebbitAddress ? decodedPathname.startsWith(`/p/${params.subplebbitAddress}`) : false;
 35  };
 36  
 37  export const isCatalogView = (pathname: string, params: ParamsType): boolean => {
 38    const { subplebbitAddress, timeFilterName } = params;
 39    const decodedPathname = decodeURIComponent(pathname);
 40  
 41    return (
 42      decodedPathname === `/p/${subplebbitAddress}/catalog` ||
 43      decodedPathname === `/p/${subplebbitAddress}/catalog/settings` ||
 44      decodedPathname === `/p/all/catalog` ||
 45      decodedPathname === `/p/all/catalog/settings` ||
 46      decodedPathname === `/p/all/catalog/${timeFilterName}` ||
 47      decodedPathname === `/p/all/catalog/${timeFilterName}/settings` ||
 48      decodedPathname === `/p/subscriptions/catalog` ||
 49      decodedPathname === `/p/subscriptions/catalog/settings` ||
 50      decodedPathname === `/p/subscriptions/catalog/${timeFilterName}` ||
 51      decodedPathname === `/p/subscriptions/catalog/${timeFilterName}/settings`
 52    );
 53  };
 54  
 55  export const isDescriptionView = (pathname: string, params: ParamsType): boolean => {
 56    const decodedPathname = decodeURIComponent(pathname);
 57    return pathname === '/p/all/description' ? true : params.subplebbitAddress ? decodedPathname.startsWith(`/p/${params.subplebbitAddress}/description`) : false;
 58  };
 59  
 60  export const isHomeView = (pathname: string): boolean => {
 61    return pathname === '/';
 62  };
 63  
 64  export const isPendingPostView = (pathname: string, params: ParamsType): boolean => {
 65    return pathname === `/profile/${params.accountCommentIndex}` || pathname === `/profile/${params.accountCommentIndex}/settings`;
 66  };
 67  
 68  export const isPostPageView = (pathname: string, params: ParamsType): boolean => {
 69    if (isDescriptionView(pathname, params) || isRulesView(pathname, params)) {
 70      return true;
 71    }
 72    const decodedPathname = decodeURIComponent(pathname);
 73    return params.subplebbitAddress && params.commentCid ? decodedPathname.startsWith(`/p/${params.subplebbitAddress}/c/${params.commentCid}`) : false;
 74  };
 75  
 76  export const isRulesView = (pathname: string, params: ParamsType): boolean => {
 77    const decodedPathname = decodeURIComponent(pathname);
 78    return params.subplebbitAddress ? decodedPathname.startsWith(`/p/${params.subplebbitAddress}/rules`) : false;
 79  };
 80  
 81  export const isSettingsView = (pathname: string, params: ParamsType): boolean => {
 82    const { accountCommentIndex, commentCid, subplebbitAddress } = params;
 83    const decodedPathname = decodeURIComponent(pathname);
 84    return (
 85      decodedPathname === `/p/${subplebbitAddress}/c/${commentCid}/settings` ||
 86      decodedPathname === `/p/${subplebbitAddress}/description/settings` ||
 87      decodedPathname === `/p/${subplebbitAddress}/rules/settings` ||
 88      decodedPathname === `/profile/${accountCommentIndex}/settings`
 89    );
 90  };
 91  
 92  export const isSubscriptionsView = (pathname: string, params: ParamsType): boolean => {
 93    const { timeFilterName } = params;
 94    return (
 95      pathname === '/p/subscriptions' ||
 96      pathname === '/p/subscriptions/settings' ||
 97      pathname === `/p/subscriptions/${timeFilterName}` ||
 98      pathname === `/p/subscriptions/${timeFilterName}/settings` ||
 99      pathname === '/p/subscriptions/catalog' ||
100      pathname === '/p/subscriptions/catalog/settings' ||
101      pathname === `/p/subscriptions/catalog/${timeFilterName}` ||
102      pathname === `/p/subscriptions/catalog/${timeFilterName}/settings`
103    );
104  };
105  
106  export const isNotFoundView = (pathname: string, params: ParamsType): boolean => {
107    return (
108      !isAllView(pathname, params) &&
109      !isBoardView(pathname, params) &&
110      !isCatalogView(pathname, params) &&
111      !isDescriptionView(pathname, params) &&
112      !isHomeView(pathname) &&
113      !isPendingPostView(pathname, params) &&
114      !isPostPageView(pathname, params) &&
115      !isRulesView(pathname, params) &&
116      !isSettingsView(pathname, params) &&
117      !isSubscriptionsView(pathname, params)
118    );
119  };