()
| 55 | type BatchAction = "delete" | "update"; |
| 56 | |
| 57 | const WorkspacesPage: FC = () => { |
| 58 | const queryClient = useQueryClient(); |
| 59 | // We have to be careful with how we use useSearchParams or any other |
| 60 | // derived hooks. The URL is global state, but each call to useSearchParams |
| 61 | // creates a different, contradictory source of truth for what the URL |
| 62 | // should look like. We need to make sure that we only mount the hook once |
| 63 | // per page |
| 64 | const [searchParams, setSearchParams] = useSafeSearchParams(); |
| 65 | // Always need to make sure that we reset the checked workspaces each time |
| 66 | // the filtering or pagination changes, as that will almost always change |
| 67 | // which workspaces are shown on screen and which can be interacted with |
| 68 | const [checkedWorkspaceIds, setCheckedWorkspaceIds] = useState( |
| 69 | new Set<string>(), |
| 70 | ); |
| 71 | const resetChecked = () => { |
| 72 | if (checkedWorkspaceIds.size !== 0) { |
| 73 | setCheckedWorkspaceIds(new Set()); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | const pagination = usePagination({ |
| 78 | searchParams, |
| 79 | onSearchParamsChange: (newParams) => { |
| 80 | setSearchParams(newParams); |
| 81 | resetChecked(); |
| 82 | }, |
| 83 | }); |
| 84 | const { permissions, user: me } = useAuthenticated(); |
| 85 | const templatesQuery = useQuery(templates()); |
| 86 | const workspacePermissionsQuery = useQuery( |
| 87 | workspacePermissionsByOrganization( |
| 88 | templatesQuery.data?.map((template) => template.organization_id), |
| 89 | me.id, |
| 90 | ), |
| 91 | ); |
| 92 | |
| 93 | // Filter templates based on workspace creation permission |
| 94 | const filteredTemplates = useMemo(() => { |
| 95 | if (!templatesQuery.data || !workspacePermissionsQuery.data) { |
| 96 | return templatesQuery.data; |
| 97 | } |
| 98 | |
| 99 | return templatesQuery.data.filter((template) => { |
| 100 | const workspacePermission = |
| 101 | workspacePermissionsQuery.data[template.organization_id]; |
| 102 | return workspacePermission?.createWorkspaceForUserID; |
| 103 | }); |
| 104 | }, [templatesQuery.data, workspacePermissionsQuery.data]); |
| 105 | |
| 106 | const filterState = useWorkspacesFilter({ |
| 107 | searchParams, |
| 108 | onSearchParamsChange: setSearchParams, |
| 109 | onFilterChange: () => { |
| 110 | pagination.goToPage(1); |
| 111 | resetChecked(); |
| 112 | }, |
| 113 | }); |
| 114 |
nothing calls this directly
no test coverage detected